Microsoft.Extensions.Options.Contextual is a little-known, still-experimental .NET package that allows configuring options objects dynamically based on a runtime context object. The setup involves marking a context class with [OptionsContext] to trigger a source generator, implementing an IOptionsContextReceiver to extract relevant values, and wiring up a contextual configuration lambda. The author walks through the full setup with a weather forecast example, then questions the value of the abstraction — noting it introduces indirect coupling via magic strings rather than eliminating coupling. The package appears to have been conceived as a feature-flagging mechanism (with LaunchDarkly mentioned in API review notes), but it never left preview, has under 1000 downloads across all versions, and competes with the already-popular Microsoft.FeatureManagement. The author concludes the package is not worth adopting and suggests OpenFeature as an alternative for feature flagging needs.

12m read timeFrom andrewlock.net
Post cover image
Table of contents
What is Microsoft.Extensions.Options.Contextual?Configuring options based on another typeAdding the Microsoft.Extensions.Options.Contextual packageConfiguring the contextual optionsSo, what's this for again?Feature flags, theoreticallyDefinitely experimentalSummary

Sort: