简体   繁体   English

在ASP.NET Core中访问配置选项:选项模式

[英]Accessing configuration options in ASP.NET Core: options pattern

I have an ASP.NET Core application. 我有一个ASP.NET Core应用程序。 I try to use the options pattern but it does not seem to work. 我尝试使用选项模式,但它似乎不起作用。

I have the following appsettings.json : 我有以下appsettings.json

{
    "ConnectionStrings": {
        "MyTablesConnectionString": "Default[...];EndpointSuffix=core.windows.net"
    },

    "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" }  }
}

The following ConnectionStrings class 以下ConnectionStrings

public class ConnectionStrings {
    public ConnectionStrings()  {
        MyTablesConnectionString = "default value";
    }

    public string MyTablesConnectionString { get; set; }
}

And here is my Startup.cs 这是我的Startup.cs

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();

    // HERE IS THE GOOD VALUE !!!!!!!!
    Debug.WriteLine($"Connection string is:{Configuration["ConnectionStrings:MyTablesConnectionString"]}");
}

public void ConfigureServices(IServiceCollection services)
{
    // Adds services required for using options.
    services.AddOptions();
    // Register the IConfiguration instance which "ConnectionStrings" binds against.
    services.Configure<ConnectionStrings>(Configuration);

    // Add framework services.
    services.AddMvc();            
}

However, when using it on the dev machine with the following controller 但是,在具有以下控制器的开发机上使用时

private readonly ConnectionStrings azureConnectionString;
public HelloWorldController(IOptions<ConnectionStrings> optionsAccessor)
{
    // HERE IS THE "DEFAULT" VALUE ?!!!!!!!!
    azureConnectionString = optionsAccessor.Value;            
}

I see that the "default value" instead of the value from the json file (invalid one) is used. 我看到使用的是“默认值”,而不是json文件中的值(无效的值)。 Am I missing something? 我想念什么吗?

You need to bind the ConnectionStrings section, like so: 您需要绑定ConnectionStrings部分,如下所示:

services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));

At the moment it is expecting to find a property "ConnectionStrings" or "Logging" from your class, since you are binding the root. 目前,由于绑定了根,因此期望从类中找到属性“ ConnectionStrings”或“ Logging”。

Alternatively, configure all options automatically 或者, 自动配置所有选项

PM> Install-Package Register.Options.Asp.Net.Core

Startup.cs Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.ConfigureOptionsFromAssemblies(configuration, new List<Assembly>()
    {
        typeof(ConnectionStrings).Assembly
    });

   ...
}

Disclaimer author of package 包装的免责声明作者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM