Unable to configure configuration from JSON application settings file in .NET Core project

Since there is no ConfigurationManager class in .NET Core, now I need to set config in appsettings.json instead of web.config

According to this on the blog, I have to set my configuration there, so I liked it:

{ "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 just wrote this "Conexion".

Now I created the following class in the ViewModels folder:

 public class ConexionConfig { public string name { get; set; } public string connectionString { get; set; } public string providerName { get; set; } } 

Now, in Startup.cs, in the ConfigureServices method, I have to add it:

 public void ConfigureServices(IServiceCollection services) { // Add framework services. services.Configure<ConexionConfig>(Configuration.GetSection("Conexion")); services.AddMvc(); } 

But unfortunately, I get the following error:

 Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationSection' to 'System.Action<ConexionConfig>' 

What am I missing?

+7
asp.net-mvc asp.net-web-api
source share
3 answers

First, you need to add the following nuget package to your ASP Core project.

 Microsoft.Extensions.Options.ConfigurationExtensions 

The extension methods contained in the package allow you to configure a strongly typed configuration the way you wanted to do it.

 services.Configure<ConexionConfig>(Configuration.GetSection("Conexion")); 

Alternatively, you can use the binder directly, as another answer in this thread, offering without the important previous package, but rather:

 Microsoft.Extensions.Configuration.Binder 

This means that you will need to explicitly enable the parameters in the pipeline and bind the action. I.e:

 services.AddOptions(); services.Configure<ConexionConfig>(x => Configuration.GetSection("Conexion").Bind(x)); 
+9
source share

Try installing the nuget package Microsoft.Extensions.Configuration.Binder and use its Bind method:

  services.Configure<ConexionConfig>(x => Configuration.GetSection("Conexion").Bind(x)); 

You also need to install the Microsoft.Extensions.Options option package and add support for it if you want to enter your parameter class:

 public void ConfigureServices(IServiceCollection services) { services.AddOptions(); //.. } 

Now you can enter IOptions<ConexionConfig> in your controllers and views.

+3
source share

I was based on an example somewhere else. Changed appsettings.json like this:

 { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } }, "Data": { "DefaultConnection": { "ConnectionString": "Data Source=myserver\\sql08;Initial Catalog=enterprises;User id=userAPP;Password=mypassword;" } } } 

The ConexionConfig class has changed to this:

  public class ConexionConfig { public string ConnectionString { get; set; } } } 

Then in Startup.cs

 ... public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.Configure<ConexionConfig>(Configuration.GetSection("Data:DefaultConnection")); } ... 

It is important to include using Microsoft.Extensions.Configuration in this file.

0
source share

All Articles