Would it be possible to have a better explanation of the differences between ConfigureHostConfiguration() and ConfigureAppConfiguration()?
What I understand is that the host is created first, and then the application is built. But, where can I find the actual difference between the host and the app?
Also, given that both the action that are passed to ConfigureHostConfiguration() and ConfigureAppConfiguration take a IConfigurationBuilder as the input, what kinds of configuration should be added to the host and which to the app configuration? I mean: is it ok to load the environment variable when configuring the host, and everything else in the app configuration? Does it even make a difference? I'm not sure how one should understand this...
Thank you
โ Do not edit this section. It is required for docs.microsoft.com โ GitHub issue linking.
@matteocontrini Thanks for the question. Yes, I believe we can make improvements in this area.
actual difference between the host and the app
It's summed up in the opening sentences, but we can assess if this has enough detail ...
.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management.
Assumed/unstated: The app is everything else.
@Tratcher Is it fair to say that host environment configuration (app name, environment, and content root) and Kestrel configuration are the primary targets of host configuration?
Can we say something to the effect that everything else falls under app config?
It's summed up in the opening sentences, but we can assess if this has enough detail ...
To be honest, I actually missed that ๐
But I'm still unsure how a developer should be affected from the distinction between the host and the app. For example, I can write:
new HostBuilder()
.ConfigureHostConfiguration((config) =>
{
config.AddJsonFile("appsettings.json");
})
But also:
new HostBuilder()
.ConfigureAppConfiguration((host, config) =>
{
config.AddJsonFile("appsettings.json");
})
And unless I'm missing something the effect is the same. If there's a difference between the two, I think it could be made clearer. If there's no difference, it would be useful to have some indication on where to configure what.
Thanks!
Config set in ConfigureHostConfiguration flows to the app's config. The last config provider to set a value (in either method) on a key wins.
I'll add a note on this to the PR.
should be affected from the distinction
There's no need to set more config in ConfigureHostConfiguration than is required to configure the host. Everything else can go in app config. We'll hear back from engineering soon, and then I can put in a PR to surface some context (what to configure where) for the two methods. Thus far for host config AFAIK, it's ...
Yes, those Host settings can only be set via ConfigureHostConfiguration. FYI Kestrel is more nuanced. The Urls setting is considered a host level setting, it's the host that reads it and passes it to the servers through a specific IServerAddresses API. Kestrel's extended binding config is kestrel specific and is read directly from config after ConfigureAppConfiguration.
Thanks @Tratcher ... I'll see if I can work the Kestrel language carefully according to what you provided.
@matteocontrini ... should get to this fairly quickly.
I'm buried in Health Checks code at the moment โ๐ ... but I'll get to this soon-ish. ๐๐
@guardrex no problem, thank you ๐
@Tratcher We don't have the Server URLs section (that's in the Web Host (Server URLs) topic) in the Generic Host topic. Your answer above seems to indicate that we should have it in Generic Host. Should I add it along with this update?
~If so, should it show UseUrls on WebHost or UseSetting with the key (urls) in HostBulder? (There's already an example using hostsettings.json where the key can be mentioned. When I say "show," I mean in the added Server URLs section.)~ Nevermind ... no UseSetting is available.
Oh, I'd overlooked this was generic host. There's no Urls or Kestrel support for generic host until 3.0, you can just exclude that.
I wish there was an extension method for IHostBuilder to set the app key (name) to go along with content root and environment ... just so that they all get set about the same way. It's uneven to have (and explain) ...
var host = new HostBuilder()
.ConfigureAppConfiguration((hostContext, configApp) =>
{
hostContext.HostingEnvironment.ApplicationName = "CustomApplicationName";
})
Most helpful comment
Config set in
ConfigureHostConfigurationflows to the app's config. The last config provider to set a value (in either method) on a key wins.I'll add a note on this to the PR.
There's no need to set more config in
ConfigureHostConfigurationthan is required to configure the host. Everything else can go in app config. We'll hear back from engineering soon, and then I can put in a PR to surface some context (what to configure where) for the two methods. Thus far for host config AFAIK, it's ...