Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting HostingEnvironment.EnvironmentName to Development in Rider

My code:

    public static async Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration(
                (hostContext, configApp) =>
                {
                    configApp.SetBasePath(Directory.GetCurrentDirectory());
                    configApp.AddJsonFile("appsettings.json");
                    configApp.AddJsonFile(
                        $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
                        optional: true);
                })
            .Build();

        await host.RunAsync();
    }

In runtime I see that the value of HostingEnvironment.EnvironmentName is Production.

I tried to set the following environment variable in running configuration, but it didn't change the runtime: ASPNETCORE_ENVIRONMENT=Development.

Where can I configure it?

like image 835
Mugen Avatar asked Aug 04 '19 14:08

Mugen


People also ask

How do you check if current environment is development or not?

If you need to check whether the application is running in a particular environment, use env. IsEnvironment("environmentname") since it will correctly ignore case (instead of checking if env. EnvironmentName == "Development" for example).


1 Answers

Adding the following solved the issue, I figured it after debugging with decompiled .NET source. It doesn't seem documented anywhere, or I'm missing something.

    public static async Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureHostConfiguration(configHost => configHost.AddEnvironmentVariables())
            .ConfigureAppConfiguration(
                (hostContext, configApp) =>
                {
                    configApp.SetBasePath(Directory.GetCurrentDirectory());
                    configApp.AddJsonFile("appsettings.json");
                    configApp.AddJsonFile(
                        $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
                        optional: true);
                })
            .Build();

        await host.RunAsync();
    }

The added line is ConfigureHostConfiguration call.

like image 82
Mugen Avatar answered Oct 17 '22 18:10

Mugen