c# - How can I retrieve AppSettings configuration back in Asp.Net MVC 6? -
assuming using new depencyinjection framework configure classes , dependencies in new asp.net/vnext.
how can use, how can pre-defined configuration settings?
public void configureservices(iservicecollection services) { // add application settings services container. services.configure<appsettings>(configuration.getsubkey("appsettings")); // add ef services services container. services.addentityframework() .addsqlserver() .adddbcontext<applicationdbcontext>(options => options.usesqlserver(configuration["data:defaultconnection:connectionstring"])); // add identity services services container. services.addidentity<applicationuser, identityrole>() .addentityframeworkstores<applicationdbcontext>() .adddefaulttokenproviders(); // configure options authentication middleware. // can add options google, twitter , other middleware shown below. // 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 services container. services.addmvc(); services.addsingleton(a => { //appsettings settingsmodel = ?? //get configuration settings filled // technical artifice 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 following line add web api services makes easier port web api 2 controllers. //you need add microsoft.aspnet.mvc.webapicompatshim package 'dependencies' section of project.json. services.addwebapiconventions(); }
you can appsettings in fooservice injecting ioptions<appsettings>
di service in it's constructor.
the ioptions<>
interface part of called options model used accessing poco style settings(ex: appsettings) across application. calls services.configure<appsettings>(
, services.configure<facebookauthenticationoptions>(options =>
in above example, register di services in turn used di service called optionsmanager
when resolving requests ioptions<>
.
example:
public class fooservice { private readonly appsettings _settings; public fooservice(ioptions<appsettings> options) { _settings = options.options; } .... .... }
Comments
Post a Comment