1

I have this code:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {

        var hostBuilder = WebHost.CreateDefaultBuilder(args);

        var settingName = "SHUTDOWNTIMEOUTSECONDS";
        var settingTiemOut= hostBuilder.GetSetting(settingName);
        //...
        //convert to double dbltimeout
        //...
        hostBuilder
            .UseShutdownTimeout(TimeSpan.FromSeconds(dbltimeout))
            .UseStartup<Startup>();

        return hostBuilder;
    }
}

I understand CreateDefaultBuilder loads configuration from 'appsettings.json', environment variables and command line args. If I define SHUTDOWNTIMEOUTSECONDS in launchSettings.json like:

  "environmentVariables": {
    "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "30"
  }

then settingTiemout is set to 30 correctly. However, if I remove it from launchSettings.json and add it in appsettings.json, settingTiemout gets null. why?

thank you

1 Answer 1

2

It's null because it's no longer a "setting" at that point. It's configuration, which according to the docs:

IWebHostBuilder configuration is added to the app's configuration, but the converse isn't true—ConfigureAppConfiguration doesn't affect the IWebHostBuilder configuration.

In other words, config in something like appsettings.json is not available to the host builder, because it's defined in ConfigureAppConfiguration, which is not available until after the host is built. To add JSON config for the actual host builder, you need to use the UseConfiguration extension to override the host builder config. For example:

var hostBuilder = WebHost.CreateDefaultBuilder(args);

...

var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("hostsettings.json", optional: true)
    .AddCommandLine(args)
    .Build();
hostBuilder.UseConfiguration(config);

A separate JSON file is used here, because the format for configuration matters. Specifically, it needs to match the config variable names the host builder expects, which precludes the use of sections in your JSON file, like you'd typically see in something like appsettings.json. In other words, with this setup, you just need to add a hostsettings.json file to your project with something like the following inside:

{
    "shutdownTimeoutSeconds": 30
}

You can consult this static class for the supported available key names to put in your JSON file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Chris Pratt. So what goes to launchSettings.json are settings (of IWebHostBuilder ). What is defined in appsettings.json is app configuration. This two will be available in app Configuration only after Build(). What was confusing me is the remarks of CreateDefaultBuilder() where it says that loads from 'appsettings.json', environment variables , command line args etc , but missed that is only after Build(). Thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.