Search code examples
c#.net-core.net-core-2.2.net-core-configuration

Proper use of IsDevelopement() on self hosted .net core 2.2 application


I have a self-hosted .NET core 2.2 console app that does not use the web host builder as I do not need HTTP endpoints for this service.

I'm trying to leverage the environment variable through the IsDevelopment() method of the hosting environment but it always comes back as Production.

The following is how I've setup my host builder. I have an environment variable called ASPNETCORE_ENVIRONMENT with a value of Development which leads me to ask two questions.

  1. What is the proper way to have this set when building my own host so that I can conditionally add user secrets to my configuration when building the host?
  2. Second question is if I can use a different environment variable other than ASPNETCORE_ENVIRONMENT since my app is not an ASP.NET core application?

I realize I could probably write code just before building the HostBuilder that explicitly looks for an environment variable and set the environment manually, but ASP.NET Core seems to hook this up behind the scenes so I wanted to see if there was a way to get a similar behavior when I'm not using the web host builder.

private static IHost BuildEngineHost(string[] args)
{
    var engineBuilder = new HostBuilder()
        .ConfigureAppConfiguration((hostContext, config) =>
        {
            config.SetBasePath(Directory.GetCurrentDirectory());
            config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            config.AddEnvironmentVariables();
            if(hostContext.HostingEnvironment.IsDevelopment())
                config.AddUserSecrets<EngineOptions>();
        })
        .ConfigureServices((hostContext, services) =>
        {
            services.Configure<EngineOptions>(hostContext.Configuration.GetSection("EngineOptions"));
            services.AddHostedService<EtlEngineService>();
        })
        .ConfigureLogging((hostContext, logging) =>
        {
            logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
            logging.AddConsole();
        });
    return engineBuilder.Build();
}

UPDATE: The following is needed to configure the host before configuration the application

.ConfigureHostConfiguration(config =>
{
    config.AddCommandLine(args);
    config.AddEnvironmentVariables();
})

This is called before .ConfigureAppConfiguration() and is loaded from any variable called "Environment" which means I don't have to use ASPNET_ENVIRONMENT.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2


Solution

  • I was able to have the hosting environment initialized with the correct values by calling ConfigureHostConfiguration() before AppConfiguration which properly sets the environment values in the host which I came across in the following doc from Microsoft.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2

    private static IHost BuildEngineHost(string[] args)
    {
        var engineBuilder = new HostBuilder()
            .ConfigureHostConfiguration(config =>
            {
                config.AddEnvironmentVariables();
                config.AddCommandLine(args);
            })
            .ConfigureAppConfiguration((hostContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                config.AddEnvironmentVariables();
                if(hostContext.HostingEnvironment.IsDevelopment())
                    config.AddUserSecrets<EngineOptions>();
            })
            .ConfigureServices((hostContext, services) =>
            {
                services.Configure<EngineOptions>(hostContext.Configuration.GetSection("EngineOptions"));
                services.AddHostedService<EtlEngineService>();
            })
            .ConfigureLogging((hostContext, logging) =>
            {
                logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
            });
        return engineBuilder.Build();
    }