Configuration and the options pattern

1 min read
On trails:ASPNETstep 5/7

The configuration system

ASP.NET Core has a flexible, extensible configuration system. It reads settings as key-value pairs from an ordered set of configuration providers. Common providers include JSON files (appsettings.json and appsettings.{Environment}.json), XML files, environment variables, command-line arguments, Azure Key Vault, and custom providers.

IConfiguration

IConfiguration is the primary interface for accessing configuration values in the application. It’s typically available through DIDependency injection and service lifetimesHow ASP.NET Core hands each class what it needs, and how long each instance lives.Opens in a new tab.

The options pattern

The options pattern lets you bind configuration sections to strongly-typed POCO classes, which gives type-safe access to configuration settings. There are three interfaces:

  • IOptions<T> provides singleton options.
  • IOptionsSnapshot<T> provides scoped options that are recomputed per request.
  • IOptionsMonitor<T> can detect changes to configuration sources at runtime.

Sources