Configuration Providers
Configuration providers exist for settings files, environment variables, Azure key vault, Azure app configuration, command-line arguments, custom providers, key-per-file, and in-memory .NET objects.
Prefer Microsoft.Extensions.Configuration over System.Configuration.
Prefer Microsoft.Extensions.ConfigurationManager over System.ConfigurationManager.
packages
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Hosting — Not required unless using a Generic Host.
- Microsoft.Extensions.Configuration.Binder — Bind an object to data in configuration providers.
- Microsoft.Extensions.Configuration.Json — JSON configuration provider.
- Microsoft.Extensions.Configuration.EnvironmentVariables — Environment variables provider.
Note: System.Configuration.ConfigurationBuilder != Microsoft.Extensions.Configuration.ConfigurationBuilder.
binding
Configuration values can be bound to instances of .NET objects.
Interfaces:
- IConfiguration— Represents a set of key/value configuration properties.
- IConfigurationRoot— The root of an IConfiguration hierarchy.
- IConfigurationSection— A section of configuration values.
configuration provider priority
- Command Line
- App Secrets (Secret Manager) in development environment
- Environment Variables
- XML / JSON / INI
This means #1 will override #2 and so on.
json configuration provider
using IHost host = Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostingContext, configuration) =>
{
    // Clear all providers that were added from CreateDefaultBuilder():
    configuration.Sources.Clear();
    IHostEnvironment env = hostingContext.HostingEnvironment;
    // *Build Action* must be set to *Content*; *Copy to Output Directory* must be *Copy if newer* or *Always*.
    configuration
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true);
    // Get the root of the configuration hierarchy. Sections from the configuration can be bound to .NET
    // objects and provided as IOptions<TOptions> through dependency injection.
    IConfigurationRoot configurationRoot = configuration.Build();
    // This class is for the options in appsettings.json:
    TransientFaultHandlingOptions options = new();
    configurationRoot.GetSection(nameof(TransientFaultHandlingOptions))
    .Bind(options);
    Console.WriteLine($"TransientFaultHandlingOptions.Enabled={options.Enabled}");
    Console.WriteLine($"TransientFaultHandlingOptions.AutoRetryDelay={options.AutoRetryDelay}");
})
.Build();
// Application code should start here.
await host.RunAsync();
environment variables configuration provider
Same code as above, except:
configuration.AddEnvironmentVariables();
prefixes
// Set a prefix:
configuration.AddEnvironmentVariables(prefix: "CustomPrefix_");
environment variable hierarchical keys separator
The : separator does not work with environment variable hierarchical keys on all platforms. Use __ instead.
So, instead ofset TransientFaultHandlingOptions:Enabled="true"
Use:set TransientFaultHandlingOptions__Enabled="true"
Note: Use setx to persist the environment variable. Use setx /M to make it a system environment variable.
command line arguments configuration provider
Same come as above, except:
if (args is { Length: > 0})
configuration.AddCommandLine(args);
sending command line arguments with dotnet
dotnet run *SomeKey*="*Some value*"
ini configuration provider
Same code as above, except:
configuration.AddIniFile(appsettings.ini", optional: true, reloadOnChange: true)
memory configuration provider
using IHost host = Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((_, configuration) =>
    configuration.AddInmemoryCollection(
        new Dictionary<string, string> {
        ["*SomeKey*"] = "*Some value*",
        ["*SomeOtherKey*:*Enabled*"] = bool.TrueString
    }))
.Build();
additional reading
Safe storage of app secrets in development in ASP.NET Core | Microsoft Docs