.NET SDK Reference

Updated 5 days ago by Archana Singh

This topic explains how to use the Harness Feature Flags (FF) SDK in your .NET application. To learn more about using a sample FF SDK code with the .NET application, see the .NET Server SDK GitHub repository.

In this topic:

Before You Begin

Prerequisites

  1. Create a feature flag in Harness Feature Flags. Feature flags wrap your code and allow you to manage the feature release in a controlled way. See Create a Feature Flag.
  2. Ensure that you have created your SDK Key. See Create an SDK Key.
  3. FF requires >= 4.6.1 version of the .NET framework.
  4. A .NET application to test your feature flag. If you do not have your .NET Application, you can download a sample application from the GitHub repository. See .NET Server Sample Application.
    1. On the GitHub page, click Code and then clone the sample application. For more information, see Cloning a repository.
    2. Import your project in an IDE such as Visual Studio Code.

Use Harness Feature Flags SDKs with .NET Applications

Perform the following steps to get started with using the Feature Flags SDK in your .NET application:

Step 1: Install the FF SDK Dependency

The first step is to install the FF SDK as a dependency in your application using your application's dependency manager. You can use Package Manager, .NET CLI, PackageReference, etc. for your application.

This document uses.NET CLI and uses 1.1.2 version for .NET Server SDKs:

dotnet add package ff-netF48-server-sdk --version 1.1.2

Step 2: Authorize your Application to Connect to the FF Client

After installing the SDK, enter the SDK keys that you created for your environment. The SDK keys authorize your application to connect to the FF client.

target represents the desired target for which you want the features to be evaluated.

"YOUR_API_KEY" is an authentication key. See Create an SDK Key.

using io.harness.cfsdk.client.dto;
using io.harness.cfsdk.client.api;

/**
* Put the API Key here from your environment
*/
String API_KEY = "YOUR_API_KEY";

config = Config.Builder()
.SetPollingInterval(60000)
.SetAnalyticsEnabled()
.SetStreamEnabled(true)
.Build();

/**
* Call to Initialize will start authentication, while await will pause execution
* until initialization is completed.
*/
await CfClient.Instance.Initialize(API_KEY, config);

/**
* Define you target on which you would like to evaluate
* the featureFlag
*/
Target target = io.harness.cfsdk.client.dto.Target.builder()
.Name("User1") //can change with your target name
.Identifier("[email protected]") //can change with your target identifier
.build();

This initializes the SDK.

You can also manually instantiate CfClient, enter the required configuration values, and initiate the authentication process.

// Creates instance of a client
var client = new CfClient(API_KEY, Config.Builder().Build());

// Starts authentication and asynchronously wait for initialisation to complete
await client.InitializeAndWait();

Step 3: Listen to the Events

This method provides a way to listen to the different events that might be triggered by SDK, indicating a specific change in the SDK.

client.InitializationCompleted += (sender, e) =>
{
// fired when authentication is completed and recent configuration is fetched from server
Console.WriteLine("Notification Initialization Completed");
};
client.EvaluationChanged += (sender, identifier) =>
{
// Fired when flag value changes.
Console.WriteLine($"Flag changed for {identifier}");
};

Step 4: Evaluate Target for Your Feature Flag

Once you have added the target, evaluate the target for your feature flag. A feature flag is evaluated for a particular target.

Bool Variation

public bool boolVariation(string key, dto.Target target, bool defaultValue) 
Number Variation

public double numberVariation(string key, dto.Target target, int defaultValue)  
String Variation

public string stringVariation(string key, dto.Target target, string defaultValue)  

JSON Variation

public JObject jsonVariation(string key, dto.Target target, JObject defaultValue)

Step 5: Verify Your Feature Flag

Run the application from your IDE and verify whether the flag variation value displayed on your application page is consistent with the feature flag you created.

Toggle the flag on and off to verify if your application is getting updated.

Add a Connector

A Connector is just a proxy to your data. Currently, supported connectors are:

  • Harness (Used by default)
  • Local (used only in development)
LocalConnector connector = new LocalConnector("local");
client = new CfClient(connector);

Add a Storage Interface

The storage interface should be used for offline support and asynchronous SDK initialization. All flags are loaded from the last saved settings when SDK is used without waiting on async methods and configuration is provided with file storage. If storage doesn't have a flag, the defaultValue parameter is used to evaluate it.

FileMapStore fileStore = new FileMapStore("Non-Freemium");
LocalConnector connector = new LocalConnector("local");
client = new CfClient(connector, Config.builder().store(fileStore).build());

Use Feature Flags Metrics

Metrics API endpoint can be changed as the following:

Config.builder()
.EventUrl("METRICS_API_EVENTS_URL")
.build();

Otherwise, the default metrics endpoint URL is used.


Please Provide Feedback