OpenFeature.Hosting 2.14.1
OpenFeature.Hosting
OpenFeature.Hosting is an extension for the OpenFeature .NET SDK that streamlines integration with .NET applications using dependency injection and hosting. It enables seamless configuration and lifecycle management of feature flag providers, hooks, and evaluation context using idiomatic .NET patterns.
๐งช The OpenFeature.Hosting package is still considered experimental and may undergo significant changes. Feedback and contributions are welcome!
๐ Quick Start
Requirements
- .NET 8+
- .NET Framework 4.6.2+
Installation
Add the package to your project:
dotnet add package OpenFeature.Hosting
Basic Usage
Register OpenFeature in your application's dependency injection container (e.g., in Program.cs for ASP.NET Core):
builder.Services.AddOpenFeature(featureBuilder => {
featureBuilder
.AddInMemoryProvider();
});
You can add global evaluation context, hooks, and event handlers as needed:
builder.Services.AddOpenFeature(featureBuilder => {
featureBuilder
.AddContext((contextBuilder, serviceProvider) => {
// Custom context configuration
})
.AddHook<LoggingHook>()
.AddHandler(ProviderEventTypes.ProviderReady, (eventDetails) => {
// Handle provider ready event
});
});
Domain-Scoped Providers
To register multiple providers and select a default provider by domain:
builder.Services.AddOpenFeature(featureBuilder => {
featureBuilder
.AddInMemoryProvider("default")
.AddInMemoryProvider("beta")
.AddPolicyName(options => {
options.DefaultNameSelector = serviceProvider => "default";
});
});
Registering a Custom Provider
You can register a custom provider using a factory:
builder.Services.AddOpenFeature(featureBuilder => {
featureBuilder.AddProvider(provider => {
// Resolve services or configuration as needed
return new MyCustomProvider();
});
});
๐งฉ Features
- Dependency Injection: Register providers, hooks, and context using the .NET DI container.
- Domain Support: Assign providers to logical domains for multi-tenancy or environment separation.
- Event Handlers: React to provider lifecycle events (e.g., readiness).
- Extensibility: Add custom hooks, context, and providers.
๐ ๏ธ Example: ASP.NET Core Integration
Below is a simple example of integrating OpenFeature with an ASP.NET Core application using an in-memory provider and a logging hook.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenFeature(featureBuilder => {
featureBuilder
.AddInMemoryProvider()
.AddHook<LoggingHook>();
});
var app = builder.Build();
app.MapGet("/", async (IFeatureClient client) => {
bool enabled = await client.GetBooleanValueAsync("my-flag", false);
return enabled ? "Feature enabled!" : "Feature disabled.";
});
app.Run();
If you have multiple providers registered, you can specify which client and provider to resolve by using the FromKeyedServices attribute:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenFeature(featureBuilder => {
featureBuilder
.AddInMemoryProvider("default")
.AddInMemoryProvider("beta")
.AddPolicyName(options => {
options.DefaultNameSelector = serviceProvider => "default";
});
});
var app = builder.Build();
app.MapGet("/", async ([FromKeyedServices("beta")] IFeatureClient client) => {
bool enabled = await client.GetBooleanValueAsync("my-flag", false);
return enabled ? "Feature enabled!" : "Feature disabled.";
});
app.Run();
๐ Lifecycle Management
AddOpenFeature registers a hosted service that initializes the configured providers on application startup and shuts them down on application exit. By default it hooks into the IHostedLifecycleService callbacks (StartingAsync/StoppedAsync), which are supported by the .NET generic host (WebApplication.CreateBuilder, Host.CreateDefaultBuilder).
Hosts that only support IHostedService โ such as the legacy ASP.NET Core WebHost/WebHostBuilder โ never invoke those callbacks. In that case the hosted service automatically falls back to StartAsync/StopAsync, so providers are still initialized and shut down without any extra configuration. Note that on such hosts the fallback preserves lifecycle execution, not the exact timing of the configured Starting/Started/Stopping/Stopped callbacks: initialization and shutdown always run within StartAsync and StopAsync.
You can also control which lifecycle callbacks are used explicitly:
builder.Services.Configure<FeatureLifecycleStateOptions>(options => {
options.StartState = FeatureStartState.Start;
options.StopState = FeatureStopState.Stop;
});
๐ Further Reading
๐ค Contributing
Contributions are welcome! See the CONTRIBUTING guide for details.
No packages depend on OpenFeature.Hosting.
.NET 10.0
- OpenFeature (>= 2.14.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
.NET Framework 4.6.2
- OpenFeature (>= 2.14.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- System.Collections.Immutable (>= 8.0.0)
- System.Threading.Channels (>= 8.0.0)
- System.ValueTuple (>= 4.6.1)
.NET 8.0
- OpenFeature (>= 2.14.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
.NET 9.0
- OpenFeature (>= 2.14.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
.NET Standard 2.0
- OpenFeature (>= 2.14.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- System.Collections.Immutable (>= 8.0.0)
- System.Threading.Channels (>= 8.0.0)
| Version | Downloads | Last updated |
|---|---|---|
| 2.14.1 | 1 | 08/22/2026 |
| 2.14.0 | 0 | 06/22/2026 |
| 2.13.0 | 0 | 04/30/2026 |
| 2.12.0 | 1 | 08/22/2026 |
| 2.11.1 | 0 | 01/08/2026 |
| 2.11.0 | 0 | 12/18/2025 |
| 2.10.0 | 0 | 12/01/2025 |
| 2.9.0 | 0 | 10/16/2025 |
| 2.8.1 | 0 | 07/31/2025 |
| 2.8.0 | 0 | 07/30/2025 |
| 2.7.0 | 0 | 07/03/2025 |
| 2.6.0 | 0 | 05/23/2025 |
| 2.5.0 | 1 | 08/21/2026 |
| 2.4.0 | 0 | 04/14/2025 |
| 2.3.2 | 0 | 03/27/2025 |
| 2.3.1 | 0 | 02/04/2025 |
| 2.3.0 | 0 | 01/31/2025 |
| 2.2.0 | 0 | 12/12/2024 |
| 2.1.0 | 0 | 11/18/2024 |