Back to results for “Dependency Injection”
.NET › Configuration & Options

What is the difference between IOptions<T>, IOptionsSnapshot<T>, and IOptionsMonitor<T>?

Advanced

Answer

IOptions<T> is registered as a singleton, computed once at first access and never refreshed even if the underlying configuration source changes at runtime -- suitable for genuinely static settings. IOptionsSnapshot<T> is scoped, recomputed once per request/scope, picking up configuration changes on the next request but staying consistent within a single request. IOptionsMonitor<T> is a singleton that can notify subscribers immediately when configuration changes via its OnChange callback, ideal for singleton services (like a BackgroundService) that need to react to live configuration updates.
// IOptionsSnapshot: fresh value per HTTP request
public MyController(IOptionsSnapshot<FeatureSettings> options) { ... }

// IOptionsMonitor: live change notifications in a singleton service
public class MyBackgroundService : BackgroundService {
    public MyBackgroundService(IOptionsMonitor<WorkerSettings> monitor) {
        monitor.OnChange(settings => Console.WriteLine("Settings changed!"));
    }
}
Real-world example A long-running BackgroundService uses IOptionsMonitor to immediately pick up a changed polling interval from a hot-reloadable appsettings.json without needing to restart the entire application, while a typical scoped controller uses IOptionsSnapshot for simpler per-request configuration access.

Common follow-ups: Why can't a singleton service safely use IOptionsSnapshot?;What triggers the OnChange callback in IOptionsMonitor?