/ff-dotnet-server-sdk

.net Server SDK for integrating with Harness Feature Flag service.

Primary LanguageC#Apache License 2.0Apache-2.0

.NET SDK For Harness Feature Flags

Table of Contents

Intro
Requirements
Quickstart
Further Reading
Build Instructions

Intro

Use this README to get started with our Feature Flags (FF) SDK for .NET. This guide outlines the basics of getting started with the SDK and provides a full code sample for you to try out. This sample doesn’t include configuration options, for in depth steps and configuring the SDK, for example, disabling streaming or using our Relay Proxy, see the .NET SDK Reference.

For a sample FF .NET SDK project, see our test .NET project.

FeatureFlags

Requirements

The library is packaged as multi-target supporting net461,netstandard2.0, net5.0, net6.0 and net7.0.

Build Requirements

If building from source you will need .Net 7.0.404 or newer (dotnet --version)

Quickstart

To follow along with our test code sample, make sure you’ve:

Install the SDK

Add the sdk using dotnet

dotnet add package ff-dotnet-server-sdk

Code Sample

The following is a complete code example that you can use to test the harnessappdemodarkmode Flag you created on the Harness Platform. When you run the code it will:

  • Connect to the FF service.
  • Report the value of the Flag every 10 seconds until the connection is closed. Every time the harnessappdemodarkmode Flag is toggled on or off on the Harness Platform, the updated value is reported.
  • Close the SDK.
using System;
using System.Collections.Generic;
using io.harness.cfsdk.client.dto;
using io.harness.cfsdk.client.api;
using System.Threading;

namespace getting_started
{
    class Program
    {
        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)
        {
            // Configure your logger
            var loggerFactory = new SerilogLoggerFactory(
                new LoggerConfiguration()
                    .MinimumLevel.Information()
                    .WriteTo.Console()
                    .CreateLogger());

            // Create a feature flag client
            CfClient.Instance.Initialize(apiKey, Config.Builder().LoggerFactory(loggerFactory).Build());
            var isInit = CfClient.Instance.WaitForInitialization(30000);
            if (!isInit)
            {
                Console.WriteLine("Failed to init the SDK within 30seconds");
            }

            // Create a target (different targets can get different results based on rules)
            Target target = Target.builder()
                            .Name("Harness_Target_1")
                            .Identifier("HT_1")
                            .Attributes(new Dictionary<string, string>(){{"email", "demo@harness.io"}})
                            .build();

           // Loop forever reporting the state of the flag
            while (true)
            {
                bool resultBool = CfClient.Instance.boolVariation(flagName, target, false);
                Console.WriteLine("Flag variation " + resultBool);
                Thread.Sleep(10 * 1000);
            }
        }
    }
}

Running the example

$ export FF_API_KEY=<your key here>
$ dotnet run --project examples/getting_started/

Running the example with Docker

If you dont have the right version of dotnet installed locally, or dont want to install the dependancies you can use docker to quicky get started

docker run -v $(pwd):/app -w /app -e FF_API_KEY=$FF_API_KEY mcr.microsoft.com/dotnet/sdk:6.0 dotnet run --framework net6.0 --project examples/getting_started/

Additional Reading

For further examples and config options, see the .NET SDK Reference.

For more information about Feature Flags, see our Feature Flags documentation.


Harness is a feature management platform that helps teams to build better software and to test features quicker.