StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 2 · Configuration and Options

Environment Override Precedence

easy100%1 of 1 solved

Problem statement

Add the configuration sources back in the right order, so a setting comes from the base file, then the environment's own file, then an environment variable, with each one overriding the one before.

Requirements

Program calls ComposeConfigurationSources, which removes every default configuration source. Your task has one part: add the sources back in StatusPageApi/Configuration/ConfigurationComposition.cs.

Configuration sources are read in the order you add them, and a later source overrides the same key from an earlier one. The current environment name, such as Development or Staging, is in builder.Environment.EnvironmentName.

ConfigurationComposition

Add these three sources, in this order:

  • appsettings.json, the base file
  • appsettings.{EnvironmentName}.json, for example appsettings.Staging.json when the environment is Staging. Mark it optional: some environments, such as Production, have no file of their own, and the app must still start
  • the environment variables. A variable such as StatusPage__WelcomeMessage (two underscores) sets the key StatusPage:WelcomeMessage

The settings files are provided, all under the StatusPage section:

  • appsettings.json sets all three keys:
    • WelcomeMessage: Welcome from the base settings file
    • RefreshSeconds: 60
    • SupportContact: support-base@example.com
  • appsettings.Staging.json sets two keys:
    • WelcomeMessage: Welcome from the staging settings file
    • RefreshSeconds: 30
  • appsettings.Demo.json sets one key:
    • WelcomeMessage: Welcome from the demo settings file

ConfigController (provided)

  • GET /config/effective answers 200 with welcomeMessage, refreshSeconds and supportContact, as the composed configuration resolves them
Sample Requests & Responses

GET /config/effective when the environment is Development, which has no StatusPage settings of its own

Response (200 OK):

{
  "welcomeMessage": "Welcome from the base settings file",
  "refreshSeconds": 60,
  "supportContact": "support-base@example.com"
}

GET /config/effective when the environment is Staging

Response (200 OK):

{
  "welcomeMessage": "Welcome from the staging settings file",
  "refreshSeconds": 30,
  "supportContact": "support-base@example.com"
}

GET /config/effective when the environment is Staging and StatusPage__WelcomeMessage is set to "Welcome from the environment variables"

Response (200 OK):

{
  "welcomeMessage": "Welcome from the environment variables",
  "refreshSeconds": 30,
  "supportContact": "support-base@example.com"
}

More in Configuration and Options

See the module
Typed Smtp SettingsThis problem covers binding a configuration section to a typed settings class with the options pattern, and receiving those settings in a service through IOptions.100%1 of 1 solvedeasyFail-Fast Options ValidationThis problem covers adding validation attributes to an options class and checking them as the application starts, so a bad setting stops it with an error naming that setting.100%1 of 1 solvedeasyPayment Gateway Settings ModuleThis problem covers layering the payment settings from files and environment variables, binding the nested gateway and retry sections to typed options, validating them at startup, and reading them in a service through IOptions.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.