Setting HostingEnvironment.EnvironmentName to Development in Rider
Asked Answered
G

1

8

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?

Glucinum answered 4/8, 2019 at 14:30 Comment(2)
Have you had a chance to review the following in docs yet learn.microsoft.com/en-us/aspnet/core/fundamentals/…Eructate
There's a way to set environment variables directly from Rider using launch settings profiles. No need to create a System wide Environment Variable to run or debug project in Rider. And you may switch between many profiles for different environments quickly and easily.Bevin
G
9

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.

Glucinum answered 5/8, 2019 at 6:42 Comment(3)
learn.microsoft.com/en-us/aspnet/core/fundamentals/host/…Eructate
Yes, I read all of this doc. Its misguiding. See this snippet: learn.microsoft.com/en-us/aspnet/core/fundamentals/host/… , that's the one I used. It simply doesn't work without calling ConfigureHostConfiguration first, which is not mentioned in this doc as far as I could find.Glucinum
I understand what you mean. I can see how that can get confusing fast. Glad you found a solution to your problem. Happy Coding.Eructate

© 2022 - 2024 — McMap. All rights reserved.