.NET
Hyphen Toggle OpenFeature Provider for .NET is an OpenFeature provider implementation that enables seamless feature flag evaluation in .NET applications. This provider integrates Hyphen's feature flagging system with the OpenFeature SDK, providing robust feature management with minimal setup.
Installation
Install the provider and OpenFeature using NuGet:
dotnet add package Hyphen.OpenFeature.Provider
dotnet add package OpenFeature
Setup and Initialization
To integrate the Hyphen Toggle provider into your application, follow these steps:
-
Configure the provider with your
publicKey
, application name and environment.
You can specify the environment in one of two formats:- Alternate ID (e.g., "production", "staging") — the environment in which your application is running.
- Project Environment ID (e.g.,
pevr_abc123
) — useful for internal references.
-
Register the provider with OpenFeature.
using OpenFeature;
using Hyphen.OpenFeature.Provider;
var publicKey = "your-public-key-here";
var options = new HyphenProviderOptions
{
Application = "your-application-name",
Environment = "production" // or project environment ID
};
await OpenFeature.SetProviderAsync(new HyphenProvider(publicKey, options));
- Configure the context needed for feature targeting evaluations, incorporating user or application context.
HyphenEvaluationContext hyphenEvaluationContext = new HyphenEvaluationContext
{
TargetingKey = "user-123",
IpAddress = "203.0.113.42",
CustomAttributes = new Dictionary<string, object>
{
{ "subscriptionLevel", "premium" },
{ "region", "us-east" }
},
User = new UserContext
{
Id = "user-123",
Email = "[email protected]",
Name = "John Doe",
CustomAttributes = new Dictionary<string, object>
{
{ "role", "admin" }
}
}
};
EvaluationContext context = hyphenEvaluationContext.GetEvaluationContext();
Usage
Evaluation Context Example
Evaluate a feature flag using the OpenFeature client and context information:
var client = OpenFeature.GetClient();
var flagValue = await client.GetBooleanValue("feature-flag-key", false, context);
Configuration
Options
Option | Type | Required | Description |
---|---|---|---|
Application | string | Yes | The application id or alternate id |
Environment | string | Yes | The environment identifier for the Hyphen project (project environment ID or alternate ID). |
HorizonUrls | string[] | No | Hyphen Horizon URLs for fetching flags. |
EnableToggleUsage | bool? | No | Enable/disable telemetry (default: true). |
Cache | CacheOptions | No | Configuration for caching evaluations. |
Cache Configuration
The Cache
option accepts the following properties:
Property | Type | Default | Description |
---|---|---|---|
TtlSeconds | number | 300 | Time-to-live in seconds for cached flag evaluations. |
GenerateCacheKeyFn | Function | - | Custom function to generate cache keys from evaluation context. |
Example with cache configuration:
var options = new HyphenProviderOptions
{
Application = "your-application-name",
Environment = "production",
Cache = new CacheOptions
{
TtlSeconds = 600, // 10 minutes
GenerateCacheKeyFn = (context) => $"{context.TargetingKey}-{context.User?.Id}"
}
};
Context
Provide an EvaluationContext
to pass contextual data for feature evaluation.
Field | Type | Required | Description |
---|---|---|---|
TargetingKey | string | Yes | Caching evaluation key. |
IpAddress | string | No | User's IP address. |
CustomAttributes | Dictionary<string, object> | No | Additional context information. |
User | UserContext | No | User-specific information. |
User.Id | string | No | Unique identifier of the user. |
User.Email | string | No | Email address of the user. |
User.Name | string | No | Name of the user. |
User.CustomAttributes | Dictionary<string, object> | No | Custom attributes specific to the user. |
Updated 16 days ago