Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Tim Corey
10m 39s
Configuration management is an essential aspect of building scalable and maintainable applications in C#. One of the most effective ways to handle configuration settings is by using the Options Pattern. In his video, The Options Pattern in C# in 10 Minutes or Less, Tim Corey provides a concise yet insightful explanation of this pattern and how to implement it efficiently. In this article, we will explore the Options Pattern through Tim's explanations and demonstrations from his video.
Tim begins by introducing the Options Pattern as a powerful yet simple way to retrieve information from configuration files and inject it into dependency injection (DI). He highlights several advantages of this pattern, such as:
Tim emphasizes that while he usually provides in-depth technical training, this video is designed as a quick-start guide to implementing the Options Pattern.
Tim starts by opening Visual Studio with a Blazor Web App (server-side rendering only). He has already made a few modifications to speed up the demonstration. The key setup involves:
appsettings.json file with a CloudInfo section containing three key-value pairs.CloudInfoOptions, which maps to these configuration values.The appsettings.json file contains the following section:
{
"CloudInfo": {
"Storage": "Azure Storage",
"Website": "Azure Static Web Apps",
"API": "Azure Web App"
}
}
Tim clarifies that while he is using appsettings.json, the configuration values could also come from:
appsettings.Development.jsonsecrets.jsonNow, Tim explains how to bind the configuration values to the CloudInfoOptions class and inject them into DI.
In Program.cs, Tim adds the following line inside builder.Services:
builder.Services.Configure<CloudInfoOptions>(builder.Configuration.GetSection("CloudInfo"));builder.Services.Configure(Of CloudInfoOptions)(builder.Configuration.GetSection("CloudInfo"))This binds the CloudInfo section of appsettings.json to the CloudInfoOptions class. Tim notes that this step ensures the values are available in dependency injection.
Tim moves to the Home Page component (Index.razor) and modifies it to display configuration values dynamically.
He injects the IOptions interface into the component:
@inject IOptions<CloudInfoOptions> CloudInfoOptions
To retrieve the actual values, he assigns them inside OnInitialized:
protected override void OnInitialized()
{
CloudInfo = CloudInfoOptions.Value;
}Protected Overrides Sub OnInitialized()
CloudInfo = CloudInfoOptions.Value
End SubTim explains that IOptions<T> provides a Singleton instance of the configuration values, meaning it remains constant throughout the application's lifecycle. When the application runs, the configuration values correctly appear on the page.
Tim then introduces three different ways to use the Options Pattern:
By default, IOptions<T> provides a Singleton instance. Tim demonstrates that if the configuration file is updated while the app is running, the changes do not reflect unless the application is restarted.
IOptionsSnapshot<T>To enable per-request updates, Tim modifies the injection to:
@inject IOptionsSnapshot<CloudInfoOptions> CloudInfoOptions
This ensures that every new HTTP request gets fresh configuration values. Tim tests this by modifying appsettings.json, refreshing the page, and showing that the updated values now appear immediately.
He explains that IOptionsSnapshot<T> is useful for scenarios where changes need to be reflected without restarting the app but do not require live monitoring.
IOptionsMonitor<T>Finally, Tim introduces IOptionsMonitor<T>, which enables real-time change detection. He updates the injection to:
@inject IOptionsMonitor<CloudInfoOptions> CloudInfoOptions
Unlike IOptionsSnapshot, which updates per request, IOptionsMonitor<T> can trigger events when configuration values change. This is particularly useful when you need to react to configuration changes dynamically.
Tim notes that IOptionsMonitor<T> is backed by a singleton but can refresh values dynamically when the underlying configuration file changes.
Tim wraps up the video by summarizing the key differences between the three approaches:
| Interface | Lifetime | Updates on Change |
|---|---|---|
IOptions<T> | Singleton | No |
IOptionsSnapshot<T> | Scoped | Per Request |
IOptionsMonitor<T> | Singleton | Yes |
He emphasizes that while the Options Pattern simplifies configuration management, understanding these different variations helps in choosing the right approach based on project requirements.
By following Tim's approach, developers can efficiently manage application settings, ensure smooth dependency injection, and handle runtime configuration changes effectively. For better understanding, please watch the complete video and do visit his channel for more insightful videos on C#.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy