ASP.NET Core Configuration & Environments 1 — Questions and Answers
Question 1: What is the default hierarchy of configuration providers in ASP.NET Core?
- Environment variables override appsettings.json which overrides command-line
- appsettings.json → appsettings.{Env}.json → Environment variables → Command-line (last wins) (Correct answer)
- Command-line → appsettings.json → Environment variables
- appsettings.json → Command-line → Environment variables
Correct answer: appsettings.json → appsettings.{Env}.json → Environment variables → Command-line (last wins)
Later providers override earlier ones: appsettings.json → environment-specific file → env vars → command-line args.
Question 2: Which environment variable sets the current environment in ASP.NET Core?
- ASPNETCORE_ENV
- DOTNET_ENVIRONMENT
- ASPNETCORE_ENVIRONMENT (Correct answer)
- APP_ENVIRONMENT
Correct answer: ASPNETCORE_ENVIRONMENT
ASPNETCORE_ENVIRONMENT sets the hosting environment name, which determines which appsettings.{Env}.json file is loaded.
Question 3: How do you bind a configuration section to a strongly-typed class?
- Configuration.GetSection("Key").Bind(instance) (Correct answer)
- Configuration.Map<T>("Key")
- Configuration.Parse<T>("Key")
- Configuration.Cast<T>("Key")
Correct answer: Configuration.GetSection("Key").Bind(instance)
GetSection("Key").Bind(instance) or GetSection("Key").Get<T>() maps configuration values to properties of a strongly-typed class.
Question 4: What does IOptions<T> provide compared to IOptionsSnapshot<T>?
- IOptions is per-request; IOptionsSnapshot is singleton
- IOptions is singleton and does not reload; IOptionsSnapshot reloads per-request (Correct answer)
- They are identical
- IOptionsSnapshot is for validation only
Correct answer: IOptions is singleton and does not reload; IOptionsSnapshot reloads per-request
IOptions<T> is a singleton that doesn't reload; IOptionsSnapshot<T> is scoped and reflects configuration changes per request.
Question 5: Which interface provides configuration that reloads when the source file changes?
- IOptions<T>
- IOptionsSnapshot<T>
- IOptionsMonitor<T> (Correct answer)
- IConfigurationReloader<T>
Correct answer: IOptionsMonitor<T>
IOptionsMonitor<T> is a singleton that fires a change notification and provides the latest configuration when the source changes.
Question 6: How do you add a custom configuration source in ASP.NET Core?
- Override WebApplication.Configure()
- Implement IConfigurationSource and IConfigurationProvider, then call builder.Configuration.Add() (Correct answer)
- Add a [ConfigSource] attribute
- Register in appsettings.json
Correct answer: Implement IConfigurationSource and IConfigurationProvider, then call builder.Configuration.Add()
You implement IConfigurationSource to describe the source and IConfigurationProvider to load data, then add it via builder.Configuration.Add().
What is the default hierarchy of configuration providers in ASP.NET Core?