skipnothing/.NET Platform
UNIT 04

Configuration & Options

Strongly-typed config done right

appsettings.json is just the start, options pattern, validation, and environment-aware config.

Loading…
BUILDS ON
TOPICS
#18

The Configuration System

Trace one config key through a stack of providers, watch the last provider added overwrite the rest, and see why an environment variable silently beats committed appsettings.json while `__` replaces the colon in env-var names.

Set `ConnectionStrings:Default` in appsettings.json, then export that same setting as an environment variable. The exported value silently wins, even though you never reopened the JSON file and never told anything which source to prefer.

12 min
#19

The Options Pattern

Bind a config section onto a typed class, then choose between IOptions, IOptionsSnapshot, and IOptionsMonitor by how fresh the value must be, and see why a scoped snapshot cannot live inside a singleton.

An `appsettings.json` section stores `MaxRetries: 3` as text, but a C# class wants it as a real `int`. One line pours those keys into typed properties, and how you read them back has three answers.

14 min
#20

Options Validation

Attach validation rules to bound options with data annotations or a cross-field check, move the failure from the first request to startup, and read every problem from one OptionsValidationException.

A `MaxPoolSize` of 5000 sits in appsettings.json, binds cleanly into a typed object, and crashes nothing at startup. The app runs for hours on the bad value, then throws on the one request that finally reads it.

10 min
#21

Environments & Feature Flags

Flip `ASPNETCORE_ENVIRONMENT` to watch a second appsettings file layer on and `IsDevelopment()` branches switch, then contrast that frozen deploy-time string with feature flags read per request through `IsEnabledAsync`, toggled by percentage or time-window filters without a redeploy.

`Hosting environment: Production` prints in the startup log, even in an app you never configured. That one string decides which settings file loads on top of the base and which branch of your code runs.

10 min
#22

Secrets Management

Move a database password out of committed appsettings.json into a plaintext User Secrets file the repo never sees, then watch the same config["Db:Password"] read null in Production until a startup-loaded Key Vault or Secrets Manager provider supplies it, ordered by the same last-writer-wins stack.

A database password sitting in `appsettings.json` is a plaintext string your next `git commit` ships to everyone with repo access. The same value, read the same way, can instead live somewhere version control never sees.

10 min
All units