liveBook · Manning

AddEnvironmentVariables

As explained in ASP.NET Core in Action, 3e

AddEnvironmentVariables is a .NET configuration extension method that adds operating-system environment variables as configuration settings.

AddEnvironmentVariables adds operating-system environment variables as key-value configuration settings in ConfigurationManager. It is useful for supplying production values without storing secrets in source control. section 10.2.3

Important behavior:

  • WebApplicationBuilder adds the environment-variable provider by default, so explicit registration may already be unnecessary. section 10.2.3
  • Environment variables can override defaults from appsettings.json when values are supplied on a production machine or Docker container. section 10.2.3
  • Use double underscores to represent configuration sections where colons are unsupported; the imported IConfiguration key uses colons. section 10.2.3

Representing a nested setting

Set an environment variable named MapSettings__MaxNumberOfPoints. The provider imports it as MapSettings:MaxNumberOfPoints, which is the colon-based key to use when retrieving the value from IConfiguration. section 10.2.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines provider behavior, hierarchy syntax, defaults, and overrides without detailing custom registration code.
Examples
Uses MapSettings__MaxNumberOfPoints to demonstrate importing a hierarchical setting.
What you can do after
Operate/configure production values through environment variables while keeping secrets out of source control.
How it connects
Connects environment variables with ConfigurationManager, IConfiguration, appsettings.json, Docker, and User Secrets.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.