29

Since there's no ConfigurationManager class in .NET Core, now I have to set config in appsettings.json instead web.config

According to this blog post I have to set my configuration there, so I did like 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 just writed that "Conexion".

Now I created in a ViewModels folder the 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 the ConfigureServices method, I have to add it by:

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?

Mr_LinDowsMac
  • 2,644
  • 9
  • 56
  • 75

3 Answers3

71

Firstly, you need to add the following nuget package to your ASP Core Project.

Microsoft.Extensions.Options.ConfigurationExtensions

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

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

Alternatively, you could use the binder directly like another answer in this thread suggests without important the previous package but rather:

Microsoft.Extensions.Configuration.Binder

This means that you would have to explicitly enable the options on the pipeline, and bind the action. That is:

services.AddOptions();
services.Configure<ConexionConfig>(x => Configuration.GetSection("Conexion").Bind(x));
Sarel Esterhuizen
  • 1,628
  • 17
  • 18
  • This answer is helpful. Just a note, with **ASP.NET Core 2.0**, that additional NuGet is not required. – Arghya C Feb 10 '18 at 19:05
  • 2
    @ArghyaC - something must have changed in the library dependencies because I've found **you do need** the NuGet package. **ASP.Net Core 2.1** – Jeremy Thompson Apr 10 '19 at 04:09
  • This should be marked as the correct answer because this worked for me. I'm using .NET Core 2.2. Used the first answer not the alternative. – Gene S Aug 12 '19 at 20:14
  • 1
    Got to this issue after upgrade of .NET Core from 2.2 to 3.1. This solved the problem for me. – smyk Feb 04 '20 at 15:51
  • 1
    I installed Install-Package Microsoft.Extensions.Options.ConfigurationExtensions -Version 3.1.2 and works perfectly – Ciprian Jijie Mar 06 '20 at 13:29
8

Try installing the nuget package Microsoft.Extensions.Configuration.Binder and use it´s Bind method:

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

You also have to install the options package Microsoft.Extensions.Options and add support for it if you want to inject your options class:

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

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

peco
  • 3,890
  • 1
  • 20
  • 27
  • This would work you are right, but if one wanted to configure it in the same way many tutorials illustrate the problem then one only needs to add the correct package. See my answer... – Sarel Esterhuizen Dec 29 '16 at 10:29
0

I based on a example somewhere else. Changed appsettings.json to something 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;"
    }
  }
}

ConexionConfig class 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"));
        }
...

Is important to include using Microsoft.Extensions.Configuration in this file.

Mr_LinDowsMac
  • 2,644
  • 9
  • 56
  • 75
  • I think the only difference between your original question and this answer is that you added the correct package along the way. See my answer... – Sarel Esterhuizen Dec 29 '16 at 10:28