ConfigurationManager.AppSettings 在 .NET Core 2.0 中可用吗?

Is ConfigurationManager.AppSettings available in .NET Core 2.0?

我有一种方法可以像这样从我的配置文件中读取设置:

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),但同样在命名空间System.Configuration下似乎没有ConfigurationManager

我很困惑,因为它在 .NET Standard 2.0 下可用,所以我希望它在 .NET Core 2.0 中可用,因为 .NET Core 2.0 与 .NET Standard 2.0 兼容。

我错过了什么?

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

感谢@JeroenMostert 为我提供了解决方案。

安装包后,您需要创建 app.config 或 web.config 并添加如下内容:

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

我从 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

我对此没有任何问题。

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

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.

您可以使用配置来解决这个问题。

例如 (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);

       ...
    }

我知道有点晚了,但也许有人正在寻找访问 .net 核心应用程序中的应用程序设置的简便方法。 在 API 构造函数中添加以下内容:

public class TargetClassController : ControllerBase
{
    private readonly IConfiguration _config;

    public TargetClassController(IConfiguration config)
    {
        _config = config;
    }

    [HttpGet("{id:int}")]
    public async Task<ActionResult<DTOResponse>> Get(int id)
    {
        var config = _config["YourKeySection:key"];
    }
}