ASP.NET Core 1.0 multiple configurations without using environment variables

This article shows how to use multiple Json configuration files in ASP.NET Core 1.0 which can be used in CI for automatic deployment without requiring environment variables.

Code: https://github.com/damienbod/AspNet5Configuration

2016.07.01: Updated to ASP.NET Core 1.0 RTM
2016.05.17: Updated to ASP.NET Core 1.0 RC2 dotnet
2015.11.18: Updated to ASP.NET Core 1.0 RC1
2015.10.16: Updated to ASP.NET Core 1.0 beta8

In ASP.NET Core, Json, XML and ini configuration files are supported per default. It is possible to create strongly type classes from the configuration files. It is also possible to load different configuration files depending on a run-time variable. An environment variable can be used for this which most of the examples or existing blogs demonstrates. Sometimes it is not possible to use an environment variable due to deployment requirements or restrictions. This example shows how to setup multiple configurations which can be used in automatic deployments for different target systems without using environment variables.

Application Global Configuration

A global application config file is used to set the application staging environment. The values in this file would typically be set from the installer, for example using a WIX silent installer with utils configuration.

{
     "StagingEnvironment ": "production"
}

This can then be used in the Startup.cs class in the constructor. The file is loaded and the StagingEnvironment value can now be used.

var globalbuilder = new ConfigurationBuilder()
                  .SetBasePath(env.ContentRootPath)
                         .AddJsonFile("globalconfig.json");
var globalConfiguration = globalbuilder.Build();

Application Configuration

The application configuration contains all the normal configurations which are required for the application. You can add many sections to the file, or a configuration class per file, or mix it as you require. I usually try to have just one file for all configuration objects when possible.

{
    "ApplicationConfiguration": {
        "MinimumLevel": 1,
        "AboutMessage": "This is the default configuration"
    }
}

The configuration section will be loaded into the ApplicationConfiguration class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AspNet5Configuration.Configurations
{
    public class ApplicationConfiguration
    {
        public int MinimumLevel { get; set; }
        public string AboutMessage { get; set; }
    }
}

Now the application configuration can be loaded. The stagingEnvironment property loaded from the global configuration is used to load the correct configuration for the required staging target. This is optional, so if nothing is set or found, the default configuration file is used.

string stagingEnvironment = globalConfiguration["StagingEnvironment"];

var builder = new ConfigurationBuilder()
     .SetBasePath(env.ContentRootPath)
   .AddJsonFile("config.json")
   .AddJsonFile($"config.{stagingEnvironment}.json", optional: true);
Configuration = builder.Build();

The configuration class can then be added the the services. In ASP.NET Core RC2, the package Microsoft.Extensions.Options.ConfigurationExtensions is required.

public void ConfigureServices(IServiceCollection services)
{
	services.Configure<ApplicationConfiguration>(
         Configuration.GetSection("ApplicationConfiguration"));
	services.AddMvc();
}

Using the Configuration

Now the configuration can be used. This is added in the constructor of the controller or the class where it is required. You cannot use the class directly, but by using the IOptions. The following example just displays the configuration value in the about HTTP Request method.

using System.Collections.Generic;
using AspNet5Configuration.Configurations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace AspNet5Configuration.Controllers
{
    [Route("api/[controller]")]
    public class AboutController : Controller
    {
        private IOptions<ApplicationConfiguration> _optionsApplicationConfiguration;
        public AboutController(IOptions<ApplicationConfiguration> o)
        {
            _optionsApplicationConfiguration = o;
        }

        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { _optionsApplicationConfiguration.Value.AboutMessage };
        }
    }
}

Testing the Configurations

The configuration can be viewed by requesting the api/about URL. This shows the used configuration.

Testing with “StagingEnvironment”: “production”

aspnet5_config_api_about_03

Testing with “StagingEnvironment”: “test”

aspnet5_config_api_about_02

Testing with “StagingEnvironment”: “unknown”

aspnet5_config_api_about_01

With this, it is possible to automatically deploy the application without using any environment variables and using different configurations for development, test, production or with different target systems.

Links:

Configuration in ASP.NET 5

http://www.mikesdotnetting.com/article/284/asp-net-5-configuration

http://gunnarpeipman.com/2014/11/asp-net-5-new-configuration-files-and-containers/

https://weblog.west-wind.com/posts/2015/Jun/03/Strongly-typed-AppSettings-Configuration-in-ASPNET-5

https://github.com/aspnet/Configuration

Introduction to ASP.NET 5, microsoft virtual academy

3 comments

  1. […] ASP.NET 5 multiple configurations without using environment variables – damienbod takes a look at using different configuration files in your ASP.NET 5 Applications while avoiding the need for environmental variables […]

  2. […] Utiliser la possibilité d’avoir des environnements multiple avec ASP.NET 5. […]

  3. […] ASP.NET Core 1.0 multiple configurations without using environment variables […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: