How to restore AppSettings configuration in Asp.Net MVC 6?

Assuming I'm using the new DepencyInjection framework to configure my classes and dependencies in the new ASP.Net/vNext.

How can I use, How can I get predefined configuration settings?

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(); } 
+7
c # dependency-injection asp.net-core-mvc
source share
1 answer

You can get AppSettings in your FooService by inserting the IOptions<AppSettings> DI service into its constructor.

The IOptions<> interface is part of something called the “Parameter Model”, which is used to access the POCO style settings (like your AppSettings) in the application. Calls such as services.Configure<AppSettings>( and services.Configure<FacebookAuthenticationOptions>(options => in the example above) actually register DI services, which in turn are used by the DI service called OptionsManager when resolving requests for IOptions<> .

Example:

 public class FooService { private readonly AppSettings _settings; public FooService(IOptions<AppSettings> options) { _settings = options.Options; } .... .... } 
+9
source share

All Articles