Asp.Net core how can I replace the Configuration Manager

user1591668 picture user1591668 · May 25, 2016 · Viewed 10.1k times · Source

I am new to ASP.NET Core RC2 and I was wondering how can I get some configuration settings and apply it to my method. For Instance in my appsettings.jsonI have this specific setting

"ConnectionStrings": {
    "DefaultConnection": 
        "Server=localhost;User Id=postgres;port=5432;Password=castro666;Database=dbname;"
  }

In my Controller every time I want to query the database I have to use this setup

 using (var conn = 
     new NpgsqlConnection(
         "Server=localhost;User Id=postgres;port=5432;Password=castro666;Database=dbname;"))
 {
     conn.Open();
 }

The obvious pitfull here is that if I want to add more to the configuration I have to change every single instance of that method. My question is how can I get the DefaultConnection in the appsettings.json so that I can do something like this

 using (var conn = 
     new NpgsqlConnection(
         ConfigurationManager["DefaultConnection"))
 {
     conn.Open();
 }

Answer

David Pine picture David Pine · May 25, 2016

In ASP.NET Core there are a lot of options you can use to access the configuration. It seems like if you're interesting it accessing the DefaultConnection you'd be better off using the DI approach. In order to ensure that you can use constructor dependency injection we have to correctly configure a few things in our Startup.cs.

public IConfigurationRoot Configuration { get; }

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();

    Configuration = builder.Build();
}

We have now read our configuration JSON from the builder and assigned it to our Configuration instance. Now, we'll need to configure it for dependency injection - so let's start by creating a simple POCO to hold the connection string.

public class ConnectionStrings
{
    public string DefaultConnection { get; set; }
}

We are implementing the "Options Pattern" where we bind strongly-typed classes to configuration segments. Now, in ConfigureServices do this:

public void ConfigureServices(IServiceCollection services)
{
    // Setup options with DI
    services.AddOptions();

    // Configure ConnectionStrings using config
    services.Configure<ConnectionStrings>(Configuration);
}

Now that this is all in place, we can simply require the constructor of the class to take on the IOptions<ConnectionStrings> and we'll be given a materialized instance of the class containing the configuration values.

public class MyController : Controller
{
    private readonly ConnectionStrings _connectionStrings;

    public MyController(IOptions<ConnectionString> options)
    {
        _connectionStrings = options.Value;
    }

    public IActionResult Get()
    {
        // Use the _connectionStrings instance now...
        using (var conn = new NpgsqlConnection(_connectionStrings.DefaultConnection))
        {
            conn.Open();
            // Omitted for brevity...
        }
    }
}

Here is the official documentation which I always suggest as a must read.