2015-06-03 11 views
7

Zakładając, że używam nowej struktury DepencyInjection do skonfigurowania moich klas i zależności w nowej ASP.Net/vNext.Jak mogę odzyskać konfigurację AppSettings w Asp.Net MVC 6?

Jak używać, Jak uzyskać wstępnie zdefiniowane ustawienia konfiguracyjne?

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

Może jestem nieporozumienie pytanie, ale nie widzę problemu. Dlaczego po prostu nie przekazać '' 'Configuration.GetSubKey (" AppSettings ")' '' do konstruktora FooService? –

+0

Ponieważ istnieje klasa 'AppSettings' utworzona domyślnie w projekcie, jest tam po prostu konfiguracja, a jeśli użyję Configuration.GetSubKey, otrzymam obiekt' IConfiguration', co oznacza, że ​​potrzebuję uzyskać wartości konfiguracyjne ręcznie. –

+0

zobacz tutaj, aby uzyskać więcej informacji: https://neelbhatt40.wordpress.com/2015/12/15/getting-a-configuration-value-in-asp-net-5-vnext-and-mvc-6/ – Neel

Odpowiedz

9

Można uzyskać AppSettings w swojej FooService wstrzykując usługę IOptions<AppSettings> DI w jego konstruktora.

Interfejs IOptions<> jest częścią czegoś o nazwie Model opcji, który służy do uzyskiwania dostępu do ustawień stylu POCO (np. Ustawień aplikacji) w aplikacji. W powyższych przykładach połączenia, takie jak services.Configure<AppSettings>( i services.Configure<FacebookAuthenticationOptions>(options =>, faktycznie rejestrują usługi DI, które z kolei są używane przez usługę DI o nazwie OptionsManager podczas rozpatrywania żądań dla IOptions<>.

Przykład:

public class FooService 
{ 
    private readonly AppSettings _settings; 

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

    .... 
    .... 
} 
+0

Gotowy! Nie mam pojęcia, że ​​istnieje interfejs IOptions. –

+1

Dla '1.0.0-rc1' zmień' options.Options' na 'options.Value'. – meze

+0

Jak korzystać z tej usługi w moim kodzie? 'FooService fooService = new FooService()' – MichaelMao

Powiązane problemy