OpenFeature.Hosting 2.14.1

OpenFeature.Hosting

NuGet Specification

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.

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