In ASP.NET Core, what does reloadOnChange do for the appsettings files, which sources never reload, and how does code observe a change?
answer
- on by default for JSON files
- hostBuilder:reloadConfigOnChange switch
- file watcher, 250 ms delay
- variables and arguments read once
- GetReloadToken and ChangeToken.OnChange
basics
~20 sWith reloadOnChange, which the default builder turns on for every JSON file, a file watcher re-reads the file after a save and fires the configuration's reload token. Environment variables and command-line arguments are read once at startup and never reload.
solid answer
~40 sThe default builder registers `appsettings.json`, `appsettings.{Environment}.json`, the `{ApplicationName}.settings` files and user secrets with `reloadOnChange: true`, unless `hostBuilder:reloadConfigOnChange` is set to `false`. A `PhysicalFileProvider` watches each file; after a change it waits `ReloadDelay` (250 ms), re-reads the file and calls `OnReload`, which fires the token from `IConfiguration.GetReloadToken()`. The next `config["Features:VideoMeetAndGreet"]` read returns the new value, but anything copied into a field at startup does not change. Environment variables and command-line arguments have no watcher. Observe changes with `ChangeToken.OnChange(() => config.GetReloadToken(), callback)`, expecting the callback more than once per save. On mounted volumes where file events are unreliable, set `DOTNET_USE_POLLING_FILE_WATCHER=true` to poll every 4 seconds.
code
csharp · 12 linesvar app = builder.Build();
var logger = app.Logger;
// read on every request: sees edits to appsettings.json
app.MapGet("/features/video", (IConfiguration config) =>
config.GetValue<bool>("Features:VideoMeetAndGreet"));
ChangeToken.OnChange(
() => app.Configuration.GetReloadToken(),
() => logger.LogInformation("Configuration reloaded"));
app.Run();go deeper
Know that editing appsettings.json can take effect without a restart, while environment variables need one.
Explain the watcher, the 250 ms delay, the reload token and why values captured at startup stay stale.
Plan for duplicate callbacks, mounted volumes that need the polling watcher, and broken saves that drop a provider's keys.
Decide which settings may change live at all, and route those through a reviewed path so a hot edit cannot quietly flip production behaviour.
## What reloadOnChange means `AddJsonFile(path, optional, reloadOnChange)` is the JSON provider's registration. With `reloadOnChange: true` the provider asks its file provider to **watch** the file. When the watcher reports a change, the provider: 1. waits `ReloadDelay`, **250 ms** by default, so a half-written file is less likely to be read; 2. reloads the file into its key dictionary; 3. calls `OnReload()`, which fires and replaces its change token. The configuration root listens to every provider's token and fires its own, the one returned by `IConfiguration.GetReloadToken()`. ## What the default builder turns on `WebApplication.CreateBuilder` reads the host setting `hostBuilder:reloadConfigOnChange` (default `true`) and passes it to every file source it registers: | Source | Reloads while running? | |---|---| | `appsettings.json`, `appsettings.{Environment}.json` | Yes, by default | | `{ApplicationName}.settings*.json` | Yes, by default | | User secrets (Development) | Yes, by default | | Environment variables | **No**, enumerated once at load | | Command-line arguments | **No**, parsed once at load | | Your own `AddInMemoryCollection` | No, unless your code calls `Reload()` | To turn file watching off, for example on a read-only image where watchers only cost handles, supply the switch through an early source such as `DOTNET_hostBuilder__reloadConfigOnChange=false` or `--hostBuilder:reloadConfigOnChange=false`. ## Who sees the new value - **Every read through `IConfiguration`** sees the new value, because the indexer asks the providers each time. - **Values copied at startup** do not change: a `bool` read in `Program.cs` and captured by a lambda, or a constructor argument stored in a singleton, keeps the startup value. - **Typed options** follow their own rules: some options interfaces recompute after a reload and one never does; that belongs to the options pattern. ## Observing a change `IChangeToken` fires once. `ChangeToken.OnChange` re-subscribes for you: ```csharp ChangeToken.OnChange( () => app.Configuration.GetReloadToken(), () => logger.LogInformation("Video meet-and-greet is now {On}", app.Configuration.GetValue<bool>("Features:VideoMeetAndGreet"))); ``` Editors and file systems often raise several events for one save, so the callback can run more than once. Make it idempotent and cheap. ## When reload goes wrong - **Containers and network shares.** File-system events can be missed on mounted volumes and network file systems. Setting `DOTNET_USE_POLLING_FILE_WATCHER=true` makes `PhysicalFileProvider` poll instead, every **4 seconds**. - **A broken save.** If the file is deleted or no longer parses during a reload, the provider drops all of its keys, and lookups fall back to lower sources until a good file returns. A half-edited production file can therefore flip feature switches back to the base file's values. - **Expecting a variable to reload.** Changing a container's environment requires a new process; no setting makes the variables provider watch. ## Cost of watching Each file registered with `reloadOnChange` holds a watcher for the life of the process. On Linux, file watchers draw on a per-user inotify allowance, and hosts that run many .NET processes, or test runs that build many hosts, can exhaust it and fail at startup. Turning reload off with `hostBuilder:reloadConfigOnChange=false`, or switching to the polling watcher, removes that pressure. Immutable container images, where nobody edits files in place, gain nothing from watchers anyway. ## Interview checklist - Default on for JSON and user-secrets files, controlled by `hostBuilder:reloadConfigOnChange`. - Watcher, 250 ms delay, `OnReload`, then the root's reload token. - Variables and arguments never reload. - `ChangeToken.OnChange` for callbacks; polling watcher for mounted volumes.
- A feature switch was read into a bool in Program.cs. After editing appsettings.json, the endpoint still sees the old value. Why?The reload updated the provider, but the bool was copied once at startup and captured by the endpoint. Only reads that go through `IConfiguration` each time, or options interfaces that recompute on reload, see the new value.
- Why might reload never fire for a settings file mounted into a container?The default watcher relies on file-system events, which can be missed for mounted volumes and network file systems. Setting `DOTNET_USE_POLLING_FILE_WATCHER=true` makes the physical file provider poll every 4 seconds instead.
Reload is a shop's chalkboard menu: staff can rub out a price and the next customer who reads the board sees the new one, but a customer who copied the price onto a receipt an hour ago keeps the old number.
- Setting reloadOnChange makes environment variable changes apply without a restart.
- reloadOnChange is off by default, so appsettings edits always need a redeploy.
- Every value read at startup is updated in place when the file reloads.
- A malformed edit during reload is ignored and the previous values stay in effect.
- The reload token fires exactly once per saved edit.