since there's no configurationmanager class in .net core, have set config in appsettings.json instead web.config
according this blog post have set configuration there, did this:
{ "logging": { "includescopes": false, "loglevel": { "default": "debug", "system": "information", "microsoft": "information" } }, "conexion": { "name" : "empresas", "connectionstring": "data source=empresas;initial catalog=catalma; integrated security=true;", "providername": "system.data.sqlclient" } }
i writed "conexion".
now created in viewmodels folder following class:
public class conexionconfig { public string name { get; set; } public string connectionstring { get; set; } public string providername { get; set; } }
now, in startup.cs, in configureservices method, have add by:
public void configureservices(iservicecollection services) { // add framework services. services.configure<conexionconfig>(configuration.getsection("conexion")); services.addmvc(); }
but unfortunately, following error:
argument 2: cannot convert 'microsoft.extensions.configuration.iconfigurationsection' 'system.action<conexionconfig>'
what missing?
firstly, need add following nuget package asp core project.
microsoft.extensions.options.configurationextensions
the extension methods contained in package allow configure typed configuration way wanted it.
services.configure<conexionconfig>(configuration.getsection("conexion"));
alternatively, use binder directly answer in thread suggests without important previous package rather:
microsoft.extensions.configuration.binder
this means have explicitly enable options on pipeline, , bind action. is:
services.addoptions(); services.configure<conexionconfig>(x => configuration.getsection("conexion").bind(x));
Comments
Post a Comment