Setting Up For Managing Production and Development Environment In ASP.NET Core

Setting Up For Managing Production and Development Environment In ASP.NET Core

In today’s world of advanced technologies, building web applications has become important for organizations. There are many languages that the develo

In today’s world of advanced technologies, building web applications has become important for organizations. There are many languages that the developers use to develop such web applications and ASP .NET by Microsoft is one of those. It has been a very popular among the developer fraternity from past many years.

While making a web application, one needs to focus on the three environments through which an application goes. These are development, staging and production. Each environment needs to be configured the application separately. To manage this, ASP .NET provides the user with three tools; each for one stage. One tool is used to manage profiles, another tool is used at the coarse-grained level while the last tool is used at the fine-grained level.

Managing profile

This process is fundamentally driven by value in the ASP .NETCORE_ENVIRONMENT environment variable. This value cannot be set by the user in the application code and the only way to set the value is through control panel->System->advanced system settings->environment variable from PowerShell or by command prompt. The only way to set this on any software, which is being used for .net coding is through the launchsettings.json file. This file organizes settings into profiles with each profile triggered with the way application is started.

One can find this file nested under the properties node in the solution explorer. The easiest way to manage this file is through the properties dialogue of the project. The debug tab can be used to see all the profiles in the launch settings through a UI offered by it. Here the UI allows the user to update as well as create profiles.

In Visual Studio 2019, the user has been provided with 4 default profiles that are set up with ASP .NET core projects. Out of these profiles, only two profiles are needed by the user to manage the environment variable. These profiles are “IIS express” and the other is named after the project name. Both of the profiles set the variable to “development” while the default setting of the environment variable is production.

Coarse-Grained Configuration

.NET core uses the environment variable to determine which appsettings.json file and the Startups.cs file are going to be used. For instance, if the environment variable is set to development, then the core uses the file appsettings.development.json file initially and then uses the value of appsettings.json file. This is only used when the first file is not found. It also looks for a class called Startup Development to configure the project when the environment variable is set to develop.

The only problem a user might face with this feature is that it isn’t turned on by default and he/she needs to turn it on before using this feature of .NET. To do this, one needs to modify the program.cs file, where one needs to modify the way the Create WebHostBuilder method calls UseStartup method. One needs to change the general version of use startup method to the one which accepts the name of the project.

Fine-Grained File

On the project, if one wants to perform more fine-grained checking, then the user needs to call the method on IHostingEnviornment object. He can request the object in any method called by ASP .NET core. A specific code is placed in the env property of the program where the object is required, so that in future when an action method requires it, then it can retrieve it from the program’s env property.

The object supports two methods, which return true or false value based on the environment variable. These methods are is Production, isStaging methods. These methods are generally used by action methods to either login or logoff. These methods are driven by environment variable being set to either of the two states. The method for the state is going to return true if the environment variable is set to that value. Otherwise, for other sets of variables, it is going to return false.

If the user wants to use another value in the environment, then he/she set the value of the environment variable, the one that he/she is willing to use in the app settings files and startup classes. The .NET core will use that value. With IHostingEnvironment object, one needs to use its environment method and pass the value he/she set the environment variable in his/her profile. Asp.Net developers provides the necessary tools to automate these changes but these changes are needed to be done by the user manually. It will take some processing time to reflect these changes.

Building a normal web application with .net is an easy task but as we add more things to the web site, which make it more complex, then it is necessary that one knows that he/she knows how to manage production and development settings in ASP .NET core.