Lesson 3 of 9 · 22m
Configuration, Options, and Validation That Fail Fast
Model configuration explicitly, validate it at startup, and avoid secrets drift across environment variables, containers, and user secrets.
Production incidents caused by configuration are rarely dramatic. Nobody uploads a blank firewall rule or drops the wrong table. Someone deploys a container with ConnectionStrings__Orders spelled differently than the app expects, the app starts anyway, and the first customer request discovers the mistake. Configuration is code input. Validate it with the same seriousness as a public API payload.
Prefer typed options over stringly-typed lookups
Scattering configuration["Orders:BatchSize"] through a codebase creates invisible coupling. Bind the shape once:
public sealed class OrderOptions
{
public const string SectionName = "Orders";
public int BatchSize { get; set; }
public TimeSpan ReservationTimeout { get; set; }
public required string WarehouseUrl { get; set; }
}Register and validate it:
builder.Services
.AddOptions<OrderOptions>()
.BindConfiguration(OrderOptions.SectionName)
.Validate(o => o.BatchSize is >= 1 and <= 500, "BatchSize must be between 1 and 500.")
.Validate(o => Uri.TryCreate(o.WarehouseUrl, UriKind.Absolute, out _), "WarehouseUrl must be absolute.")
.ValidateOnStart();ValidateOnStart matters. Without it, invalid configuration can pass health checks and wait in the grass until the first use of the options object.
Know the configuration precedence order
Default host configuration composes multiple providers. In broad terms, later providers win: application settings files, environment-specific files, user secrets in development, environment variables, then command-line arguments. In containers and production Linux services, environment variables are often the operational override mechanism:
export Orders__BatchSize=25
export ConnectionStrings__Orders='Host=db.internal;Database=orders;Username=orders_app'The double underscore maps to configuration hierarchy. A typo creates a parallel universe where your intended setting exists but is never bound.
Secrets are configuration, but not all configuration is a secret
Use dotnet user-secrets for local development secrets, environment variables or a managed secret store in deployment, and never commit live credentials:
dotnet user-secrets init
dotnet user-secrets set "ConnectionStrings:Orders" "Host=localhost;Database=orders;Username=orders"appsettings.json can contain defaults and shape examples. It should not contain production passwords, API keys, signing keys, or connection strings with embedded credentials.
Feature configuration needs operational empathy
Configuration is often how operators disable a broken integration, reduce batch pressure, or shift traffic during an incident. Prefer explicit switches with bounded values:
{
"Orders": {
"BatchSize": 25,
"ReservationTimeout": "00:00:45",
"WarehouseUrl": "https://warehouse.internal"
}
}A value that can crash the service when invalid is not a flexible control; it is an unguarded edge.
Field notes
TimeSpanbinding accepts simple duration strings, but be consistent across environments; mixed formats invite subtle behavior changes.IOptionsMonitor<T>can react to reloadable configuration, but not every dependency can be re-created safely after the object graph exists.- Log validation failures without logging secret values. Redact connection strings and key material.
- Make configuration examples match real deployment shape. Placeholder examples age into traps.
Was this lesson helpful?