- Home
- Skills
- Aaronontheweb
- Dotnet Skills
- Microsoft Extensions Configuration
microsoft-extensions-configuration_skill
- Shell
643
GitHub Stars
2
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill aaronontheweb/dotnet-skills --skill microsoft-extensions-configuration- advanced-patterns.md9.2 KB
- SKILL.md9.4 KB
Overview
This skill demonstrates Microsoft.Extensions.Options configuration patterns for .NET: strongly-typed settings, startup validation, IValidateOptions, named options, post-configuration, and the IOptions lifetimes. It focuses on fail-fast validation, maintainable configuration classes, and patterns that make settings testable and safe in production. The guidance is practical and ready to apply in Program.cs and DI registration.
How this skill works
It shows how to bind configuration sections to POCO classes and run validation at startup using Data Annotations and custom IValidateOptions<T> validators. Validators can be registered in DI, use other services or environment info, and run together with PostConfigure logic. The skill also explains named options, when to use IOptions/IOptionsSnapshot/IOptionsMonitor, and how to react to configuration changes.
When to use it
- Bind appsettings.json or environment configuration to strongly-typed classes
- Fail fast on invalid configuration during application startup
- Implement cross-property, conditional, or environment-aware validation
- Register multiple instances of the same settings type (named options)
- Use IOptionsMonitor for background services that need runtime updates
Best practices
- Always call ValidateOnStart() for critical settings to surface errors immediately
- Prefer Data Annotations for simple checks and IValidateOptions<T> for cross-field or external checks
- Register validators in DI so they can take dependencies like ILogger or IHostEnvironment
- Use PostConfigure to normalize or set defaults after binding but before validation
- Choose the correct options interface: IOptions for static, IOptionsSnapshot for per-request, IOptionsMonitor for change notifications
Example use cases
- SmtpSettings with required Host, port range, and conditional password rules validated at startup
- Database connection settings with environment-specific checks (no localhost in production) using validators with DI
- Named DatabaseSettings for Primary and Replica, consumed via IOptionsSnapshot and validated separately
- Background worker reading WorkerSettings via IOptionsMonitor and subscribing to OnChange for live updates
- ApiSettings PostConfigure to ensure BaseUrl formatting and set environment-based defaults
FAQ
ValidateOnStart forces option validation during host startup so the app fails fast with clear messages instead of throwing later when a service first accesses options.
When should I use IValidateOptions instead of Data Annotations?
Use IValidateOptions for cross-property rules, conditional logic, external checks, custom error aggregation, or when the validator needs DI services; use Data Annotations for simple attribute-based rules.