ASP.NET 5 - Environment specific pipeline configuration

Introduction

In ASP.NET 5, we configure and add middlewares (aka pipelines) using Startup class. The convention is similar to Owin/Katana but not the same.

The Configuration method in Owin/Katana Startup class is replaced by Configure method and the parameter type is IApplicationBuilder rather than IAppBuilder and how we create and add middleware is also different to some extend. 

ASP.NET 5 Startup class allows us to add and configure services using ConfigureService method, this is not available in Owin/Katana Startup class. The services we add using this method is added to the DependencyInjection container which comes with ASP.NET 5 out of the box.

Environment awareness

The Startup class can access the hosting environment details using IHostingEnvironment. When we specify IHostingEnvironment as a dependency by making it as a parameter in Startup constructor, ConfigureServices or Configure method, the dnx will populate it for us and DependencyInjection framework will inject the instance.

The injected instance has EnvironmentName property, the value of this property is set from an environment variable ASPNET_ENV. If this variable is not found then the value is set as Development.

The instance also has a handy method IsEnvironment to find out whether the code is running on a specific environment or not. We can use this to do things differently based on the environment where the code is running. For example, we can read different config files as shown below

    // Example
    public class Startup
    {
        private readonly IHostingEnvironment _env;

        public Startup(IHostingEnvironment env)
        {
            _env = env;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            if (_env.IsEnvironment("Production"))
            {
                // read prod config
            }
            else
            {
                // read dev config
            }
        }

    .
    .
    .

   }

If we want to add/configure middlewares differently based on the environment then we can use IsEnvironment method just like how it is used in ConfigureServices method or we can use environment specific Configure methods. 

The environment specific Configure method only support Development, Staging and Production environment names and the method names have to be ConfigureDevelopment, ConfigureStaging and ConfigureProduction respectively for this to work. Here is an example of how we can use this,

// Full example
    public class Startup
    {
        private readonly IHostingEnvironment _env;

        public Startup(IHostingEnvironment env)
        {
            _env = env;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            if (_env.IsEnvironment("Production"))
            {
                // read prod config
            }
            else if (_env.IsEnvironment("Staging"))
            {
                // read staging config
            }
            else
            {
                // read dev config
            }
        }

        public void ConfigureDevelopment(IApplicationBuilder builder, IHostingEnvironment env)
        {
            // Development configuration
            Configure(builder);
        }

        public void ConfigureStaging(IApplicationBuilder builder)
        {
            // Staging configuration
            Configure(builder);
        }

        public void ConfigureProduction(IApplicationBuilder builder)
        {
            // Production configuration
            Configure(builder);
        }

        public void Configure(IApplicationBuilder builder)
        {
            // Common
        }
    }

This allows us to keep environment specific configurations/pipelines in a better way. 

comments powered by Disqus