LaunchDarkly OpenFeature provider for the Server-Side SDK for .NET
This provider allows for using LaunchDarkly with the OpenFeature SDK for .NET.
This provider is designed primarily for use in multi-user systems such as web servers and applications. It follows the server-side LaunchDarkly model for multi-user contexts. It is not intended for use in desktop and embedded systems applications.
LaunchDarkly overview
LaunchDarkly is a feature management platform that serves trillions of feature flags daily to help teams build better software, faster. Get started using LaunchDarkly today!
Supported .NET versions
This version of the SDK is built for the following targets:
- .NET 6.0: runs on .NET 6.0 and above.
- .NET Framework 4.7.1: runs on .NET Framework 4.7.1 and above.
- .NET Standard 2.0: runs in any project that is targeted to .NET Standard 2.x rather than to a specific runtime platform.
While these are the minimum supported versions, you should use the latest patched version of a supported .NET release.
Feature matrix
This matrix mirrors the feature matrix of the OpenFeature SDK for .NET and describes what this provider supports. Rows which are not supported state whether the limitation comes from the OpenFeature .NET SDK or from the provider.
| Status |
Feature |
Notes |
| β
|
Providers |
Evaluates boolean, string, integer, double, and structure values through the LaunchDarkly .NET SDK. |
| β
|
Targeting |
The EvaluationContext is converted to a LaunchDarkly single or multi-context. See OpenFeature Specific Considerations. |
| β
|
Hooks |
Hooks are registered on the OpenFeature API and client; the provider requires no additional support and its results are visible to hooks, including flag metadata. |
| β
|
Tracking |
Track sends a LaunchDarkly custom event for the evaluation context, with the tracking event value and remaining details attached. |
| β
|
Logging |
The provider logs through the logging configuration of the Configuration it is given. |
| β
|
Domains |
Domains bind clients to providers in the OpenFeature SDK; a separate provider instance may be registered per domain. |
| β
|
Eventing |
LaunchDarkly data source status changes are emitted as PROVIDER_READY, PROVIDER_STALE, and PROVIDER_ERROR. Flag changes are emitted as PROVIDER_CONFIGURATION_CHANGED with the changed flag key. |
| β οΈ |
Initialization |
InitializeAsync waits for the LaunchDarkly client to become ready or to shut down, and stops waiting if the cancellation token given to SetProviderAsync is canceled. It has no timeout of its own; StartWaitTime applies to the client constructor. Since LaunchDarkly .NET SDK 8.17.0 a rejected SDK key is retried rather than failing permanently, so without a cancellation token InitializeAsync can wait indefinitely. Provider gap: #71. |
| β
|
Shutdown |
ShutdownAsync closes the LaunchDarkly client. A closed client cannot be restarted, so a new provider instance is required afterward. |
| β
|
Transaction Context Propagation |
Provided by the OpenFeature SDK, which merges the transaction context into the evaluation context before the provider is called; no provider support is required. |
| β
|
Extending |
The underlying LaunchDarkly client is available through GetClient() for functionality with no OpenFeature equivalent. |
| β
|
Multi-Provider (experimental) |
Provided by the OpenFeature SDK; no provider support is required. |
| β οΈ |
DependencyInjection (experimental) |
Not yet supported by this provider: #48, #49. |
| β
|
Flag metadata |
LaunchDarkly evaluation reason details are returned as OpenFeature flag metadata. See Flag Metadata. |
Supported: β
| Partially supported: β οΈ | Not supported: β
Getting started
Installation
dotnet add package LaunchDarkly.ServerSdk
dotnet add package LaunchDarkly.OpenFeature.ServerProvider
dotnet add package OpenFeature
Usage
using LaunchDarkly.OpenFeature.ServerProvider;
using LaunchDarkly.Sdk.Server;
var config = Configuration.Builder("my-sdk-key")
.StartWaitTime(TimeSpan.FromSeconds(10))
.Build();
var provider = new Provider(config);
// If you need access to the LdClient, then you can use GetClient().
// This can be used for use-cases that are not supported by OpenFeature such as migration flags and track events.
var ldClient = provider.GetClient()
await OpenFeature.Api.Instance.SetProviderAsync(provider);
Refer to the SDK reference guide for instructions on getting started with using the SDK.
For information on using the OpenFeature client please refer to the OpenFeature Documentation.
OpenFeature Specific Considerations
LaunchDarkly evaluates contexts, and it can either evaluate a single-context, or a multi-context. When using OpenFeature both single and multi-contexts must be encoded into a single EvaluationContext. This is accomplished by looking for an attribute named kind in the EvaluationContext.
There are 4 different scenarios related to the kind:
- There is no
kind attribute. In this case the provider will treat the context as a single context containing a "user" kind.
- There is a
kind attribute, and the value of that attribute is "multi". This will indicate to the provider that the context is a multi-context.
- There is a
kind attribute, and the value of that attribute is a string other than "multi". This will indicate to the provider a single context of the kind specified.
- There is a
kind attribute, and the attribute is not a string. In this case the value of the attribute will be discarded, and the context will be treated as a "user". An error message will be logged.
The kind attribute should be a string containing only contain ASCII letters, numbers, ., _ or -.
The OpenFeature specification allows for an optional targeting key, but LaunchDarkly requires a key for evaluation. A targeting key must be specified for each context being evaluated. It may be specified using either targetingKey, as it is in the OpenFeature specification, or key, which is the typical LaunchDarkly identifier for the targeting key. If a targetingKey and a key are specified, then the targetingKey will take precedence.
The targeting key may also be specified using SetTargetingKey. Currently, if SetTargetingKey is used, and a targetingKey attribute exists, then the attribute will be used instead.
There are several other attributes which have special functionality within a single or multi-context.
Examples
A single user context
var evaluationContext = EvaluationContext.Builder()
.Set("targetingKey", "my-user-key") // Could also use "key" instead of "targetingKey".
.Build();
A single context of kind "organization"
var evaluationContext = EvaluationContext.Builder()
.Set("kind", "organization")
.Set("targetingKey", "my-org-key") // Could also use "key" instead of "targetingKey".
.Build();
A multi-context containing a "user" and an "organization"
var evaluationContext = EvaluationContext.Builder()
.Set("kind", "multi") // Lets the provider know this is a multi-context
// Every other top level attribute should be a structure representing
// individual contexts of the multi-context.
// (non-conforming attributes will be ignored and a warning logged).
.Set("organization", new Structure(new Dictionary<string, Value>
{
{"targetingKey", new Value("my-org-key")},
{"name", new Value("the-org-name")},
{"myCustomAttribute", new Value("myAttributeValue")}
}))
.Set("user", new Structure(new Dictionary<string, Value> {
{"targetingKey", new Value("my-user-key")},
}))
.Build();
Setting private attributes in a single context
var evaluationContext = EvaluationContext.Builder()
.Set("kind", "organization")
.Set("name", "the-org-name")
.Set("targetingKey", "my-org-key")
.Set("anonymous", true)
.Set("myCustomAttribute", "myCustomValue")
.Set("privateAttributes", new Value(new List<Value>{new Value("myCustomAttribute")}))
.Build();
Setting private attributes in a multi-context
var evaluationContext = EvaluationContext.Builder()
.Set("kind", "multi")
.Set("organization", new Structure(new Dictionary<string, Value>
{
{"targetingKey", new Value("my-org-key")},
{"name", new Value("the-org-name")},
// This will ONLY apply to the "organization" attributes.
{"privateAttributes", new Value(new List<Value>{new Value("myCustomAttribute")})}
// This attribute will be private.
{"myCustomAttribute", new Value("myAttributeValue")},
}))
.Set("user", new Structure(new Dictionary<string, Value> {
{"targetingKey", new Value("my-user-key")},
{"anonymous", new Value(true)},
// This attribute will not be private.
{"myCustomAttribute", new Value("myAttributeValue")},
}))
.Build();
Flag Metadata
Evaluations include flag metadata for the LaunchDarkly specific parts of the evaluation result which have no OpenFeature equivalent. Each entry is absent when it does not apply to the evaluation.
| Key |
Type |
Description |
variationIndex |
integer |
The index of the returned variation. Absent for default values. |
inExperiment |
boolean |
Present, and true, when the evaluation was part of an experiment. |
ruleIndex |
integer |
The index of the rule that matched. |
ruleId |
string |
The identifier of the rule that matched. |
prerequisiteKey |
string |
The key of the prerequisite flag that failed. |
bigSegmentsStatus |
string |
The status of the Big Segments query made during the evaluation. |
var details = await client.GetBooleanDetailsAsync("my-flag", false, evaluationContext);
var inExperiment = details.FlagMetadata.GetBool("inExperiment") ?? false;
Advanced Usage
Asynchronous Initialization
The LaunchDarkly SDK by default blocks on construction for up to 10 seconds for initialization. If you require construction to be non-blocking, then you can adjust the startWaitTime to TimeSpan.Zero. Initialization will be completed asynchronously and OpenFeature will emit a ready event when the provider has initialized. The SetProviderAsync method can be awaited to wait for the SDK to finish initialization.
var config = Configuration.Builder("my-sdk-key")
.StartWaitTime(TimeSpan.Zero)
.Build();
Provider Shutdown
This provider cannot be re-initialized after being shutdown. This will not impact typical usage, as the LaunchDarkly provider will be set once and used throughout the execution of the application. If you remove the LaunchDarkly Provider, by replacing the default provider or any named providers aliased to the LaunchDarkly provider, then you must create a new provider instance.
var ldProvider = new Provider(config);
await OpenFeature.Api.Instance.SetProviderAsync(provider);
await OpenFeatute.Api.Instance.SetProviderAsync(new SomeOtherProvider());
/// The LaunchDarkly provider will be shutdown and SomeOtherProvider will start handling requests.
// This provider will never finish initializing.
await OpenFeature.Api.Instance.SetProviderAsync(ldProvider);
// Instead you should create a new provider.
var ldProvider2 = new Provider(config);
await OpenFeature.Api.Instance.SetProviderAsync(ldProvider2);
Learn more
Read our documentation for in-depth instructions on configuring and using LaunchDarkly. You can also head straight to the complete reference guide for this SDK.
The authoritative description of all properties and methods is in the dotnet documentation.
Contributing