ASP.NET Core Interview Question #31

What is the difference between IOptions, IOptionsSnapshot, and IOptionsMonitor?

Application Structure & Configuration Junior Beginner

Quick Interview Answer

IOptions<T> provides a singleton-style options value and is suitable when settings do not need to change while the application is running.

Detailed Explanation

IOptions<T> provides a singleton-style options value and is suitable when settings do not need to change while the application is running.

IOptionsSnapshot<T> is scoped and recalculates options once per request. It is useful in request-based applications when configuration may reload between requests.

IOptionsMonitor<T> is a singleton service that can provide the current value and notify consumers when configuration changes. It is useful for singleton services or applications that need to react to updated settings.

The correct choice depends on the consumer's lifetime and whether runtime configuration reload is required.

Code Example

public MyService(IOptions<MySettings> options) { }
public MyScopedService(IOptionsSnapshot<MySettings> options) { }
public MyMonitorService(IOptionsMonitor<MySettings> options)
{
    options.OnChange(settings =>
    {
        // React to updated settings.
    });
}