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

EDIT ON GITHUB

.NET SDK reference (client-side)

Read time: 2 minutes
Last edited: Oct 26, 2021

Overview

This reference guide documents all of the methods available in the client-side .NET SDK, and explains in detail how these methods work. LaunchDarkly's SDKs are open source. To learn more, read the client-side .NET SDK GitHub repository. Additionally you can clone and run sample applications using this SDK with Xamarin, and Xamarin.Forms. The online API docs contain the programmatic definitions of every type, property, and method.

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 are looking to set up LaunchDarkly on the server-side, head to our server-side .NET SDK reference.

To learn more, 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.

Requirements

The client-side .NET SDK natively supports Android (version 7.1 or higher) or iOS (version 10 or higher); it can also be used on 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 SDK used the Xamarin.Essentials library, but it no longer has the dependency.

To start using the client-side .NET SDK:

1Install-Package LaunchDarkly.XamarinSdk

Next, import the LaunchDarkly packages in your application code:

1using LaunchDarkly.Client;
2using LaunchDarkly.Xamarin;

After the dependency is installed, initialize the LaunchDarkly client. Create a single, shared instance of LdClient. This authorizes your application to connect to LaunchDarkly and retrieve flag values for your application and environment. To create a client instance, you need your environment's mobile key, available in the Projects tab of your account settings page. Be sure to use a mobile key. Never embed a server-side SDK key into a mobile application.

The following example shows the simplest way to create the client. It will block for up to one second until the latest feature flags have been retrieved from LaunchDarkly.

Here's how:

1var user = User.WithKey(user_key);
2var timeSpan = TimeSpan.FromSeconds(10);
3client = LdClient.Init("MY_MOBILE_KEY", user, timeSpan);

However, calling blocking code from the main thread in an Android app is not considered a best practice. The preferred method, loading the client asynchronously, is shown here:

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

It's important to make 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.

Android requires AccessNetworkState permission

For Android, the AccessNetworkState permission is required and must be configured in the Android project. To learn more, read the Xamarin docs about how to implement this requirement.

Using client, you can check which variation a particular user should receive for a given feature flag. Here's how:

1bool showFeature = client.BoolVariation("your.feature.key", false);
2if (showFeature) {
3 // application code to show the feature
4}
5else {
6 // the code to run if the feature is off
7}
Making feature flags available to this SDK

You must make feature flags available to client-side SDKs before the flags can be evaluated. If you evaluate a feature flag that is not available, you receive the default value for that flag. If you want a project's flags to be made 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:

1Configuration config = Configuration.Builder("YOUR_SDK_KEY")
2 .StreamUri(new URI("YOUR_RELAY_URI"))
3 .Build();
4LdClient client = LdClient.Init(config);

Lastly, 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 Apple App Store data collection policy.

Supported features

This SDK supports the following features: