In professional application development that is followed in most .NET Core web development firms, you will find three main phases i.e. development, staging, and production phase. These are the various phases where the application is properly tested before publishing it to the end-users. Sometimes depending on the phases, developers wish to have control over the application behavior. Fortunately, ASP.Net Core provides support for this feature by using Environment variables. The Environment variable is a kind of variable that specifies the runtime environment in which the application is running. In Asp.Net Core, an application’s behavior can be configured using an environment variable.
In ASP.Net Core, the ASPNETCORE_ENVIRONMENT variable is used to point-out the runtime environment.
Its value can be anything as per the requirement and its environment phase can be development, staging, or production. One important point to note here is that its value is not case sensitive in Windows and macOS but for Linux, it is case-sensitive.
How to set Environment Variables in Visual studio?
Open the project in visual studio, after that open the solution explorer window, right-click on the project, click on properties. You will see the window as shown in the below screenshot. Now click on the Debug tab. You can see the Environment Variable with name and value.
This is how we can set environment variables in Visual Studio. You can also alter the value as per your requirement. This value will get stored in the launchSettings.json file. You can observe this in the below reference. And yes, you can even alter the values directly in the JSON file as well.
How to access Environment variable at runtime?
We can easily fetch the environment value in the program to execute some other additional code based on an environment variable. Observe the below example. You can see IHostingEnvironment. In general words, it is a service embraced with the value of ASPNETCORE_ENVIRONMENT variable in EnvironmentName property.
Apart from that, .Net core also includes conditional extension functions such as IsEnvironment(), IsProduction(), IsStating(), IsDevelopement(). By noticing the below example, you will also get an idea about checking the environment variable in the configure() method.
Environment Variable updates in .NET Core 3.1
The following are the new variables added in .Net core 3.1:
- DOTNET_RESTORE_CONFIGFILE – This variable is used to restore the operations in NuGet.Config file, without combining with DOTNET_RESTORE_SOURCES.
- DOTNET_PUBLISH_READYTORUN – To compile the application early before time, we need to set this variable’s value to true. It benefits in decreasing the load of a JIT compiler while execution which eventually reduces startup time. By default, it would be set to False.
To control the build behavior of the web application, Dot Net Core 3.1 supports a wide range of Environment variables. Let us discuss briefly a few of them.
- DOTNET_STARTUP_PROJECT: It selects the project that needs to be executed.
- DOTNET_ASSEMBLY_NAME: It selects the Assembly that needs to be executed.
- DOTNET_RESTORE_SOURCES: This variable overrides all the sources identified in the nuget.config file.
- DOTNET_TOOLS: It defines the .Net tools that need to be installed before developing the application.
- DOTNET_NPM_TOOLS: It provides a list of NPM packages that need to be installed before developing the application.
- DOTNET_TEST_PROJECTS: It provides a test project list.
- DOTNET_CONFIGURATION: Executes the application project in debug mode or release mode. Debug and Release are the fixed values for this configuration.
- HTTP_PROXY, HTTPS_PROXY: This configures the proxies of HTTP and HTTPS used while building and running the app.
- DOTNET_RM_SRC: Once you set it to true, your source code will not get appended in the image.
- DOTNET_SSL_DIRS: By using this, a list of files or folders can be specified with trustworthy SSL certificates. These certificates shall be trusted by each and every process that runs while building process and all the processes which run in the image after the build process. This also includes the application that was built.
- ASPNETCORE_URLS: This variable is mandatorily set to http://*:8080 for configuration of .Net Core for using the port. One cannot change this port.
- DOTNET_RESTORE_DISABLE_PARALLEL: It restricts in restoring multiple projects in parallel when set to true. One of the benefits with this variable is – it lessens restore timeout errors.
- DOTNET_INCREMENTAL: By default, it will be in False. When you set it to true, the NuGet packages would be re-utilized for incremental builds.
- DOTNET_PACK: This command is used to pack code into a NuGet package. It builds the project and produces the ‘.nupkg’ file which is a NuGet package.
- NPM_MIRROR: This variable uses a custom NPM registry mirror to download packages during the build process.
- DOTNET_VERBOSITY: This variable is used to specify the verbosity of the dotnet build commands. If it is set, the environment variables are printed at the start of the build. These variables can have MS build verbosity values which includes q[uiet], m[inimal], n[ormal], d[etailed] and diag[nostic].
Set & Get Methods
These value can be created, modified or deleted by the SetEnvironmentVariable() method. It has two overloads.
- In the first overload, you can pass a variable name and value you want to set on that variable in the current process. A Null value can be passed to delete the set variable.
- Second overload takes care of creation, modification, and deletion of the environment variable that is saved in the current process or stored in the Windows operating system registry key which is reserved for the local machine or the current user.
The value of these variables can be accessed by method GetEnvironmentVariable(). This method has two overloads.
- First overload of this method takes the name of the variable as an argument and returns the value of that variable for the current process. This method either returns string or null.
- We can pass the name of the variable as well as the EnvironmentVariableTarget enum by which we can get the value of the environment variable from the current process or from the Windows operating system registry key for the current user or local machine. It returns the value of the environment variable specified by the variable and target parameters.
To sum up, .Net Core 3.1 has included a rich set of Environment variables that can help effortlessly to the ASP.Net core programmers while development. In fact, .Net Core is such a kind of environment where the more you go deep the more you will get learning.