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?

Mugen
  • 8,301
  • 10
  • 62
  • 140
  • Have you had a chance to review the following in docs yet https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2#set-the-environment – Nkosi Aug 04 '19 at 15:03
  • There's a way to set environment variables directly from Rider using [launch settings profiles](https://www.jetbrains.com/help/rider/Run_Debug_Configuration_dotNet_Launch_Settings_Profile.html#editing-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. – Viacheslav Shchupak Aug 18 '19 at 05:03

1 Answers1

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.

Mugen
  • 8,301
  • 10
  • 62
  • 140
  • https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2#host-configuration-1 – Nkosi Aug 05 '19 at 11:47
  • Yes, I read all of this doc. Its misguiding. See this snippet: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2#configureappconfiguration , 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. – Mugen Aug 05 '19 at 12:00
  • 2
    I understand what you mean. I can see how that can get confusing fast. Glad you found a solution to your problem. Happy Coding. – Nkosi Aug 05 '19 at 12:17