Microsoft.Extensions.Options.Contextual
9.0.0-preview.9.24507.7
Prefix Reserved
dotnet add package Microsoft.Extensions.Options.Contextual --version 9.0.0-preview.9.24507.7
NuGet\Install-Package Microsoft.Extensions.Options.Contextual -Version 9.0.0-preview.9.24507.7
<PackageReference Include="Microsoft.Extensions.Options.Contextual" Version="9.0.0-preview.9.24507.7" />
paket add Microsoft.Extensions.Options.Contextual --version 9.0.0-preview.9.24507.7
#r "nuget: Microsoft.Extensions.Options.Contextual, 9.0.0-preview.9.24507.7"
// Install Microsoft.Extensions.Options.Contextual as a Cake Addin #addin nuget:?package=Microsoft.Extensions.Options.Contextual&version=9.0.0-preview.9.24507.7&prerelease // Install Microsoft.Extensions.Options.Contextual as a Cake Tool #tool nuget:?package=Microsoft.Extensions.Options.Contextual&version=9.0.0-preview.9.24507.7&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. |
.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 (>= 9.0.0-rc.2.24473.5)
- System.Collections.Immutable (>= 9.0.0-rc.2.24473.5)
- System.Threading.Tasks.Extensions (>= 4.5.4)
-
net8.0
- Microsoft.Extensions.Options (>= 9.0.0-rc.2.24473.5)
-
net9.0
- Microsoft.Extensions.Options (>= 9.0.0-rc.2.24473.5)
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 |
---|---|---|
9.0.0-preview.9.24507.7 | 150 | 10/8/2024 |
9.0.0-preview.8.24460.1 | 75 | 9/10/2024 |
9.0.0-preview.7.24412.10 | 76 | 8/14/2024 |
9.0.0-preview.6.24353.1 | 136 | 7/10/2024 |
9.0.0-preview.5.24311.7 | 88 | 6/11/2024 |
9.0.0-preview.4.24271.2 | 95 | 5/21/2024 |
9.0.0-preview.3.24209.3 | 104 | 4/11/2024 |
9.0.0-preview.2.24157.4 | 94 | 3/12/2024 |
9.0.0-preview.1.24108.1 | 95 | 2/13/2024 |
8.10.0-rtm.24502.7 | 62 | 10/8/2024 |
8.9.1-rtm.24456.4 | 55 | 9/6/2024 |
8.9.0-rtm.24454.2 | 50 | 9/5/2024 |
8.8.0-rtm.24412.9 | 84 | 8/13/2024 |
8.7.0-rtm.24353.5 | 48 | 7/10/2024 |
8.6.0-rtm.24311.4 | 64 | 6/11/2024 |
8.5.0-rtm.24263.4 | 67 | 5/14/2024 |
8.4.0-rtm.24205.3 | 91 | 4/9/2024 |
8.3.0-rtm.24161.6 | 76 | 3/12/2024 |
8.2.0-rtm.24107.6 | 66 | 2/13/2024 |
8.1.0-rtm.24058.3 | 115 | 1/9/2024 |
8.0.0-rtm.23558.11 | 364 | 11/14/2023 |
8.0.0-rc.2.23510.2 | 110 | 10/10/2023 |
8.0.0-rc.1.23453.1 | 124 | 9/12/2023 |
8.0.0-preview.7.23407.5 | 108 | 8/8/2023 |
8.0.0-preview.6.23360.2 | 89 | 7/12/2023 |
8.0.0-preview.5.23308.3 | 96 | 6/14/2023 |
8.0.0-preview.4.23273.7 | 80 | 5/23/2023 |