You are currently viewing Some Important Generic Host Settings For .NET Applications

Some Important Generic Host Settings For .NET Applications

This article is extension to generic host series. Previous articles were about introduction, interfaces and host configurations. In this article, let’s quickly have a look at some settings that can be applied to generic host.

Application Name

This is a setting from IHostEnvironment implementation. By default this value is set to the name of the assembly which contains the entry point (i.e. the main method).

If required, we can change this value via an environment variable. The environment variable to use would be PREFIX_APPLICATIONNAME where PREFIX is the string that is prefix identify your application related environment variables. PREFIX can be replaced by any text.

Environment Name

This is also a setting from IHostEnvironment implementation . This value can be set via environment variable PREFIX_ENVIRONMENT where PREFIX can be replaced by any text of your choice.

There are three framework defined values – Development, Staging and Production, but this can be set to any string value. Also, the values are not case sensitive.

This can also be set by calling UseEnvironment method.

Content Root path

This is also a setting from IHostEnvironment implementation. This setting is the path where host looks for content files. If this value is not set, the host fails to start.

This value can be set via the environment variable PREFIX_CONTENTROOT. This can also be set by calling UseContentRoot method.

Web Root Path

This is a setting from IWebHostEnvironment. This property is very specific to web applications (APIs, MVC app, Razor views app, etc.). This path is the relative path for public static resources (*.js, *.css, images, etc).

The default value is wwwroot folder inside the content root path. This folder must exist.

This value can also be set via code on the host builder. or via the environment variable PREFIX_WEBROOT. In code, this can be set by calling UseWebRoot method.

Startup Assembly

The assembly where the framework can look for Startup class. By default, this value is set to application’s assembly.

This can be set via code, or via environment variable PREFIX_STARTUPASSEMBLY. In code, this can be set UseStartup method. If multiple methods are called, the last one takes the preference.

Disable Reload On Change

The reloadOnChange is the parameter which decides if the configuration should be reloaded after it is changed. This setting can also be applied via environment variable PREFIX_hostBuilder:reloadConfigOnChange, the default value is true. If there is need to disable this behavior, this environment variable can be set to false.

Code Example

Enough of boring (but important) theory. Let’s look at the code example, which shows how the code can be used to apply these settings.

If you want to apply these settings via environment variables, please refer this blogpost. Have you used any of the above settings ? Let me know your thoughts.

Leave a Reply