• SDKS
No results for ""


.NET SDK reference

Read time: 7 minutes
Last edited: May 14, 2021

This reference guide documents all of the methods available in our .NET SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source. To learn more, read .NET SDK GitHub repository. The online API docs contain the programmatic definitions of every type and method. Additionally you can clone and run a sample application using this SDK.

For use in server-side applications only

This SDK is intended for use in multi-user .NET server applications. If you have a .NET application and are looking to set up LaunchDarkly in a mobile, desktop, or embedded application, head to our Xamarin SDK reference.

To learn more, read client-side and server-side SDKs.

Getting started

The .NET SDK requires version 4.5 or later of the .NET framework or .NETCoreApp 1.0 or later.

Building on top of our Getting Started guide, the following steps will get you started with using the LaunchDarkly SDK in your .NET application.

The first step is to install the LaunchDarkly SDK as a dependency in your application using your application's dependency manager.

1Install-Package LaunchDarkly.ServerSdk
3# Note that in earlier versions, the package name was LaunchDarkly.Client

Next you should import the LaunchDarkly client in your application code.

1using LaunchDarkly.Client;
3// Note that this namespace is no longer the same as the package name

Once the SDK is installed and imported, you'll want to create a single, shared instance of LdClient. You should specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

1LdClient ldClient = new LdClient("YOUR_SDK_KEY");
LDClient must be a singleton

It's important to make this a singleton. The client instance maintains internal state that allows us to serve feature flags without making any remote requests. Be sure that you're not instantiating a new client with every request.

Using ldClient, you can check which variation a particular user should receive for a given feature flag.

1User user = User.WithKey(username);
2bool showFeature = ldClient.BoolVariation("your.feature.key", user, false);
3if (showFeature) {
4 // application code to show the feature
6else {
7 // the code to run if the feature is off

Lastly, when your application is about to terminate, shut down ldClient with Dispose(). This ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events. This is something you only need to do once.

1// shut down the client, since we're about to quit

Customizing your client

You can also pass custom parameters to the client by creating a custom configuration object:

1Configuration config = LaunchDarkly.Client.Configuration.Builder("YOUR_SDK_KEY")
2 .EventFlushInterval(TimeSpan.FromSeconds(2))
3 .Build();
4LdClient ldClient = new LdClient(config);

To get a complete list of configuration options for the client, including proxy settings, timeouts, and streaming/polling options, read the API documentation for IConfigurationBuilder.


Feature flag targeting and rollouts are all determined by the user you pass to your variation calls. In our .NET SDK, the User class has a WithKey method for creating a simple user with only a key, and a Builder method for building a user with other properties. Here's an example:

1LDUser user = User.Builder("aa0ceb")
2 .FirstName("Ernestina")
3 .LastName("Evans")
4 .Email("ernestina@example.com")
5 .Custom("groups", LdValue.ArrayOf(LdValue.Of("Google"), LdValue.Of("Microsoft")))
6 .Build();

Let's walk through this snippet. The argument to Builder is the user's key. In this case we've used the hash "aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an email address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (set by calls to FirstName, LastName, Email, and Custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

In addition to the built-in attributes defined in the User class, you can pass us any of your own user data by passing custom attributes, like the groups attribute in the example above.

A note on types

Most of our built-in attributes (like names and email addresses) expect string values. Custom attributes values can be strings, booleans (like true or false), numbers, arrays, or objects (a.k.a. dictionaries)—the same types supported by JSON. For representing the complex types—arrays and objects—the SDK uses the LdValue type. If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way. The .NET SDK is strongly typed, so be aware of this distinction.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us, including organizations, groups, andaccount plans. Anything you pass to us becomes available instantly on our dashboard.

Private user attributes

You can optionally configure the .NET SDK to treat some or all user attributes as private user attributes . Private user attributes can be used for targeting purposes, but are removed from the user data sent back to LaunchDarkly.

In the .NET SDK there are two ways to define private attributes for the entire LaunchDarkly client:

  • When creating the LaunchDarkly Configuration object, you can call the AllAttributesPrivate method, which takes in a boolean parameter. If true, all user attributes (except the key) for all users are removed before the user is sent to LaunchDarkly.
  • When creating the LaunchDarkly Configuration object, you can call the PrivateAttribute method, which takes in an attribute name (string) as a parameter and adds it to an internally managed list of private attributes. This method may be called multiple times to mark additional attributes as private. If any user has a custom or built-in attribute named in the private attributes list, it will be removed before the user is sent to LaunchDarkly.

You can also mark attributes as private when building the user object itself by calling AsPrivateAttribute() after setting the attribute on the user builder. For example:

1var user = User.Builder("aa0ceb")
2 .Email("test@example.com").AsPrivateAttribute()
3 .Build();

When this user is sent back to LaunchDarkly, the email attribute will be omitted.

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

1LDUser user = User.Builder("aa0ceb")
2 .Anonymous(true)
3 .Build();

You will still need to generate a unique key for anonymous users. Session IDs or UUIDs work best for this.

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. You also can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. This is actually a good thing, because it keeps anonymous users from polluting your Users page!


The Variation methods determine whether a flag is enabled or not for a specific user. In .NET, there is a Variation method for each type (e.g. BoolVariation, StringVariation):

1var value = ldClient.BoolVariation("your.feature.key", user, false);

Variation calls take the feature flag key, a User, and a default value.

The Variation call will automatically create a user in LaunchDarkly if a user with that user key doesn't exist already. There's no need to create users ahead of time (but if you do need to, take a look at Identify).


The VariationDetail methods (BoolVariationDetail, etc.) work the same as the Variation methods, but also provide additional "reason" information about how a flag value was calculated (for instance, if the user matched a specific rule). You can examine the "reason" data programmatically; you can also view it with Data Export, if you are capturing detailed analytics events for this flag.

To learn more, read Evaluation reasons.


The Track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. Here's a simple example:

1ldClient.Track("your-goal-key", user);

You can also attach custom data to your event by passing an extra parameter to Track, using the LdValue type which can contain any kind of data supported by JSON:

1ldClient.Track("Completed purchase", user, LdValue.Of("sku132"));


Identify creates or updates users on LaunchDarkly, making them available for targeting and autocomplete on the dashboard. In most cases, you won't need to call Identify. The Variation methods will automatically create users on the dashboard for you. Identify can be useful if you want to pre-populate your dashboard before launching any features.


All Flags

Note that unlike Variation and Identify calls, AllFlagsState does not send events to LaunchDarkly. Thus, users are not created or updated in the LaunchDarkly dashboard.

The AllFlagsState method captures the state of all feature flag keys with regard to a specific user. This includes their values, as well as other metadata.

This method can be useful for passing feature flags to your front-end. In particular, it can be used to provide bootstrap flag settings for our JavaScript SDK.

1var state = ldClient.AllFlagsState(user);

Fallback value and offline mode

The default (fallback) values are defined in your code. The default value will only be returned if an error is encountered or if LaunchDarkly is unreachable.

In some situations, you might want avoid remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. You can do this by setting offline mode in the client's Config. When the client is in offline mode, no network requests will be made, so it is suitable for unit-testing.

1Configuration config = Configuration.Builder("SDK_KEY")
2 .Offline(true)
3 .Build();
4LdClient client = new LdClient(config);

Secure mode hash

The SecureModeHash method computes an HMAC signature of a user signed with the client's SDK key. If you're using our JavaScript SDK for client-side flags, this method generates the signature you need for secure mode.

1var hash = ldClient.SecureModeHash(user);


Internally, the LaunchDarkly SDK keeps an event buffer for events generated by evaluations (or by Track or Identify calls). These are flushed periodically in a background thread. In some situations (for example, if you're testing out the SDK in a REPL), you may want to manually call Flush to process events immediately.


The flush interval is configurable. If you need to change the interval, you can do so via the Configuration class.


Dispose safely shuts down the client instance and releases all resources associated with the client. In most long-running applications, you should not have to call dispose.



The .NET SDK uses the Common.Logging framework. For an example configuration check out the Common.Logging readme.

Be aware of two considerations when enabling the DEBUG log level:

  1. Debug-level logs can be very verbose. It is not recommended that you turn on debug logging in high-volume environments.
  2. Potentially sensitive information is logged including LaunchDarkly users created by you in your usage of this SDK.

Database integrations

Separate packages allow feature flag data to be cached with Redis, DynamoDB, or Consul.

To learn more, read Using a persistent feature store.

Transport Layer Security (TLS) and other networking issues

.NET applications running on older operating systems are prone to use older, less secure versions of TLS. It is possible to increase your application's security using AppContext switches.

To learn more, read Microsoft's documentation.

Potential network connectivity issues caused by DNS caching

LaunchDarkly servers operate in a load-balancing framework which may cause their IP addresses to change. This could result in the SDK failing to connect to LaunchDarkly if an old IP address is still in your system's DNS cache. In .NET, the DNS cache retains IP addresses for two minutes by default. If you are noticing intermittent connection failures that always resolve in two minutes, you may wish to change this setting to a lower value as described here.