.NET has three ways to read options: IOptions<T>, IOptionsSnapshot<T>, and IOptionsMonitor<T>. Their APIs look deceptively similar. However, their lifetimes and caching behavior differ a lot. We had a support case recently where a customer was trying to build a system where configuration changes could be made (and picked up) at runtime, but the application kept using the old values.
Short version: inject IOptions<T> for values that never change at runtime, IOptionsSnapshot<T> in request-scoped code to get fresh values per request, and IOptionsMonitor<T> in singletons and background services that need to see changes.
So, what are your options? (pun!) Let’s look at what each interface does, and why the one you pick depends on where it is injected.
The examples share the same SMTP configuration:
public class SmtpOptions
{
public string Host { get; set; } = "localhost";
public int Port { get; set; } = 587;
public string SenderAddress { get; set; } = "";
public int BatchSize { get; set; } = 100;
}
// Program.cs
builder.Services.Configure<SmtpOptions>(
builder.Configuration.GetSection("Smtp"));The type requested in a constructor determines when the value is created and whether it can change.
IOptions: resolved once, cached forever
IOptions<T> is registered as a singleton. The value behind .Value is created on first access and remains cached until the process exits, regardless of request boundaries or configuration reloads.
public class SmtpClientFactory
{
private readonly string _host;
private readonly int _port;
public SmtpClientFactory(IOptions<SmtpOptions> options)
{
// Bound once, on first resolve. This value will never change again.
_host = options.Value.Host;
_port = options.Value.Port;
}
public SmtpClient Create() => new(_host, _port);
}That lifetime is fine (good, even) for values fixed while the process runs, such as the host and port used to create an SMTP client. It is a poor fit for configuration expected to change at runtime.
IOptionsSnapshot: fresh per scope
IOptionsSnapshot<T> is registered as scoped, which in ASP.NET Core means one instance per HTTP request. The options are re-bound from configuration the first time you touch them within a scope, then cached for the rest of that scope so repeated reads inside a single request stay consistent.
public class EmailPreviewController : ControllerBase
{
private readonly IOptionsSnapshot<SmtpOptions> _options;
public EmailPreviewController(IOptionsSnapshot<SmtpOptions> options)
{
_options = options;
}
[HttpPost]
public IActionResult Preview(EmailDraft draft)
{
// Re-read at the start of every request, stable within this request.
return Ok(RenderPreview(draft, _options.Value.SenderAddress));
}
}For request-scoped code, a snapshot keeps the sender address consistent while the preview renders. A later request can pick up a changed value without restarting the application.
Beware of captive dependencies…
When a singleton depends on IOptionsSnapshot<T>, the container resolves the snapshot from the root scope and keeps that instance:
// Registered as a singleton...
builder.Services.AddSingleton<MailQueueProcessor>();
public class MailQueueProcessor
{
// ...but this dependency is scoped. It gets captured and never refreshes.
public MailQueueProcessor(IOptionsSnapshot<SmtpOptions> options) { }
}This captive dependency traps a shorter-lived service inside a longer-lived one. The snapshot machinery remains, but the freshness is gone because it’s never recreated.
The service provider’s scope validation catches this mismatch. In WebApplicationBuilder, it should be enabled for the Development environment by default:
builder.Host.UseDefaultServiceProvider(options =>
{
options.ValidateScopes = true;
options.ValidateOnBuild = true;
});When enabled, scope validation turns the mismatch into a startup error instead of letting it surface in production:
Cannot consume scoped service 'Microsoft.Extensions.Options.IOptionsSnapshot`1[SmtpOptions]' from singleton 'MailQueueProcessor'.If you see this error, switch the singleton to IOptionsMonitor<T>. Enabling scope validation outside Development catches the same mistake there.
IOptionsMonitor: singleton with change tracking
IOptionsMonitor<T> fits singletons and background services. The monitor is a singleton, but a configuration change invalidates the cached reference behind CurrentValue:
public class MailQueueProcessor : BackgroundService
{
private readonly IOptionsMonitor<SmtpOptions> _options;
public MailQueueProcessor(IOptionsMonitor<SmtpOptions> options)
{
_options = options;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Read fresh on every iteration, no restart needed.
var batchSize = _options.CurrentValue.BatchSize;
var messages = await DequeueBatchAsync(batchSize, stoppingToken);
await DeliverBatchAsync(messages, stoppingToken);
}
}
}The OnChange callback handles configuration changes that require work, such as reconnecting to an SMTP server:
public class SmtpConnection : IDisposable
{
private readonly IDisposable? _subscription;
private SmtpOptions _current;
public SmtpConnection(IOptionsMonitor<SmtpOptions> options)
{
_current = options.CurrentValue;
Connect(_current.Host, _current.Port);
_subscription = options.OnChange(updated =>
{
if (updated.Host != _current.Host || updated.Port != _current.Port)
{
Reconnect(updated.Host, updated.Port);
}
_current = updated;
});
}
public void Dispose() => _subscription?.Dispose();
}OnChange returns an IDisposable. Without disposal, the callback stays registered for the monitor’s lifetime, which is the lifetime of the application for a singleton monitor.
OnChange fires more than once
A single editor save can produce several OnChange notifications. An editor may replace a temporary file or write the file in multiple passes, and the underlying FileSystemWatcher reports those operations separately. One human edit can therefore invoke the callback two or three times.
Duplicate notifications are harmless when the callback only updates a field, but depending on what you are building, or when expensive work is needed to handle a configuration update, you may want to debounce the event.
Named options
Two configurations of the same type can represent a primary and backup SMTP server. Named registrations avoid separate PrimarySmtpOptions and BackupSmtpOptions classes that differ only in name:
builder.Services.Configure<SmtpOptions>("Primary",
builder.Configuration.GetSection("Smtp:Primary"));
builder.Services.Configure<SmtpOptions>("Backup",
builder.Configuration.GetSection("Smtp:Backup"));The named lookup is Get(string name), available on IOptionsSnapshot<T> and IOptionsMonitor<T>:
public class MailSender
{
private readonly IOptionsMonitor<SmtpOptions> _options;
public MailSender(IOptionsMonitor<SmtpOptions> options)
{
_options = options;
}
public async Task SendAsync(MailMessage message)
{
try
{
await DeliverAsync(message, _options.Get("Primary"));
}
catch (SmtpException)
{
await DeliverAsync(message, _options.Get("Backup"));
}
}
}IOptions<T> has no Get method, so named options require a snapshot or monitor. The unnamed registration from a plain Configure<T> uses Options.DefaultName (an empty string), which makes options.Get("") equivalent to options.Value.
Named options also fit multi-tenant applications, with one set of options resolved by each tenant identifier:
foreach (var tenant in tenants)
{
builder.Services.Configure<SmtpOptions>(
tenant.Id,
builder.Configuration.GetSection($"Tenants:{tenant.Id}:Smtp"));
}
// Later, in a request-scoped service:
SmtpOptions options = _snapshot.Get(currentTenant.Id);IOptions vs IOptionsSnapshot vs IOptionsMonitor: which one do you pick?
The consuming service’s lifetime usually settles the choice. In request-scoped code, IOptionsSnapshot<T> can pick up changes between requests. Longer-lived services use IOptionsMonitor<T> when values can change; otherwise, IOptions<T> keeps the value pinned for the process lifetime.
Keep in mind that IOptionsSnapshot<T> does not belong in a singleton. IOptionsMonitor<T> can be used there instead when a singleton has to observe changes.
Enjoy!