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

问题描述 投票:71回答:4

我有一个从我的配置文件中读取设置的方法,如下所示:

var value = ConfigurationManager.AppSettings[key];

只针对.NET Standard 2.0时,它编译得很好。

现在我需要多个目标,所以我用以下内容更新了我的项目文件:

<TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks>

但现在,netcoreapp2.0的编译失败,出现以下错误消息:

Error   CS0103  The name 'ConfigurationManager' does not exist in the current context   (netcoreapp2.0)

另外,我创建了一个新的.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。

我错过了什么?

c# .net-core app-config .net-standard
4个回答
177
投票

是的,在引用NuGet包ConfigurationManager.AppSettings之后,System.Configuration.ConfigurationManager在.NET Core 2.0中可用。

积分转到@JeroenMostert给我解决方案。


7
投票

设置完软件包后,您需要创建app.config或web.config并添加如下内容:

<configuration>
  <appSettings>
    <add key="key" value="value"/>
  </appSettings>
</configuration>

5
投票

我将Nuget的System.Configuration.ConfigurationManager安装到我的.net core 2.2应用程序中。

然后我参考using System.Configuration;

接下来,我改变了

WebConfigurationManager.AppSettings

to ..

ConfigurationManager.AppSettings

到目前为止,我认为这是正确的。 4.5.0 is typical with .net core 2.2


3
投票

最新的指导如下:(来自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);
    }
}

在本地开发和在Azure中运行时,可以从环境变量中读取应用程序设置。在本地开发时,应用程序设置来自local.settings.json文件中的Values集合。在本地和Azure两种环境中,GetEnvironmentVariable("<app setting name>")都会检索指定应用程序设置的值。例如,当您在本地运行时,如果您的local.settings.json文件包含{ "Values": { "WEBSITE_SITE_NAME": "My Site Name" } },则会返回“我的站点名称”。

System.Configuration.ConfigurationManager.AppSettings属性是获取应用设置值的替代API,但我们建议您使用GetEnvironmentVariable,如此处所示。


0
投票

您可以使用“配置”来解决此问题。

Ex(Startup.cs):

在此实现之后,您可以通过DI将其传递给控制器​​。

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        Configuration = builder.Build();

    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        var microserviceName = Configuration["microserviceName"];

       services.AddSingleton(Configuration);

       ...
    }
© www.soinside.com 2019 - 2024. All rights reserved.