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.