dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

ConfigurationBuilder's AddJsonFile not Working in 3.1? #17992

Closed mrlife closed 4 years ago

mrlife commented 4 years ago

I noticed that .AddJsonFile() is called automatically for appsettings.json and appsettings.{Environment}.json. I'm not sure if this is new behavior, but I do know that calling AddJsonFile() in ConfigureAppConfiguration() doesn't seem to have an affect anymore, but should it?

I read through the docs but don't see where this should be done differently. Here is my Main():

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();

    var host = CreateHostBuilder(args)

        .ConfigureAppConfiguration((hostingContext, config) =>
                                                    {
            IHostEnvironment env = hostingContext.HostingEnvironment;

            //last registration wins here
            config
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();
            })
        .Build();

    host.Run();
}

Further technical details

.NET Core SDK (reflecting any global.json): Version: 3.1.100 Commit: cd82f021f4

Runtime Environment: OS Name: Mac OS X OS Version: 10.15 OS Platform: Darwin RID: osx.10.15-x64 Base Path: /usr/local/share/dotnet/sdk/3.1.100/

Host (useful for support): Version: 3.1.0 Commit: 65f04fb6db

.NET Core SDKs installed: 3.1.100 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

davidfowl commented 4 years ago

CreateHostBuilder(args).Build().Run();

The above line of code blocks forever so the second line of code that configures a new host with new configuration will never run.

PS: Are you trying to add new configuration sources? From the above, it looks like you're re-adding the defaults.

mrlife commented 4 years ago

Yes, deleting the extra line CreateHostBuilder(args).Build().Run(); fixed the issue. It's not clear how it got added, but this issue can be fully deleted if you want.