ASP.NET Core configuration: IOptions vs IOptionsSnapshot vs IOptionsMonitor

C# Language Interview Q&A November 13, 2026 csharp

Sources in order (the last one wins): appsettings.json → appsettings.{Environment}.json → User Secrets (Development only) → environment variables → command line.

csharp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// ======================================================================
// Q: How does configuration work? IOptions vs IOptionsSnapshot vs
// IOptionsMonitor?
//
// Sources in order (the last one wins): appsettings.json →
// appsettings.{Environment}.json → User Secrets (Development only) → environment
// variables → command line. On Azure: Key Vault / App Configuration.
// - IOptions<T>: Singleton lifetime. No runtime changes: read once.
// - IOptionsSnapshot<T>: Scoped lifetime. Picks up changes per request. Not
//   usable in singletons.
// - IOptionsMonitor<T>: Singleton lifetime. Picks up changes, via CurrentValue
//   and OnChange.
// ======================================================================

builder.Services.AddOptions<ErpOptions>()
    .Bind(builder.Configuration.GetSection("Erp"))
    .ValidateDataAnnotations()
    .ValidateOnStart();      // bad config = the app does not start, instead of blowing up later