Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

appsettings does not get reloaded when using UseConfiguration extension method #1430

Closed
BennieCopeland opened this issue May 23, 2018 · 6 comments
Assignees
Milestone

Comments

@BennieCopeland
Copy link

The appsettings.json file is never reloaded on change when using the UseConfiguration extension method.

public static IWebHost DoesntWork(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
        .Build();

    return new WebHostBuilder()
        .UseKestrel()
        .UseConfiguration(config)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();
}

Using the ConfigureAppConfiguration extension method however does cause the appsettings.json file to be reloaded on change.

public static IWebHost Works(string[] args)
{
    return new WebHostBuilder()
        .UseKestrel()
        .ConfigureAppConfiguration((builder, config) =>
        {
            config.AddJsonFile("appSettings.json", optional: false, reloadOnChange: true);
        })
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();
}

I've created a sample project that uses a feature toggle to optionally print "Hello World" on the home page.
Example project: https://github.com/BennieCopeland/AppSettingBug

@Tratcher
Copy link
Member

That's expected. UseConfiguration copies all of the keys:

public static IWebHostBuilder UseConfiguration(this IWebHostBuilder hostBuilder, IConfiguration configuration)
{
foreach (var setting in configuration.AsEnumerable())
{
hostBuilder.UseSetting(setting.Key, setting.Value);
}
return hostBuilder;
}

It would be nice to change this behavior in 3.0.

@BennieCopeland
Copy link
Author

Is there documentation on the difference between host and application configuration? While I like the changes that took place with 2.0, it kind of muddied the waters when figuring out configuration. And this:

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

hides a lot of the magic. It's not clear on if I can override settings by just adding UseKestrel(options => {...}), so I went back to the long way of using WebHostBuilder and ran into my issue above.

It seems that there are now at least two configurations, a Host UseConfiguration and Application ConfigureAppConfiguration, which is fine, but the documentation could be clearer. The IOptionsSnapshot page talks about the hot reloading, but doesn't say how to enable it. The Configuration page, talks all about configuration, and file formats, and custom providers; but all the examples use the ConfigurationBuilder and don't show how it plugs into ASP.Net Core. The Hosting page talks about UserConfiguration, but ConfigureAppConfiguration isn't even documented. It shows up in a code example on the Startup page, but is never explained.

@Tratcher
Copy link
Member

@Steveiwonder

This comment has been minimized.

@Tratcher
Copy link
Member

That is not related to hosting or this issue. Please open a new issue against configuration.

@Tratcher
Copy link
Member

There's not a good way to fix UseConfiguration since it's an extension method on IWebHostBuilder which only has GetSetting and UseSetting and we're trying to avoid breaking the interface.

This isn't an issue going forward because generic host has ConfigureHostConfiguration with first class config support.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants