• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL
launchdarkly.com

EDIT ON GITHUB

.NET SDK reference

Read time: 2 minutes
Last edited: Jun 18, 2021

This reference guide documents all of the methods available in the .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

Requirements

Version 6.0 and higher of the LaunchDarkly .NET SDK is compatible with .NET 5.0+, .NET Framework 4.5.2+, or .NET Core 2.1+.

Prior to version 6.0, the LaunchDarkly .NET SDK also supported .NET Framework 4.5.0 and .NET Core 1.0.

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

Next, import the LaunchDarkly SDK's namespace(s) in your application code. The namespace is not the same as the package name.

1using LaunchDarkly.Sdk;
2using LaunchDarkly.Sdk.Server;
3
4// LaunchDarkly.Sdk defines general types like User, which are also used in the Xamarin SDK.
5// LaunchDarkly.Sdk.Server defines the LdClient and Configuration types for the server-side .NET SDK.

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 should be a singleton

In the standard use case where there is only one SDK key, it's important to make the LdClient 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.

If you need to use more than one SDK key, the SDK allows you to create more than one LdClient instance. This is an uncommon requirement, but one the SDK supports.

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
5}
6else {
7 // the code to run if the feature is off
8}

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
2ldClient.Dispose();

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 notice intermittent connection failures that always resolve in two minutes, you may want to change this setting to a lower value as described in Microsoft's documentation.

Supported features

This SDK supports the following features: