2015-06-03 20 views
7

Supponendo che sto usando il nuovo framework DepencyInjection per configurare le mie classi e dipendenze nel nuovo ASP.Net/vNext.Come posso recuperare la configurazione di AppSettings in Asp.Net MVC 6?

Come posso utilizzare, Come posso ottenere le mie impostazioni di configurazione predefinite?

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

Forse sono fraintendimento della domanda, ma non vedo il problema. Perché non passare semplicemente '' 'Configuration.GetSubKey (" AppSettings ")' '' nel FooService Constructor? –

+0

Perché c'è la classe 'AppSettings' creata di default nel progetto, è lì solo per avere la configurazione, e se uso Configuration.GetSubKey otterrò un oggetto' IConfiguration', il che significa che avrei bisogno di ottenere il valori di configurazione manualmente. –

+0

vedere qui per maggiori dettagli: https://neelbhatt40.wordpress.com/2015/12/15/getting-a-configuration-value-in-asp-net-5-vnext-and-mvc-6/ – Neel

risposta

9

È possibile ottenere AppSettings nel FooService iniettando servizio IOptions<AppSettings> DI dentro di costruttore.

L'interfaccia IOptions<> fa parte di qualcosa denominato Modello di opzioni che viene utilizzato per accedere alle impostazioni di stile POCO (ad esempio: AppSettings) attraverso l'applicazione. Le chiamate come services.Configure<AppSettings>( e services.Configure<FacebookAuthenticationOptions>(options => nell'esempio sopra riportato, registrano effettivamente servizi DI che a loro volta vengono utilizzati da un servizio DI chiamato OptionsManager durante la risoluzione delle richieste per IOptions<>.

Esempio:

public class FooService 
{ 
    private readonly AppSettings _settings; 

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

    .... 
    .... 
} 
+0

Ottenuto! Non ho idea che esista un'interfaccia IOptions. –

+1

Per '1.0.0-rc1' cambia' options.Options' a 'options.Value'. – meze

+0

Come utilizzare questo servizio nel mio codice? 'FooService fooService = new FooService()' – MichaelMao