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?

3
  • Hello, did you ever find a solution to this? I've got exactly the same issue. Commented Dec 7, 2016 at 8:54
  • Yes I did, but I change appsettings.json content to match an example that I see somewhere else. Commented Dec 8, 2016 at 15:45
  • See posted answer Commented Dec 8, 2016 at 15:56

3 Answers 3

72

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));
Sign up to request clarification or add additional context in comments.

5 Comments

This answer is helpful. Just a note, with ASP.NET Core 2.0, that additional NuGet is not required.
@ArghyaC - something must have changed in the library dependencies because I've found you do need the NuGet package. ASP.Net Core 2.1
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.
Got to this issue after upgrade of .NET Core from 2.2 to 3.1. This solved the problem for me.
I installed Install-Package Microsoft.Extensions.Options.ConfigurationExtensions -Version 3.1.2 and works perfectly
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.

1 Comment

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...
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.

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.