2015-06-03 16 views
7

Giả sử rằng tôi đang sử dụng khung DepencyInjection mới để định cấu hình các lớp và phụ thuộc của tôi trong ASP.Net/vNext mới.Làm thế nào tôi có thể lấy lại cấu hình AppSettings trong Asp.Net MVC 6?

Tôi làm cách nào để sử dụng, Làm cách nào để có được cài đặt cấu hình được xác định trước?

public void ConfigureServices(IServiceCollection services) 
    { 
     // Add Application settings to the services container. 
     services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings")); 

     // Add EF services to the services container. 
     services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<ApplicationDbContext>(options => 
       options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); 
     // Add Identity services to the services container. 
     services.AddIdentity<ApplicationUser, IdentityRole>() 
      .AddEntityFrameworkStores<ApplicationDbContext>() 
      .AddDefaultTokenProviders(); 

     // Configure the options for the authentication middleware. 
     // You can add options for Google, Twitter and other middleware as shown below. 
     // For more information see http://go.microsoft.com/fwlink/?LinkID=532715 
     services.Configure<FacebookAuthenticationOptions>(options => 
     { 
      options.AppId = Configuration["Authentication:Facebook:AppId"]; 
      options.AppSecret = Configuration["Authentication:Facebook:AppSecret"]; 
     }); 

     services.Configure<MicrosoftAccountAuthenticationOptions>(options => 
     { 
      options.ClientId = Configuration["Authentication:MicrosoftAccount:ClientId"]; 
      options.ClientSecret = Configuration["Authentication:MicrosoftAccount:ClientSecret"]; 
     }); 

     // Add MVC services to the services container. 
     services.AddMvc(); 

     services.AddSingleton(a => 
     { 
      //AppSettings settingsModel = ?? //GET CONFIGURATION SETTINGS FILLED 

      // TECHNICAL ARTIFICE TO RETRIEVE CURRENT SETTINGS 
      //var settingsModel = new AppSettings(); 
      //var config = Configuration.GetSubKey("AppSettings"); 
      //foreach (var item in typeof(AppSettings).GetProperties().Where(b => b.CanWrite)) 
      { 
       //item.SetValue(settingsModel, config.Get(item.Name)); 
      } 

      return new FooService(settingsModel); 
     }); 

     //Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers. 
     //You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json. 
     services.AddWebApiConventions(); 
    } 
+0

Có lẽ tôi hiểu lầm câu hỏi nhưng tôi không thấy vấn đề. Tại sao không chỉ truyền '' 'Configuration.GetSubKey (" AppSettings ")' '' vào trong FooService Constructor? –

+0

Vì có lớp 'AppSettings' được tạo mặc định trong dự án, nên chỉ có cấu hình và nếu tôi sử dụng Configuration.GetSubKey, tôi sẽ nhận được một đối tượng' IConfiguration', có nghĩa là tôi sẽ cần giá trị cấu hình theo cách thủ công. –

+0

xem tại đây để biết thêm chi tiết: https://neelbhatt40.wordpress.com/2015/12/15/getting-a-configuration-value-in-asp-net-5-vnext-and-mvc-6/ – Neel

Trả lời

9

Bạn có thể nhận AppSettings trong FooService của bạn bằng cách tiêm dịch vụ IOptions<AppSettings> DI trong hàm tạo của nó.

Giao diện IOptions<> là một phần của một cái gì đó được gọi là Mô hình tùy chọn được sử dụng để truy cập cài đặt kiểu POCO (ví dụ: AppSettings) của bạn trên ứng dụng của bạn. Các cuộc gọi như services.Configure<AppSettings>(services.Configure<FacebookAuthenticationOptions>(options => trong ví dụ trên của bạn, thực sự đăng ký dịch vụ DI mà lần lượt được sử dụng bởi dịch vụ DI được gọi là OptionsManager khi giải quyết yêu cầu cho IOptions<>.

Ví dụ:

public class FooService 
{ 
    private readonly AppSettings _settings; 

    public FooService(IOptions<AppSettings> options) 
    { 
     _settings = options.Options; 
    } 

    .... 
    .... 
} 
+0

OK! Tôi không có ý tưởng rằng tồn tại một giao diện IOptions .. –

+1

Thay đổi '1.0.0-rc1'' options.Options' thành 'options.Value'. – meze

+0

Cách sử dụng dịch vụ này trong mã của tôi? 'FooService fooService = new FooService()' – MichaelMao

Các vấn đề liên quan