Configuration is the set of externally supplied settings that control application behavior without requiring a new immutable container image for every environment, region, or operational change. Typical differences include database connection details, logging levels, and dependent API addresses. Keep configuration distinct from secrets: exposed configuration presents little or no risk, while cryptographic keys, connection strings, and API keys require protection. section 13.4
Listing 13.10 Environment-specific configuration values in appsettings
{
"EnvironmentName": "Development",
"RegionName": "My Desk"
}
Environment-specific appsettings files provide values such as environment and region names. section 13.4
ASP.NET Core configuration combines hierarchical sources into IConfiguration and resolves duplicate keys by applying later sources over earlier ones:
- appsettings.json supplies baseline values. section 13.4.2
- appsettings.Environment.json overrides baseline values for the active environment. section 13.4.2
- User secrets, environment variables, and command-line arguments override earlier sources in that order. section 13.4.2
- Verify precedence by changing one TestOverwrite key through each source and observing the /config response. section 13.4.2
Read a narrow section when a component needs only part of the hierarchy, or bind that section to a typed record through the options pattern. Typed settings provide stronger types and simplify unit testing compared with mocking IConfiguration. Binding occurs at startup: later configuration changes do not update the bound objects, and changes to those objects do not flow back to configuration. section 13.4.5
Listing 13.20 Using the options pattern to get CosmosDbSettings
using Microsoft.Extensions.Options; #1
public class CosmosDbRepository
{
private readonly CosmosDbSettings _settings; #2
public CosmosDbRepository(
IOptions<CosmosDbSettings> options) #3
{
_settings = _options.Value; #4
}
private void Connect()
{
var dbName = _settings.DatabaseName; #5
var endpoint = _settings.Endpoint;
}
}
The options pattern injects a bound CosmosDbSettings record through IOptions and accesses typed properties in the repository. section 13.4.5
Listing 13.21 Creating test configuration with options pattern
public class CosmosDbRepositoryTests
{
private readonly CosmosDbRepository _repo;
public CosmosDbRepositoryTests()
{
var config = new CosmosDbSettings( #1
"somedb",
"https://somedb.documents.azure.com/");
_repo = new(Options.Create(config)); #2
}
}
Options.Create supplies a typed configuration object directly in a repository test, avoiding IConfiguration mocking. section 13.4.5






















