.NET Feature Flags

.NET/C# Feature Flag Resources/Solutions

  • LaunchDarkly .NET Feature Flag SDK – LaunchDarkly
    • An installable feature flag software development kit for .NET apps.  This SDK harnesses the LaunchDarkly app and allows you to target users and manage rollouts.  Feature flags are evaluated in microseconds.
    • Main Website
    • GitHub Repo
  • .NET Feature Flags – CodePlex
    • “Features Flags is a .net implementation of the feature flags technique (also known as Feature Toggle). Feature Flags are very useful to enable real continous integration. This implementation stores features state inside a SQL Server Database and enables dynamic activation from a WPF UI and from .net code.”
  • NFeature – Ben Aston
    • “A simple feature configuration system (or feature toggle / flipper / whatever you want to call it.). Feature configuration walls enable you to integrate your code earlier, which brings lots of goodness (such as helping to avoid branch merge problems.)”
    • GitHub Repo
  • FeatureToggle – Jason Roberts
    • “Simple, reliable feature toggles in .NET. Install FeatureToggle easily via NuGet”
    • GitHub Repo
  • FeatureSwitcher – Max Malook
    • “FeatureSwitcher is little library build to support you when you want to introduce feature switches/toggles in your code. Before this library was born, the existed alternatives (nToggle, FeatureToggle and NFeature) was tested. The API of the first two is toggle centric it meens you have to decide while you coding how a feature is later controlled in production ex. using date range or database entry. Although the API of the last one is feature centric a feature must be defined as enum value what makes it complex for configuration.”
    • GitHub Repo
  • nToggle – Steve Moyer
  • Toggler – Manoj

Implementing Feature Toggles in .NET

  • “Learn what feature toggles are and how to use them in applications, and also how to use the FeatureToggle library.”

———————

LaunchDarkly .NET SDK Reference


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, the SDKs are open source– head to the .NET SDK GitHub repository to look under the hood.

.NET SDK Reference


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– head to our .NET SDK GitHub repositoryto look under the hood.

Getting started


If you haven’t taken a look at our Quickstart guide yet, we recommend starting there to see how install our SDK into your .NET application.

Requirements

The .NET SDK requires version 4.5 or later of the .NET framework. Support for .NET Core is coming soon.

Once the SDK is installed, you’ll want to create a single, shared instance of LDClient:

LdClient ldClient = new LdClient("YOUR_SDK_KEY");

LDClient must be a singleton

It’s important to make this a singleton– internally, 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.

Customizing your client


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

Configuration config = LaunchDarkly.Client.Configuration.Default()
          .WithSdkKey("YOUR_SDK_KEY")
          .WithEventQueueFrequency(TimeSpan.FromSeconds(2));
LdClient ldClient = new LdClient(config);

Here, we’ve customized the event queue flush frequency.

Users


Feature flag targeting and rollouts are all determined by the user you pass to your variation calls. In our .NET SDK, the User class contains extension methods that make it easy to construct users. Here’s an example:

 LDUser user = User.WithKey("aa0ceb")
      .AndFirstName("Ernestina")
      .AndLastName("Evans")
      .AndEmail("ernestina@example.com")
      .AndCustomAttribute("groups", new List<String>(){"Google", "Microsoft"})

Let’s walk through this snippet. The argument to WithKey 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 e-mail 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 via calls to AndFirstName, AndLastName, AndEmail, and AndCustom 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 e-mail addresses) expect string values. Custom attributes values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers.

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– organizations, groups, account plans– anything you pass to us becomes available instantly on our dashboard.

Anonymous users


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

 LDUser user = User.WithKey("aa0ceb")
      .AndAnonymous(true)

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– it keeps anonymous users from polluting your Users page!

Variation


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

ldClient.BoolVariation("your.feature.key", user, false);

variation calls take the feature flag key, an LDUser, and a default value.

The default value will only be returned if an error is encountered– for example, if the feature flag key doesn’t exist or the user doesn’t have a key specified.

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).

All Flags


The AllFlags method produces a map of feature flag keys to their values for a specific user.

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.

ldClient.AllFlags();

Track


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:

ldClient.Track("Signed up", user);

You can also attach custom data to your event by passing an extra parameter to Track:

ldClient.Track("Completed purchase", "sku132");

Identify


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 call 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.

ldClient.Identify(user);

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.

ldClient.SecureModeHash(user);

Flush


Internally, the LaunchDarkly SDK keeps an event buffer for Track and 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.

ldClient.Flush();

Note that the flush interval is configurable– if you need to change the interval, you can do so via theConfiguration class.

Dispose


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.

ldClient.Dispose();