我有一个方法,读取设置从我的配置文件像这样:
var value = ConfigurationManager.AppSettings[key];
它在仅针对. net标准2.0时编译良好。
现在我需要多个目标,所以我更新了我的项目文件:
<TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks>
但是现在,netcoreapp2.0的编译失败,并出现以下错误消息:
名称“ConfigurationManager”在当前上下文中不存在(netcoreapp2.0)
另外,我创建了一个新的。net Core 2.0控制台应用程序(这次只针对。net Core 2.0),但同样地,在命名空间System.Configuration下似乎没有ConfigurationManager。
我很困惑,因为它在。net标准2.0下可用,所以我希望它在。net核心2.0中可用,因为。net核心2.0是。net标准2.0兼容的。
我错过了什么?
最新的指南如下:(来自https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables)
Use:
System.Environment。GetEnvironmentVariable(名称、EnvironmentVariableTarget.Process);
从文档中可以看出:
public static class EnvironmentVariablesExample
{
[FunctionName("GetEnvironmentVariables")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
log.LogInformation(GetEnvironmentVariable("AzureWebJobsStorage"));
log.LogInformation(GetEnvironmentVariable("WEBSITE_SITE_NAME"));
}
public static string GetEnvironmentVariable(string name)
{
return name + ": " +
System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}
}
App settings can be read from environment variables both when developing locally and when running in Azure. When developing locally, app settings come from the Values collection in the local.settings.json file. In both environments, local and Azure, GetEnvironmentVariable("<app setting name>") retrieves the value of the named app setting. For instance, when you're running locally, "My Site Name" would be returned if your local.settings.json file contains { "Values": { "WEBSITE_SITE_NAME": "My Site Name" } }.
The System.Configuration.ConfigurationManager.AppSettings property is an alternative API for getting app setting values, but we recommend that you use GetEnvironmentVariable as shown here.