.NET SDK reference
In Version 1.1.4 of the .NET SDK, the package name for installing the SDK changed from ff-netF48-server-sdk to ff-dotnet-server-sdk. To use this version, make sure you remove the old package name and use the new one. You can do this by using the following commands:
Remove the old package
dotnet remove package ff-netF48-server-sdk
Add the new packagedotnet add package ff-dotnet-server-sdk
This topic describes how to use the Harness Feature Flags .NET SDK for your .Net application.
For getting started quickly, you can use our sample code from the .NET SDK README. You can also clone and run a sample application from the .NET SDK GitHub Repository.
Before you begin
Make sure you read and understand:
- Feature Flags Overview
- Getting Started with Feature Flags
- Client-Side and Server-Side SDKs
- Communication Strategy Between SDKs and Harness Feature Flags
Version
Latest SDK version can be found on GitHub Release Page
If you are using an older version of the .NET Framework, it may not default the security protocol to TLS 1.2. For compatibility with this SDK, set the protocol to TLS 1.2 by using the following:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Requirements
To use this SDK, make sure you:
- Install .NET Framework 4.8 or newer, or .Net 5.0.104 or newer.
The library is packaged as multi-target and supports the netstandard 2.0 set of API's as well as net461 for older frameworks.*
- Download the SDK from our GitHub repository
- Create a .NET application, or clone our sample application.
- Create a Feature Flag on the Harness Platform. If you are following along with the SDK README sample code, make sure your flag is called
harnessappdemodarkmode
. - Create a server SDK key and make a copy of it
Install the SDK
Install the SDK by using the dotnet add package
command, for example:
dotnet add package ff-dotnet-server-sdk --version 1.4.0
In Version 1.1.4 of the .NET SDK, the package name for installing the SDK changed from ff-netF48-server-sdk to ff-dotnet-server-sdk. To use this version, make sure you remove the old package name and use the new one. You can do this by using the following commands:
Remove the old package
dotnet remove package ff-netF48-server-sdk
Add the new packagedotnet add package ff-dotnet-server-sdk
To initialize the .NET SDK, you need to:
- Add your Server SDK key to connect to your Harness Environment.
- Add a Target that you want to Evaluate against a Feature Flag.
- (Optional) Configure the SDK.
- Complete the initialization with the SDK using the Server SDK Key, Target, and Configuration parameters you set.
Add the Server SDK Key
To connect to the correct Environment that you set up on the Harness Platform, you need to add the Server SDK Key from that Environment. Input the Client SDK Key into the sdkKey
parameter, for example:
public static String apiKey = "YOUR_API_KEY";
Add a Target
What is a Target?
Targets are used to control which users see which Variation of a Feature Flag, for example, if you want to do internal testing, you can enable the Flag for some users and not others. When creating a Target, you give it a name and a unique identifier. Often Targets are users but you can create a Target from anything that can be uniquely identified, such as an app or a machine.
For more information about Targets, go to Targeting Users With Flags.
To add a Target, build it and pass in arguments for the following:
Parameter | Description | Required? | Example |
Identifier | Unique ID for the Target.Read Regex requirements for Target names and identifiers below for accepted characters. | Required | .Identifier("HT_1") |
Name | Name for this Target. This does not have to be unique. Note: If you don’t provide a value, the name will be the same as the identifier.Read Regex requirements for Target names and identifiers below for accepted characters. | OptionalNote: If you don't want to send a name, don't send the parameter. Sending an empty argument will cause an error. | .Name("Harness_Target_1") |
Attributes | Additional data you can store for a Target, such as email addresses or location. | Optional | .Attributes(new Dictionary<string, string>(){{"email", "demo@harness.io"}}) |
Regex requirements for Target names and identifiers
Identifier
Regex: ^[A-Za-z0-9.@_-]*$
Must consist of only alphabetical characters, numbers, and the following symbols:
. (period)
@ (at sign)
-(dash)
_ (underscore)
The characters can be lowercase or uppercase but cannot include accented letters, for example Cafe_789
.
Name
Regex: ^[\\p{L}\\d .@_-]*$
Must consist of only alphabetical characters, numbers, and the following symbols:
. (period)
@ (at sign)
-(dash)
_ (underscore)
(space)
The characters can be lowercase or uppercase and can include accented letters, for example Café_123
.
For example:
Target target = Target.builder()
.Name(".NET SDK Target 1")
.Identifier("DotNetTarget1")
.Attributes(new Dictionary<string, string>(){{"email", "demo@harness.io"}})
.build();
Configure the SDK
When initializing the SDK, you also have the option of providing alternative configuration by using Config.Builder()
.
You can configure the following base features of the SDK:
Name | Example | Description | Default Value |
configUrl | ConfigUrl("https://config.ff.harness.io/api/1.0") | The URL used to fetch Feature Flag Evaluations. When using the Relay Proxy, change this to: http://localhost:7000 | https://config.ff.harness.io/api/1.0 |
eventUrl | EventUrl("https://events.ff.harness.io/api/1.0") | The URL for posting metrics data to the Feature Flag service. When using the Relay Proxy, change this to: http://localhost:7000 | https://events.ff.harness.io/api/1.0 |
pollingInterval | SetPollingInterval(60000) | The interval in milliseconds that we poll for changes when you are using stream mode. | 60000 (milliseconds) |
streamEnabled | SetStreamEnabled(true) | Set to true to enable streaming mode.Set to false to disable streaming mode. | true |
analyticsEnabled | SetAnalyticsEnabled(true) | Set to true to enable analytics.Set to false to disable analytics.Note: When enabled, analytics data is posted every 60 seconds. | true |
For example:
CfClient.Instance.Initialize(apiKey, Config.Builder()
.ConfigUrl("https://config.ff.harness.io/api/1.0")
.EventUrl("https://events.ff.harness.io/api/1.0")
.SetPollingInterval(60)
.SetStreamEnabled(true)
.SetAnalyticsEnabled(true)
.Build());
Complete the initialization
To complete the initialization, create an instance of the cfClient
and pass in the apiKey
and Config.Builder
, for example:
CfClient.Instance.Initialize(apiKey, Config.Builder().Build());
Sample of initializing the SDK
public static String apiKey = Environment.GetEnvironmentVariable("FF_API_KEY");
public static String flagName = Environment.GetEnvironmentVariable("FF_FLAG_NAME") is string v && v.Length > 0 ? v : "harnessappdemodarkmode";
static void Main(string[] args)
{
// Create a feature flag client
CfClient.Instance.Initialize(apiKey, Config.Builder().Build());
CfClient.Instance.WaitForInitialization();
// Create a target (different targets can get different results based on rules)
Target target = Target.builder()
.Name("DotNetSDK")