Reginald Blue
Reginald Blue

Reputation: 982

Host.CreateDefaultBuilder not loading environment config overrides

I've reviewed the other questions on this topic, but none of them seem to have the same issue. Configuration:

According to the docs, this set of calls:

        var builder = Host.CreateDefaultBuilder(args);
        builder.ConfigureServices(ConfigureServices);
        var host = builder.Build();

Should automagically load the appropriate environment config overrides.

(ConfigureServices in this case just loads one dummy service)

I have an appconfig.json, an appconfig.Development.json, both marked as "Copy Always" and do show up in the target directory.

They have just one entry each, the same entry, but different values:

  {
    "Foo": "Bar" 
  }

and

  {
    "Foo": "Quz" 
  }

I have these environment variables set in the launchSettings.json:

  {
    "profiles": {
      "ConsoleApp1": {
        "commandName": "Project",
        "environmentVariables": {
          "DOTNET_ENVIRONMENT": "Development",
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      }
    }
  }

When I run the application it says it's in the correct enviornment:

        var hostEnv = host.Services.GetService(typeof(IHostEnvironment)) as IHostEnvironment;

        Console.WriteLine($"Environment: {hostEnv.EnvironmentName}");

Displays:

    Environment: Development

But when I try to retrieve the config value, I get the base value, not the override:

        var config = host.Services.GetService(typeof(IConfiguration)) as IConfiguration;
        var children = config.GetChildren();
        var foo = children.First(c => c.Key == "Foo");
        Console.WriteLine($"Foo: {foo.Value}");

Displays:

    Foo: Bar

But based on the override, it should show "Foo: Quz".

Right after the build completes, looking at the host and digging down shows that it seems like it will try to load the Development file: DebugViewOfHostDevelopmentConfig

But when retrieving the config root itself, it seems like it's empty: DebugViewOfEmptyDevelopmentConfig

I checked the file in the bin folder and it is definitely not empty.

Upvotes: 3

Views: 2360

Answers (1)

Reginald Blue
Reginald Blue

Reputation: 982

Unfortunately, the answer here is both embarrassing and anti-climactic. I am tempted to delete the question, but perhaps this will help someone else.

The answer is that the file name I used was not:

appsettings.Development.json

but

appsettings.Development..json

Note carefully that there are two periods before json, which is exceedingly hard to see in the Visual Studio Solution Explorer.

enter image description here

Upvotes: 3

Related Questions