ASP.NET Core MVC Interview Question #133
What is the difference between IOptions, IOptionsSnapshot, and IOptionsMonitor?
Dependency Injection, Services, Error Handling, Logging & Performance Senior Advanced
Quick Interview Answer
IOptions provides a straightforward singleton-style access to configured option values. IOptionsSnapshot is scoped and can provide updated values per request for supported configuration providers. IOptionsMonitor is singleton-friendly and supports retrieving current values and change notifications.
Detailed Explanation
IOptions provides a straightforward singleton-style access to configured option values. IOptionsSnapshot is scoped and can provide updated values per request for supported configuration providers. IOptionsMonitor is singleton-friendly and supports retrieving current values and change notifications.
Choose the interface according to the service lifetime and whether configuration reload behavior is required. Do not assume that every configuration source supports runtime reload.
Code Example
public class ThemeService
{
private readonly IOptionsSnapshot<ThemeOptions> _options;
public ThemeService(
IOptionsSnapshot<ThemeOptions> options)
{
_options = options;
}
}