• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

.NET SDK reference (client-side)

Read time: 3 minutes
Last edited: Aug 12, 2022

Overview

This topic documents how to get started with the client-side .NET SDK, and links to reference information on all of the supported features.

SDK quick links

LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositorydotnet-client-sdk
Sample applicationshello-dotnet-client (Xamarin)
Published moduleNuGet
For use in mobile, desktop, and embedded client applications only

This SDK is intended for use in single-user mobile, desktop, and embedded applications. If you have a .NET application and want to set up LaunchDarkly on the server-side, read the server-side .NET SDK reference.

To learn more about LaunchDarkly's different SDK types, read Client-side and server-side SDKs.

This SDK was previously named the Xamarin SDK

Older versions of this SDK were named the LaunchDarkly Xamarin SDK, because Xamarin was the .NET-based platform for mobile devices. Because you can also use this SDK in desktop and embedded applications without Xamarin, we have renamed it.

Older versions were published with the NuGet package name LaunchDarkly.XamarinSdk. In all future releases, the package name is LaunchDarkly.ClientSdk instead.

Getting started

After you complete the Getting started process, follow these instructions to start using the LaunchDarkly SDK in your application.

SDK version compatibility

The LaunchDarkly client-side .NET SDK is compatible with Android SDK version 7.1 and higher and with iOS version 10.0 and higher.

It is also compatible with any other platform that supports .NET Standard version 1.6 or higher, although the .NET Standard version lacks some mobile-specific features such as detecting network connectivity. Previous beta releases of the LaunchDarkly client-side .NET SDK used the Xamarin.Essentials library, but the SDK no longer has that dependency.

To start using the client-side .NET SDK:

Install-Package LaunchDarkly.ClientSdk

Next, import the LaunchDarkly packages in your application code:

using LaunchDarkly.Sdk;
using LaunchDarkly.Sdk.Client;
// LaunchDarkly.Sdk defines general types like User, which are also used in the server-side .NET SDK.
// LaunchDarkly.Sdk.Client defines the LdClient and Configuration types for the client-side .NET SDK.

After you install the dependency, initialize the LaunchDarkly client by creating a single, shared instance of LdClient. To create a client instance, you need your environment's mobile key. This authorizes your application to connect to a particular environment within LaunchDarkly. Your mobile key is available in the Projects tab of your Account settings page.

Never embed a server-side SDK key into a client-side application

Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.

The following example shows the simplest way to create the client. It blocks for up to one second until it retrieves the latest feature flags from LaunchDarkly.

Here's how:

var user = User.WithKey(user_key);
var timeSpan = TimeSpan.FromSeconds(10);
client = LdClient.Init("YOUR_MOBILE_KEY", user, timeSpan);

However, calling blocking code from the main thread in a mobile app is not considered a best practice. The preferred method is to load the client asynchronously.

Here's how:

User user = User.WithKey(user_key);
client = await LdClient.InitAsync("YOUR_MOBILE_KEY", user);
LdClient must be a singleton

It's important to make LdClient a singleton. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

Android requires AccessNetworkState permission

Android requires the AccessNetworkState permission and you must configure it in the Android project. To learn more about how to implement this requirement, read Microsoft's Xamarin documentation.

You can use client to check which variation a particular user will receive for a given feature flag.

Here's how:

bool showFeature = client.BoolVariation("your.feature.key", false);
if (showFeature) {
// Application code to show the feature
}
else {
// The code to run if the feature is off
}
Making feature flags available to this SDK

You must make feature flags available to mobile SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the user will receive the default value for that flag.

To make a flag available to this SDK, check the SDKs using Mobile key checkbox during flag creation, or on the flag's Settings tab. To make all of a project's flags available to this SDK by default, check the SDKs using Mobile key checkbox in your project Settings.

Using the Relay Proxy

You can configure the client-side .NET SDK to connect to the Relay Proxy as follows:

Configuration config = Configuration.Builder("YOUR_SDK_KEY")
.ServiceEndpoints(
Components.ServiceEndpoints().RelayProxy("YOUR_RELAY_URI")
)
.Build();
LdClient client = LdClient.Init(config);

Shutting down

Shut down the client when your application terminates. To learn more, read Shutting down.

Data collection

To learn more about data collection within this SDK and implications on submissions to the Apple App Store, read the Apple App Store data collection policy.

Supported features

This SDK supports the following features: