DEV Community

Cover image for .Net Core: Multiple Environments
Fernando Sonego
Fernando Sonego

Posted on

.Net Core: Multiple Environments

One of the best features of .Net Core is the ability to work with multiple environments. We can use environments like development, test, and production. This configuration will allow us to modify the behavior according to the environment in which the application is running. The objective of this publication is to discuss the possible uses of this new feature together with good practices in application development.

Suppose we have 3 environments where we must run or load different configurations: Development, Staging, and Production. Asp.Net Core allows us to distinguish these environments by means of variables called “Environment Variable”. the variable we are referring to is called ASPNETCORE_ENVIROMENT. This is what will allow us to configure our environment. The most common values are Development, Staging, and Production.

Once this variable is configured, we can detect at runtime what the value of this variable is and in what environment we are running the application.

We must bear in mind that in Linux the name is case sensitive, for this reason, the files and configurations will be taken as case sensitive as a good practice.

Development

This environment is that we will use while we are developing our application. How do we configure it? we must right-click on our project, select configuration, a window will open and select from the left tab "Debug" as we can see in the next window.

Remember that when I modify this screen, the configuration is modified in the launchSessting.json file inside the properties folder. In this file we can include any environment that we want, for example, we can add several configuration profiles as we can see in the following code:

{
    "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
        "applicationUrl": "http://localhost:40088/",
        "sslPort": 0
    }
},
    "profiles": { 
        "IIS Express": {
            "commandName": "IISExpress",
            "launchBrowser": true,
        "environmentVariables":{ 
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    } 
},
    "IIS Express (Staging)": { 
        "commandName": "IISExpress",
        "launchBrowser": true,
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Staging"
        }
    }
}

If we change any of these settings in the file, the effects will not be taken automatically. We will have to restart the servers, especially Kestrel will need to restart, once it has been modified to be able to correctly detect the change.

Staging

This is the environment that we will use for a test or pre-production environment. Almost always for a final test before making a move to production. This environment should almost always be a production mirror to reduce deployment impact.

Production

This is the environment that is often called Alive. It is an environment where we configure security, performance, and reliability characteristics of the application. some of the characteristics that the production environment can have are:

  • Enable cache functions
  • Client-side file settings such as packages, shrinking js and css files or CDN (Content Delivery Network) settings
  • Disable diagnostic functions
  • Activate logging and monitoring settings.

The list can be longer depending on the needs of each application.

Setting up the environments

In Windows we can make use of the configuration by means of once our application is running:

Command line

set ASPNETCORE_ENVIRONMENT = »Development»

PowerShell

$ Env: ASPNETCORE_ENVIRONMENT = "Development"

These commands will only be validated while the window where it is being executed is open. If we close it we will lose this configuration. In case we want the value to be global we must configure it from Control Panel> System> Advanced System Configuration, in the Advanced Options tab we must add the ASPNETCORE_ENVIRONMENT variable as we see in the following screenshot:

Enviroments

In macOS we must use the following command from bash

ASPNETCORE_ENVIRONMENT = Development dotnet run

At the operating system or machine level, we must configure the variables in the .bashrc or .bash.profile files. we must edit the file and add the following:

export ASPNETCORE_ENVIRONMENT = Development

In Linux, we must use the export command from the console for the open session or modify the bash_profile file for the entire operating system or machine.

How to verify the environment at runtime

Asp.Net provides a service based on the IHostingEnviroment interface that is available through Dependency Injection in the main container. In the startup.cs file we can see how the env variable is injected, which will allow us to access the environment.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }else{
        app.UseExceptionHandler("/Home/Error");
    }
}

To check a specific environment while we are running the application, IHostingEnvironment provides the method IsEnviroment ("Environment name").

In the code we can see that it is asking if the environment is Development through the method IsDevelopment () which will configure some options for this environment. For example, we see that app.UseBrowserLink () is configured; which is a characteristic of Visual Studio that we will not use in production.

We also have the possibility to use these configurations through Tag Helper within the MVC Views. For example, we can tell you to use uncompressed css or js files in Development and in Staging and Production to use compressed files.

<environment include="Development">
    <div>The effective tag is: &lt;environment include="Development"&gt;</div>
</environment>
<environment exclude="Development">
    <div>The effective tag is: &lt;environment exclude="Development"&gt;</div>
</environment>
<environment include="Staging,Development,Staging_2">
    <div>
        The effective tag is:
        &lt;environment include="Staging,Development,Staging_2"&gt;
    </div>
</environment>

Conclusion

Asp.Net Core has a number of features that we as developers can take very good advantage of as well as great control over the environments in which we usually work. This configuration allows us to change the behavior of our application with a simple change of value in the environment variable.

Oldest comments (0)