skip to content

In ASP.NET Core, what sources does WebApplication.CreateBuilder load and in which order, and why might production ignore a value set in appsettings.Production.json?

level: seniorimportance: should knowfreq 58%

answer

  1. files, secrets, variables, arguments
  2. prefixed variables at the very bottom
  3. {ApplicationName}.settings.json after appsettings
  4. user secrets only in Development
  5. GetDebugView names the winning provider

basics

~20 s

Later sources win: ASPNETCORE_ and DOTNET_ variables, appsettings.json, appsettings.{Environment}.json, {ApplicationName}.settings files, user secrets in Development, all environment variables, then command-line arguments. Production usually ignores a file value because a variable or argument overrides it, or the file never loads.

solid answer

~40 s

`WebApplication.CreateBuilder(args)` fills one `ConfigurationManager`, and a lookup takes the last source that defines the key. The order is: `ASPNETCORE_`- and `DOTNET_`-prefixed variables (prefix stripped), `appsettings.json`, `appsettings.{Environment}.json`, `{ApplicationName}.settings.json` and its environment twin, user secrets (Development only), all environment variables, and command-line arguments. When production ignores `appsettings.Production.json`, the usual causes are: the platform sets the same key as an environment variable or argument; the environment name is not `Production`, or the file's casing does not match on Linux; the file was not deployed; a source added later in `Program.cs` overrides it; or the key path is misspelt. Calling `GetDebugView()` on `builder.Configuration` prints every key with the provider that supplied it, which settles it in one run.

code

csharp · 7 lines
csharp
var builder = WebApplication.CreateBuilder(args);

foreach (var source in builder.Configuration.Sources)
    Console.WriteLine(source.GetType().Name);   // registration order, last wins

// Added after CreateBuilder: overrides env vars and command line
builder.Configuration.AddJsonFile("features.json", optional: true, reloadOnChange: true);

go deeper

for a junior

Know that environment variables and command-line arguments beat the appsettings files, and that user secrets are a Development-only source.

for a middle

Recite the default chain in order, including where the prefixed variables and the {ApplicationName}.settings files sit.

for a senior

Diagnose an ignored value systematically: environment name, injected variables, deployed files, extra sources in Program.cs, then GetDebugView with masked secrets.

for a principal

Decide which layer owns each class of setting across environments and keep the number of override layers small enough that a dump explains every value.

## The full default chain `WebApplication.CreateBuilder(args)` creates a `ConfigurationManager` and adds sources in this order. A lookup scans **from the last source to the first** and returns the first hit, so a later source wins: 1. Environment variables prefixed `ASPNETCORE_`, with the prefix removed. 2. Environment variables prefixed `DOTNET_`, with the prefix removed. 3. Command-line arguments are applied early for host settings as well, and again at the end. 4. `appsettings.json`. 5. `appsettings.{Environment}.json`. 6. `{ApplicationName}.settings.json`, then `{ApplicationName}.settings.{Environment}.json`. 7. User secrets, **only when the environment is Development**. 8. **All** environment variables, unprefixed, with `__` read as `:`. 9. Command-line arguments, when `args` was passed to `CreateBuilder`. The docs' priority list names the command line, environment variables, user secrets and the two appsettings files; the 10.0.12 source also loads the two `{ApplicationName}.settings` files. Every JSON file is optional and registered with `reloadOnChange` on by default. ## Why production ignores a file value | Symptom | Cause | Check | |---|---|---| | Value differs from the file | An environment variable such as `Features__OnlineApplications` or a `--Features:OnlineApplications` argument overrides it | List the container's variables and process arguments | | No Production value at all | The environment name is not `Production` (unset, misspelt, or `production` on a case-sensitive file system) | Log `builder.Environment.EnvironmentName` | | File present locally, absent on the server | The file was not in the deployment output | List the files next to the app's dll | | Value changed by your own code | `builder.Configuration.AddJsonFile(...)` or `AddInMemoryCollection(...)` called after `CreateBuilder` sits above everything | Read `Program.cs` for extra sources | | Key never matched | A typo in the section path, or an array override that left later elements in place | Compare the flattened keys | The first row is the most common. Hosting platforms and container orchestrators inject settings as environment variables, and they outrank every JSON file by design. Source order also explains the reverse surprise: a value in **user secrets** works on a developer laptop and vanishes in production because that source is added only in Development. ## Finding the winning provider `ConfigurationManager` implements `IConfigurationRoot`, so `GetDebugView()` is available. It prints every key with its value and the provider that won, for example `JsonConfigurationProvider for 'appsettings.Production.json' (Optional)` or `EnvironmentVariablesConfigurationProvider`. ```csharp if (builder.Environment.IsDevelopment()) { Console.WriteLine(builder.Configuration.GetDebugView(ctx => ctx.Key.Contains("Password", StringComparison.OrdinalIgnoreCase) ? "***" : ctx.Value ?? "")); } ``` The overload taking a `Func<ConfigurationDebugViewContext, string>` lets you mask values, because the dump includes connection strings and secrets and must never reach logs unfiltered. `builder.Configuration.Sources` lists the registered sources in order when you need only the chain. ## Reading the dump A trimmed `GetDebugView()` for the adoption API in production might show: - `ConnectionStrings:` then `Adoptions=Host=db;... (EnvironmentVariablesConfigurationProvider)` - the platform's variable won. - `Features:` then `OnlineApplications=false (JsonConfigurationProvider for 'appsettings.Production.json' (Optional))` - the file won. - `VideoMeetAndGreet=true (JsonConfigurationProvider for 'appsettings.json' (Optional))` - no override exists. If the Production file never appears in the dump at all, the problem is the environment name, the content root (the default is the current working directory, so starting the app from another folder hides every file) or a missing file, not precedence. ## Controlling the chain - Anything you add after `CreateBuilder` goes **on top**. `builder.Configuration.AddJsonFile("features.json", optional: true)` therefore overrides environment variables and arguments, which is rarely what a reader of the code expects. - `CreateBuilder()` without `args` registers no command-line provider for app settings. - `builder.Configuration.Sources.Clear()` followed by your own list gives full control, at the price of rebuilding what the default provides. ## Interview checklist - Recite the order from the files up to the command line, and say later wins. - Place user secrets in Development only and the prefixed variables at the bottom. - Give at least three causes for an ignored file value and the `GetDebugView()` check.

  • Why does a setting kept in user secrets work on a laptop but disappear in production?
    The default chain adds the user-secrets provider only when the environment is Development. In Production that source is absent, so the key falls back to the JSON files or is missing; production values must come from environment variables, a vault provider or a mounted source.
  • You add builder.Configuration.AddJsonFile("features.json") so operators can tune flags. What precedence does it get?
    It is appended after the command-line provider, so it becomes the highest-precedence source and overrides environment variables and arguments. If operators should still be able to override it with a variable, insert it lower in `builder.Configuration.Sources` or re-add the environment-variables provider after it.

  • appsettings.Production.json has the highest precedence because it is the most specific file.
  • User secrets are loaded in every environment, so they are a safe place for production keys.
  • A JSON file added with AddJsonFile after CreateBuilder sits below environment variables.
  • The default chain loads only appsettings.json, appsettings.{Environment}.json, env vars and arguments.
  • ASPNETCORE_-prefixed variables override everything because the framework reads them first.