Skip to main content
Version: Config V2

OpenFeature Provider for .NET

ConfigCat OpenFeature Provider for .NET on GitHub

Getting started

1. Install the provider

Install-Package OpenFeature.Providers.ConfigCat

2. Initialize the provider

The ConfigCatProvider constructor takes the SDK key and an optional callback that can be used to specify additional configuration options for the ConfigCat .NET SDK:

using System;
using ConfigCat.Client;
using OpenFeature.Providers.ConfigCat;

// Specify options for the ConfigCat SDK.
Action<ConfigCat.Client.Configuration.ConfigCatClientOptions> configureOptions = (options) =>
{
options.PollingMode = PollingModes.AutoPoll(pollInterval: TimeSpan.FromSeconds(60));
options.Logger = new ConsoleLogger(LogLevel.Warning);
// ...
};

// Configure the provider.
await OpenFeature.Api.Instance.SetProviderAsync(new ConfigCatProvider("#YOUR-SDK-KEY#", configureOptions));

// Create a client.
var client = OpenFeature.Api.Instance.GetClient();

For more information about all the configuration options, see the .NET SDK documentation.

3. Evaluate your feature flag

var isAwesomeFeatureEnabled = await client.GetBooleanValueAsync("isAwesomeFeatureEnabled", false);
if (isAwesomeFeatureEnabled)
{
doTheNewThing();
}
else
{
doTheOldThing();
}

4. Cleaning up

On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client.

await OpenFeature.Api.Instance.ShutdownAsync();

Evaluation Context

An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.

The following table shows how the different context attributes are mapped to User Object attributes.

Evaluation contextUser ObjectRequired
targetingKey (Id / Identifier)Identifier
EmailEmail
CountryCountry
Any otherCustom

Remarks:

  • If targetingKey is present in the evaluation context, it will be mapped to the Identifier property. Otherwise, Id or Identifier will be mapped, whichever occurs first. (If none of these keys are present, the Identifier property will be set to the fallback value "<n/a>".)
  • The keys Id, Identifier, Email and Country are matched case-insensitively. If the same key appears in multiple cases - e.g. email and Email - the first occurrence will be mapped to the corresponding property.
  • All of the above will also be included in the Custom dictionary (except for the exact keys Identifier, Email and Country). This allows them to be referenced using their original names in feature flag rules.
  • Other keys are mapped as custom user attributes with their values unchanged. (Although the ConfigCat SDK handles value conversion internally, it's recommended to use the type expected by the referencing feature flag rules. Read more here.)

To evaluate feature flags for a context, use the OpenFeature Evaluation API:

var context = OpenFeature.Model.EvaluationContext.Builder()
.SetTargetingKey("#SOME-USER-ID#")
.Set("Email", "[email protected]")
.Set("Country", "CountryID")
.Set("Rating", 4.5)
.Set("RegisteredAt", DateTime.Parse("2023-11-22 12:34:56 +00:00", System.Globalization.CultureInfo.InvariantCulture))
.Build();

var isAwesomeFeatureEnabled = await client.GetBooleanValueAsync("isAwesomeFeatureEnabled", false, context);

Look under the hood