.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:

  1. 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.
  2. 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));
  1. 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

OptionTypeRequiredDescription
ApplicationstringYesThe application id or alternate id
EnvironmentstringYesThe environment identifier for the Hyphen project (project environment ID or alternate ID).
HorizonUrlsstring[]NoHyphen Horizon URLs for fetching flags.
EnableToggleUsagebool?NoEnable/disable telemetry (default: true).
CacheCacheOptionsNoConfiguration for caching evaluations.

Cache Configuration

The Cache option accepts the following properties:

PropertyTypeDefaultDescription
TtlSecondsnumber300Time-to-live in seconds for cached flag evaluations.
GenerateCacheKeyFnFunction-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.

FieldTypeRequiredDescription
TargetingKeystringYesCaching evaluation key.
IpAddressstringNoUser's IP address.
CustomAttributesDictionary<string, object>NoAdditional context information.
UserUserContextNoUser-specific information.
User.IdstringNoUnique identifier of the user.
User.EmailstringNoEmail address of the user.
User.NamestringNoName of the user.
User.CustomAttributesDictionary<string, object>NoCustom attributes specific to the user.