2016-06-23 101 views
2

我試圖使用選項模式here,但我希望能夠得到比鍵/值對更復雜的東西。ASP.NET核心選項模式

理想情況下,我希望能夠使用Paste JSON as Classes複製我的appsettings.json,然後將根類移交到services.Configure<T>(Configuration)並完成它。

這裏是我做了什麼:

appsettings.json:

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

相應的類

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

雖然這會返回空對象ConnectionAConnectionB

有什麼建議嗎?

每個請求全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) 
      .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?}"); 
     }); 
    } 

編輯

這工作:

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"]; 
}); 

所以看起來我只是不能使用services.Configure<Data>(Configuration)延期。

+0

您的JSON的格式不正確。這只是一個錯誤的問題嗎? – Nkosi

+0

只有一個錯字,對不起。 – Charles

+0

你還包括'services.AddOptions();'第一次在您的設置 – Nkosi

回答

6

嘗試的services.Configure<Data>(Configuration.GetSection("Data"));代替services.Configure<Data>(Configuration);

0

嘗試這樣的:

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

這就是我所擁有的...我只是沒有從該文件複製名稱(因爲它有敏感信息),而且我從類文件中做了,我會更新示例。 – Charles