Adventures on the edge

Learning new development technologies
    "When living on the bleeding edge - you sometimes have to bleed" -BillKrat

Configuring multiple environments with ASP.NET MVC Core 2

Source code available: HERE

With ASP.NET MVC Core 2, much of the configuration work is already done for us; it is configured for appsettings.{Environment}.json right out of the box.  Line 29 is all you’ll require for the Startup method.

config02

I added the following extension method (line 53 above) to simplify my ConfigureService code [being consistent with existing code]:

public static void AddAppSettings(this IServiceCollection services, IConfiguration configuration)
{
     var section = configuration.GetSection(nameof(AppSettings));
     services.Configure<AppSettings>(section);
}

My appsettings.development.json configuration file has a section “AppSettings” which contains one setting [as of this writing] it is “Environment”.   For consistency I created a AppSettings class that has a property name of Environment as required by the above “services.Configure<AppSettings>(section)” process; it will populate all AppSettings properties of the same name of the settings within the configuration file.

If you examine the Property page of your project you’ll find that the ASPNETCORE_ENVIRONMENT variable has already been set and defaults to “Development” – this will result in the appsettings.development.json file being used.   Since there are no other configuration files if you were to use any other name, e.g., “Production” it will default to the appsettings.json file.

config01

All that remains is to use constructor injection to retrieve the configuration settings.   With default usage [out-of-the-box] you can use the “IOptions<AppSettings> settings” parameter as shown on line 29 of the image below.  To retrieve an instance of AppSettings you’ll need to use the Value property of settings (reference line 33 below).   However, there is more information available [by default] that I want to have available to my application, so I also registered an “IAppSettings appSettings” (line 30) implementation that will provide all of the environment variables as well (see red arrow for AppSettings).  Note: I provide both methods for demonstration purposes only, I will only be using IAppSettings in my code.

config03

So if your only interested in the out-of-the-box usage, you are done here.  If you are interested in how to have the environment variables (in the case above 83 of them), please read on.

In the Startup class I not only modified ConfigureServices (as shown above) but also made an update to the Configure process as shown in bold below:

Config05

The UseAppsettings extension method (line 19 below) uses the service provider to obtain applicable services and then calls the AppSetting class Initialize method with an event argument that contains services.

Config04

The Initialize method above uses the event arguments (services) to retrieve and populate the ‘Values” property with the available settings.

Comments are closed