One of the extremely powerful features of .Net Core is Configuration provider, which allows reading configuration data as key-value pairs from various sources like Azure Key Vault, directory files, Azure app configuration, and environment variables, to name but a few. Whenever we create a web application in .Net core, there are 5 default configuration providers that are loaded by the static method CreateDefaultBuilder () of Host Class at runtime.
- public static IHostBuilder CreateHostBuilder(string[] args) =>
- Host.CreateDefaultBuilder(args)
You can verify it, by putting a breakpoint in the constructor of the Startup class.
We can get detailed information about each by writing a line of code in a method (e.g.: OnGet()) of a class (e.g: IndexModel.cshtml.cs).
- IEnumerable<IConfigurationProvider> configurationProviders = ((ConfigurationRoot)_configuration).Providers;
For brevity and to keep the HTML table visible, I have removed fully qualified namespaces from the right-side column from below the image.
In the above image, there are 6 providers to distinguish from the default 5 providers, so I enabled the secret management feature of .Net core for this demo application. So, the total number of providers can vary based on your application configurations.
Built-in configuration providers read configuration data from. json/.xml file, environment variables, and command-line arguments.
All the concrete providers like jsonconfigurationprovider, commandlineconfiugrationprover etc, inherit from abstract class ConfigurationProvider which implements interface IConfigurationProvider.
Below is the code to generate the above HTML table
- <table border="1" style="border:1px solid black;">
- @{
- int configProviderCounter = 0;
-
- }
- <tr>
- <td> No </td>
- <td>File Name</td>
- <td> <b>Concrete Class</b> </td>
- </tr>
- @foreach (IConfigurationProvider configProvider in Model.ConfigProviders)
- {
- configProviderCounter = configProviderCounter + 1;
- <tr>
- <td>@configProviderCounter</td>
- <td> @configProvider.ToString() </td>
- <td> @configProvider.GetType().ToString().Substring(configProvider.GetType().ToString().LastIndexOf(".") + 1) </td>
- </tr>
-
- }
- </table>
I hope you found this information very helpful.