This is the third article in .NET generic host series after introduction and important interfaces posts. There are two different methods
ConfigureAppConfiguration. These two methods seems to be very similar and both of these methods might have similar logic.
In this short article, let’s try to understand if they are same or not.
Host configuration is used for the properties of the IHostEnvironment implementation. Host configurations can be applied by calling ConfigureHostConfiguration on
IHostBuilder. This takes an Action delegate as input parameter, and we can use this delegate to add our own code to initialize the host configurations.
This can be called multiple times. The last value which is applied to a key is applied to the host.
This can load configurations from any source like environment variables, or command lines or JSON file.
App configurations is created by calling ConfigureAppConfiguration on
IHostBuilder instance. This also takes an an Action delegate as input parameter, and we can use this delegate to add our own code to initialize the application specific configurations.
This can also be invoked multiple times. The last value which is applied to a key is applied to the application.
Once all app configurations calls are executed, Configuration will be updated with the results for future build steps. The resulting configuration will also be available in the Services DI Container.
This can also load configurations from any source like environment variables or command lines or JSON file or from any other configuration provider.
As shown below, both delegates can be used to load the configurations.
So What is the difference ?
One obvious question that may bother us – why two methods ? Technically, there is nothing to stop you from writing the whole configuration initialization code in only one of the method.
In my opinion, this distinction might be helpful if the application configurations can be divided into two logical parts – host specific configuration and application specific configurations.
Host specific configuration settings (like content root, or web root paths or environment name) should be done as part of host configuration method call, while the rest of the application specific configurations should be loaded as part of app configurations call.
In short, the line between these two methods is extremely blur in my opinion. Do you share different opinion, I would love to know your thoughts.