This project demonstrates how to use IOptionsMonitor<T> in ASP.NET Core to access strongly-typed configuration that can be observed for changes at runtime. It shows the .NET Options pattern for binding JSON configuration sections to C# classes and injecting them into minimal API endpoints.
The application creates a minimal ASP.NET Core Web API with a single GET /hello endpoint that reads from a SettingsOptions class bound to the Settings section in appsettings.json. The IOptionsMonitor<SettingsOptions> is injected directly into the route handler, and its CurrentValue property provides real-time access to configuration values including StringProperty and IntegerProperty. Unlike IOptions<T>, IOptionsMonitor<T> supports live reloading of configuration changes without restarting the application, making it ideal for long-lived services and background tasks.
This is a focused educational example of Microsoft's recommended configuration management approach in modern .NET 8 applications.
- .NET 8.0 SDK or later
- An IDE such as Visual Studio, Visual Studio Code, or JetBrains Rider
dotnet run --project OptionsMonitorExampleThen navigate to https://localhost:<port>/hello or use the included .http file to test the endpoint.
If you found this useful and you want to learn more about C#, .NET, and software engineering, subscribe to the free Dev Leader Weekly newsletter:
Subscribe to Dev Leader Weekly
- All Links
- Website - Dev Leader
- YouTube - Dev Leader
- YouTube - Dev Leader Path To Tech
- YouTube - Dev Leader Podcast
- YouTube - CodeCommute
- Newsletter - Dev Leader Weekly
- LinkedIn - Nick Cosentino
- GitHub - ncosentino
- Twitter/X - Dev Leader
- Threads - Dev Leader
- Bluesky - Dev Leader
- Mastodon - Dev Leader
- Facebook - Dev Leader
- TikTok - Dev Leader
- Twitch - Dev Leader
- Stack Overflow - Nick Cosentino
Powered by BrandGhost 👻