Skip to main content

Define a custom environment in ASP.NET Core

One of the cool features of ASP.NET Core is, Hosting Environment Management. It makes life easy for the developers, while dealing with multiple environments. Previously, the developers have to build the application differently for each environment (Staging, UAT, Production) due to dependency on config file sections and the preprocessor directive applicable at compile time. It’s a time-consuming and painful process. ASP.NET Core takes a different approach and offers a new way to save developers from all this hassle.

ASP.NET Core offers an interface named IHostingEnvironment, allows you to programmatically retrieve the current environment so you can have an environment-specific behaviour. By default, ASP.NET Core has 3 environments Development, Staging, and Production. In real life projects, these 3 environments may not suffice as you may have a couple of other environments like QA, UAT etc.. This post shows how to define a custom environment in ASP.NET Core, other than these 3 default environments.

Define a custom environment in ASP.NET Core

IHostingEnvironment provides information about the web hosting environment an application is running in. ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT at application startup and stores that value in IHostingEnvironment.EnvironmentName. As this approach is based on an environment variable, the switching of environments happens at runtime and not at compile time like the old ASP.NET.

ASPNETCORE_ENVIRONMENT can be set to any value, but three values are supported by the framework: Development, Staging, and Production. If no value is set, it will default to Production. To set any value other than these default values, create a static class which creates extension methods on an IHostingEnvironment interface. Like,

public static class HostingEnvironmentExtensions
{
    public const string QAEnvironment = "QA";
    public const string UATEnvironment = "UAT";
    public const string TestEnvironment = "Test";

    public static bool IsQA(this IHostingEnvironment hostingEnvironment)
    {
        return hostingEnvironment.IsEnvironment(QAEnvironment);
    }

    public static bool IsUAT(this IHostingEnvironment hostingEnvironment)
    {
        return hostingEnvironment.IsEnvironment(UATEnvironment);
    }

    public static bool IsTest(this IHostingEnvironment hostingEnvironment)
    {
        return hostingEnvironment.IsEnvironment(TestEnvironment);
    }
}

The above static class adds QA, UAT and Test environment. Visual Studio intellisense also confirms it.

Define a custom environment in ASP.NET Core

You can set the value of ASPNETCORE_ENVIRONMENT to any of these and the app will behave accordingly. You can set the value of this variable via the command prompt, PowerShell, Visual Studio and via Windows control panel. Please refer this post to find out more about setting up ASPNETCORE_ENVIRONMENT.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *