我有一个方法,读取设置从我的配置文件像这样:

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兼容的。

我错过了什么?


当前回答

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

感谢@JeroenMostert给我的解决方案。

其他回答

一旦你有包设置,你需要创建一个app.config或web。配置并添加如下内容:

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

最新的指南如下:(来自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.

我知道现在有点晚了,但也许有人正在寻找一种简单的方法来访问。net core app中的appsettings。 在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"];
    }
}

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

感谢@JeroenMostert给我的解决方案。

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

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

       ...
    }