Microsoft.Extensions.Options.Contextual
10.10.0-preview.1.26459.2
Prefix Reserved
dotnet add package Microsoft.Extensions.Options.Contextual --version 10.10.0-preview.1.26459.2
NuGet\Install-Package Microsoft.Extensions.Options.Contextual -Version 10.10.0-preview.1.26459.2
<PackageReference Include="Microsoft.Extensions.Options.Contextual" Version="10.10.0-preview.1.26459.2" />
<PackageVersion Include="Microsoft.Extensions.Options.Contextual" Version="10.10.0-preview.1.26459.2" />
<PackageReference Include="Microsoft.Extensions.Options.Contextual" />
paket add Microsoft.Extensions.Options.Contextual --version 10.10.0-preview.1.26459.2
#r "nuget: Microsoft.Extensions.Options.Contextual, 10.10.0-preview.1.26459.2"
#:package Microsoft.Extensions.Options.Contextual@10.10.0-preview.1.26459.2
#addin nuget:?package=Microsoft.Extensions.Options.Contextual&version=10.10.0-preview.1.26459.2&prerelease
#tool nuget:?package=Microsoft.Extensions.Options.Contextual&version=10.10.0-preview.1.26459.2&prerelease
Microsoft.Extensions.Options.Contextual
APIs for dynamically configuring options based on a given context.
Install the package
From the command-line:
dotnet add package Microsoft.Extensions.Options.Contextual
Or directly in the C# project file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options.Contextual" Version="[CURRENTVERSION]" />
</ItemGroup>
Usage Example
Start with an option type.
internal class WeatherForecastOptions
{
public string TemperatureScale { get; set; } = "Celsius"; // Celsius or Fahrenheit
public int ForecastDays { get; set; }
}
Define a context and a receiver that will be used as inputs to dynamically configure the options.
[OptionsContext]
internal partial class WeatherForecastContext // Note class must be partial
{
public Guid UserId { get; set; }
public string? Country { get; set; }
}
internal class CountryContextReceiver : IOptionsContextReceiver
{
public string? Country { get; private set; }
public void Receive<T>(string key, T value)
{
if (key == nameof(Country))
{
Country = value?.ToString();
}
}
}
Create a service that consumes the options for a given context.
internal class WeatherForecast
{
public DateTime Date { get; set; }
public int Temperature { get; set; }
public string TemperatureScale { get; set; } = string.Empty;
}
internal class WeatherForecastService
{
private readonly IContextualOptions<WeatherForecastOptions> _contextualOptions;
private readonly Random _rng = new(0);
public WeatherForecastService(IContextualOptions<WeatherForecastOptions> contextualOptions)
{
_contextualOptions = contextualOptions;
}
public async Task<IEnumerable<WeatherForecast>> GetForecast(WeatherForecastContext context, CancellationToken cancellationToken)
{
WeatherForecastOptions options = await _contextualOptions.GetAsync(context, cancellationToken).ConfigureAwait(false);
return Enumerable.Range(1, options.ForecastDays).Select(index => new WeatherForecast
{
Date = new DateTime(2000, 1, 1).AddDays(index),
Temperature = _rng.Next(-20, 55),
TemperatureScale = options.TemperatureScale,
});
}
}
The options can be configured with both global options (ForecastDays), and options that vary depending on the current context (TemperatureScale).
using var host = FakeHost.CreateBuilder()
.ConfigureServices(services => services
.Configure<WeatherForecastOptions>(options => options.ForecastDays = 7)
.Configure<WeatherForecastOptions>(ConfigureTemperatureScaleBasedOnCountry)
.AddSingleton<WeatherForecastService>())
.Build();
static void ConfigureTemperatureScaleBasedOnCountry(IOptionsContext context, WeatherForecastOptions options)
{
CountryContextReceiver receiver = new();
context.PopulateReceiver(receiver);
if (receiver.Country == "US")
{
options.TemperatureScale = "Fahrenheit";
}
}
And lastly, the service is called with some context.
var forecastService = host.Services.GetRequiredService<WeatherForecastService>();
var usForcast = await forecastService.GetForecast(new WeatherForecastContext { Country = "US" }, CancellationToken.None);
var caForcast = await forecastService.GetForecast(new WeatherForecastContext { Country = "CA" }, CancellationToken.None);
Feedback & Contributing
We welcome feedback and contributions in our GitHub repo.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.2
- Microsoft.Extensions.Options (>= 8.0.2)
- System.Collections.Immutable (>= 8.0.0)
- System.Threading.Tasks.Extensions (>= 4.5.4)
-
net10.0
- Microsoft.Extensions.Options (>= 10.0.12)
-
net8.0
- Microsoft.Extensions.Options (>= 8.0.2)
-
net9.0
- Microsoft.Extensions.Options (>= 9.0.20)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Microsoft.Extensions.Options.Contextual:
| Package | Downloads |
|---|---|
|
Excos.Options
Excos.Options provides a feature/experiment management system on top of Options.Contextual |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.10.0-preview.1.26459.2 | 103 | 9/9/2026 |
| 10.9.0-preview.1.26411.16 | 159 | 8/11/2026 |
| 10.8.0-preview.1.26364.2 | 136 | 7/14/2026 |
| 10.7.0-preview.1.26309.5 | 152 | 6/9/2026 |
| 10.6.0-preview.1.26261.4 | 99 | 5/12/2026 |
| 10.5.0-preview.1.26213.4 | 243 | 4/15/2026 |
| 10.4.0-preview.1.26160.2 | 454 | 3/10/2026 |
| 10.3.0-preview.1.26109.11 | 105 | 2/10/2026 |
| 10.2.0-preview.1.26063.2 | 104 | 1/13/2026 |
| 10.1.0-preview.1.25608.1 | 467 | 12/9/2025 |
| 10.0.0-preview.1.25559.3 | 301 | 11/11/2025 |
| 9.10.0-preview.1.25513.3 | 207 | 10/14/2025 |
| 9.9.0-preview.1.25458.4 | 186 | 9/9/2025 |
| 9.8.0-preview.1.25412.6 | 184 | 8/12/2025 |
| 9.7.0-preview.1.25356.2 | 194 | 7/8/2025 |
| 9.6.0-preview.1.25310.2 | 360 | 6/10/2025 |
| 9.5.0-preview.1.25262.9 | 401 | 5/13/2025 |
| 9.4.0-preview.1.25207.5 | 256 | 4/8/2025 |
| 9.3.0-preview.1.25161.3 | 230 | 3/11/2025 |
| 9.2.0-preview.1.25105.6 | 202 | 2/11/2025 |