⚙️ Fix an Options Reload That Skips Its Own Validator
Registering options with ValidateDataAnnotations() or a custom IValidateOptions<T> correctly checks the very first bind, but a long-lived singleton that resolved IOptionsMonitor<T> once at startup and keeps reading its CurrentValue later never re-triggers that validation on a later configuration reload unless the code explicitly listens for the change and re-runs the check itself – so an operator pushing an invalid value at runtime can have it take effect silently, with no validation failure logged anywhere.
⚙️ The Problem
builder.Services.AddOptions<ThrottleSettings>()
.Bind(builder.Configuration.GetSection("Throttle"))
.ValidateDataAnnotations(); // validated once, on first resolution
// A singleton resolves IOptionsMonitor<ThrottleSettings> at startup
// and reads .CurrentValue later - a later config reload updates the
// snapshot it returns, but nothing re-runs ValidateDataAnnotations
// against that new snapshot automatically.
public class ThrottleService(IOptionsMonitor<ThrottleSettings> opts)
{
public int Limit => opts.CurrentValue.MaxRequests; // never re-validated
}
✅ Fix: Re-Validate Explicitly on Every Reload a Singleton Cares About
- Call ValidateOnStart() when registering the options to at least guarantee the very first bind is checked at boot, while remembering this still doesn’t cover every later reload by itself.
- For options a singleton reads repeatedly over the app’s lifetime, subscribe to IOptionsMonitor<T>.OnChange and explicitly re-run the same validation logic inside that callback, rejecting and logging a reload that fails validation instead of letting CurrentValue silently return the invalid snapshot.
- Add an integration test that writes a deliberately invalid value to the configuration source after the app has already started, and assert the running app either rejects it or falls back safely, since a test that only checks startup-time validation won’t catch this gap at all.
🚨 Why This Is Dangerous
- A configuration value that passes validation at deploy time but gets hot-reloaded to an invalid value later – a bad feature-flag edit, a mistyped connection string – can take effect in a running production app with nothing in the validation pipeline ever firing to catch it.
Options validation that only runs once isn’t protecting the value your app is using right now – it’s protecting the one it started with.




