ConfigurationManager.AppSettings是否在.NET Core 2.0中可用?

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给我解决方案.

  • 我有点困惑.这会将`.NET Framework 4.6`列为依赖项.这是否意味着我的`.NET Core`项目不再是纯粹的"核心"项目? (8认同)
  • 你能从你的配置文件中发布代码吗?我试图弄清楚如何/在哪里设置 .NET Core 2.0 中的全局变量 (3认同)
  • @AlexSanséau,我试图找出在哪里可以设置AppSettings的值。web.config或appsettings.json不起作用。您能否举一个在哪里设置AppSettings的示例?谢谢。 (2认同)
  • @JanDeutschl,它应该进入web.config或app.config的appSettings部分,就像处理传统的.NET项目一样。 (2认同)
  • 如何将 appsettings.json 应用程序设置加载到 ConfigurationManager 中?这个答案似乎令人困惑,因为它使用 ConfigurationManager 但仍然使用 Web.Config (这不是您应该在 ASP.NET Core 中使用的内容。) (2认同)

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

我对此没有任何问题。

  • 它对我不起作用(.net core 3.1)。如果访问它没有任何设置数据 (4认同)

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如下所示。


Sha*_*iro 7

设置好程序包后,您需要创建一个app.config或web.config并添加如下内容:

<configuration>
  <appSettings>
    <add key="key" value="value"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)