简体   繁体   中英

ASP.NET Core Options Pattern

I'm trying to use the Options Pattern described here , but I'm wanting to be able to get something more complex than just key/value pairs.

Ideally, I'd like to be able to copy my appsettings.json using the Paste JSON as Classes , and then hand off the root class to services.Configure<T>(Configuration) and be done with it.

Here's what I've done:

appsettings.json:

{
  "Data": {
    "ConnectionA": {
      "ConnectionString": "string1"
    },
    "ConnectionB": {
      "ConnectionString": "string2"
    }
  }
}

corresponding classes

public class Data
{
    public Connectiona ConnectionA { get; set; }
    public Connectionb ConnectionB { get; set; }
}

public class Connectiona
{
    public string ConnectionString { get; set; }
}

public class Connectionb
{
    public string ConnectionString { get; set; }
}

startup.cs

services.Configure<Data>(Configuration);

This returns null objects for ConnectionA and ConnectionB though.

Any suggestions?

full startup.cs per request:

    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)
            .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions();
        services.Configure<Data>(Configuration);

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

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=User}/{action=Index}/{id?}");
        });
    }

Edit :

This works:

services.Configure<Data>(options => {
    options.Connectiona = new Connectiona();
    options.Connectiona = new Connectionb();
    options.Connectiona.ConnectionString = Configuration["Data:Connectiona:ConnectionString"];
    options.Connectionb.ConnectionString = Configuration["Data:Connectionb:ConnectionString"];
});

So it looks like I just can't use the services.Configure<Data>(Configuration) extension.

Try services.Configure<Data>(Configuration.GetSection("Data")); instead of services.Configure<Data>(Configuration);

try it like this:

{
  "Data": {
    "DataWarehouseConnection": {
      "ConnectionString": "yourconnectionstringhere"
    },
    "DefaultConnection ": {
      "ConnectionString": "yourconnectionstringhere"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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