Ale*_*éau 104 c# app-config .net-core .net-standard
我有一个从我的配置文件中读取设置的方法,如下所示:
var value = ConfigurationManager.AppSettings[key];
Run Code Online (Sandbox Code Playgroud)
只针对.NET Standard 2.0时,它编译得很好.
现在我需要多个目标,所以我用以下内容更新了我的项目文件:
<TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks>
Run Code Online (Sandbox Code Playgroud)
但现在,编译失败netcoreapp2.0,出现以下错误消息:
Error CS0103 The name 'ConfigurationManager' does not exist in the current context (netcoreapp2.0)
Run Code Online (Sandbox Code Playgroud)
另外,我创建了一个新的.NET Core 2.0控制台应用程序(这次仅针对.NET Core 2.0),但同样ConfigurationManager在命名空间下似乎没有System.Configuration.
我很困惑,因为它可以在.NET Standard 2.0下使用,所以我希望它可以在.NET Core 2.0中使用,因为.NET Core 2.0符合.NET Standard 2.0.
我错过了什么?
Ale*_*éau 213
是的,ConfigurationManager.AppSettings在引用NuGet包之后可以在.NET Core 2.0中使用System.Configuration.ConfigurationManager.
积分转到@JeroenMostert给我解决方案.
Tom*_*kel 11
我System.Configuration.ConfigurationManager从Nuget 安装到.net core 2.2应用程序中。
然后我参考 using System.Configuration;
接下来,我改变了
WebConfigurationManager.AppSettings
to ..
ConfigurationManager.AppSettings
Run Code Online (Sandbox Code Playgroud)
到目前为止,我相信这是正确的。 4.5.0 is typical with .net core 2.2
我对此没有任何问题。
Nat*_*ugh 10
最新的一套指南如下:(来自https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables)
用:
System.Environment.GetEnvironmentVariable(name, 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);
}
}
Run Code Online (Sandbox Code Playgroud)
在本地开发和在 Azure 中运行时,都可以从环境变量中读取应用设置。在本地开发时,应用设置来自local.settings.json文件中的
Values集合。在本地和 Azure 两种环境中,都会检索命名应用设置的值。例如,当您在本地运行时,如果您的 local.settings.json 文件包含.GetEnvironmentVariable("<app setting name>"){ "Values": { "WEBSITE_SITE_NAME": "My Site Name" } }该System.Configuration.ConfigurationManager.AppSettings属性是用于获取应用程序的设定值的替代API,但我们建议您使用
GetEnvironmentVariable如下所示。
设置好程序包后,您需要创建一个app.config或web.config并添加如下内容:
<configuration>
<appSettings>
<add key="key" value="value"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64307 次 |
| 最近记录: |