You are currently viewing Multiple Ways To Set Hosting Environment In .NET Applications

Multiple Ways To Set Hosting Environment In .NET Applications

As we all know, .NET 5 has been released few months back and now .NET 6 previews are also available. If you know this, then it is not a news for you that .NET Framework will not have any major version release and .NET basically is the new name for .NET Core.

The .NET Core web application uses environment variables to identify the logical environment for the running application. This article explains which environment variable is used for this purpose and what are various options to set that environment variable.

Environment Identifier in .NET (core)

The .NET 5 is now the latest version of .NET Core (hereafter referred to as .NET). And it uses JSON configuration file, appsettings.json. The ASPNETCORE_ENVIRONMENT is the environment variable which .NET uses to determine the name of environment.

Just to add up to the complications, there is one more environment variable DOTNET_ENVIRONMENT. This also serves the same purpose. If both environment variables are defined, then ASPNETCORE_ENVIRONMENT takes priority and it overrides the DOTNET_ENVIRONMENT value.

By default, 3 values and methods are provided by framework for ASPNETCORE_ENVIRONMENT:

But if you have more than 3 environments or the environment names are different, you can set the environment variable value to any string value(e.g. Test, Dev, Performance, Security, Load, etc.)

In order to check if the code is running in your environment, you can read the value of environment variable and check if it is the expected value.

How to set it ?

There are multiple ways to setup the environment variables. The environment variable can be setup either on machine level, so it would be applicable to all processes running on that machine OR it can be setup only on process level (or user level or session level).

Below are some ways to set the environment.

Local Debugging – launchsettings.json

When running application locally, you can use launchsettings.json file (in Properties folder), to set the environment variables. You can define multiple profiles in the JSON file as shown below:

You can also create the profiles and set the active profile by right clicking on the .NET Core web application project and then selecting Properties option from the context menu.

Set ASPNETCORE_ENVIRONMENT values from project properties

Command Line Tools

Windows command prompt or PowerShell command line can be used to set the ASPNETCORE_ENVIRONMENT as shown below.

You can check docs for further details.

Windows Control Panel

Search for Edit the system environment variables in the start menu and you should be able to see the option as shown below.

Control Panel Option to edit the system environment variables

Then, click on Environment Variables button to change the environment variable for either user or for all users on the machine.

Control Panel system properties to edit environment variables

IIS App Pools – applicationHost.config

If you wish to deploy the web app in IIS, then you can set the environment variables on application pool level. If you go to applicationHost.config, it will have application pool specific XML configuration section. That section can contain list of environment variables as shown in below snapshot.

If you set the environment variable using this method, it would set the variable to be on process level. Meaning, other app pool can have different value for this environment variable.

I feel the disadvantage with this approach is – the file is not associated with application, but it is associated with whole IIS instance. So, if you also do not want to go with this approach and using IIS for hosing .NET Core app, then next option is probably better for you.

You can setup some scripts to automatically update the file. Alternatively you can also use IIS Manager UI to set the environment variables on the app pool.

Web.Config for IIS based deployments

If the .NET Core web application is being deployed in IIS, then the environment can also be set in web.config as shown below. This config file should be present at root of the application.

Azure Web Apps Environment

If you are using Azure App Service for deployed web app, then navigate to the App Service and then select Configuration under Settings.

Then under Application settings (the first tab), you can add a New application setting with name ASPNETCORE_ENVIRONMENT and set it to the value you want. Then hit on Save to persist the changes and restart the app service.

Azure App Service: Provide aspnetcore_environment value

Code

The default ASP.NET Core web app templates call ConfigureWebHostDefaults, which uses ASPNETCORE_ENVIRONMENT variable. You can create your own method to configure the web host, and that code can set the environment variable.

There are multiple options for setting environment from code. You can either read it from some other file like json file or XML file or environment variables or command line.

In below code sample, the environment is being set by using UseEnvironment method. The value from command line is used to set the environment value.

NOTE

Note that the values set on system level are overridden by the values set in the launchSettings.json file. So, if you want to use the the values from system level or user level environment variables set by using command line or any other option, you need to specify --no-launch-profile switch in dotnet run command.

I have tried to cover some of of the ways that you would want to use for setting ASPNETCORE_ENVIRONMENT in real world projects. If I missed anything, please comment here to let me know which option you used for setting the environment variable.

Leave a Reply

This Post Has 2 Comments