|
| 1 | +# Flagsmith Feature Flag .NET Provider |
| 2 | + |
| 3 | +The Flagsmith Flag provider allows you to connect to your Flagsmith instance. |
| 4 | + |
| 5 | +# .Net SDK usage |
| 6 | + |
| 7 | +## Install dependencies |
| 8 | + |
| 9 | +The first things we will do is install the **Open Feature SDK** and the **Flagsmith Feature Flag provider**. |
| 10 | + |
| 11 | +### .NET Cli |
| 12 | +```shell |
| 13 | +dotnet add package OpenFeature.Contrib.Providers.Flagsmith |
| 14 | +``` |
| 15 | +### Package Manager |
| 16 | + |
| 17 | +```shell |
| 18 | +NuGet\Install-Package OpenFeature.Contrib.Providers.Flagsmith |
| 19 | +``` |
| 20 | +### Package Reference |
| 21 | + |
| 22 | +```xml |
| 23 | +<PackageReference Include="OpenFeature.Contrib.Providers.Flagsmith" /> |
| 24 | +``` |
| 25 | +### Packet cli |
| 26 | + |
| 27 | +```shell |
| 28 | +paket add OpenFeature.Contrib.Providers.Flagsmith |
| 29 | +``` |
| 30 | + |
| 31 | +### Cake |
| 32 | + |
| 33 | +```shell |
| 34 | +// Install OpenFeature.Contrib.Providers.Flagsmith as a Cake Addin |
| 35 | +#addin nuget:?package=OpenFeature.Contrib.Providers.Flagsmith |
| 36 | + |
| 37 | +// Install OpenFeature.Contrib.Providers.Flagsmith as a Cake Tool |
| 38 | +#tool nuget:?package=OpenFeature.Contrib.Providers.Flagsmith |
| 39 | +``` |
| 40 | + |
| 41 | +## Using the Flagsmith Provider with the OpenFeature SDK |
| 42 | + |
| 43 | +To create a Flagmith provider you should define provider and Flagsmith settings. |
| 44 | + |
| 45 | +```csharp |
| 46 | +using OpenFeature.Contrib.Providers.Flagd; |
| 47 | + |
| 48 | +namespace OpenFeatureTestApp |
| 49 | +{ |
| 50 | + class Hello { |
| 51 | + static void Main(string[] args) { |
| 52 | + |
| 53 | + // Additional configs for provider |
| 54 | + var providerConfig = new FlagsmithProviderConfiguration(); |
| 55 | + |
| 56 | + //Flagsmith client configuration |
| 57 | + var flagsmithConfig = new FlagsmithConfiguration |
| 58 | + { |
| 59 | + ApiUrl = "https://edge.api.flagsmith.com/api/v1/", |
| 60 | + EnvironmentKey = string.Empty, |
| 61 | + EnableClientSideEvaluation = false, |
| 62 | + EnvironmentRefreshIntervalSeconds = 60, |
| 63 | + EnableAnalytics = false, |
| 64 | + Retries = 1 |
| 65 | + }; |
| 66 | + var flagsmithProvider = new FlagsmithProvider(providerConfig, flagsmithConfig);\ |
| 67 | + |
| 68 | + // Set the flagsmithProvider as the provider for the OpenFeature SDK |
| 69 | + OpenFeature.Api.Instance.SetProvider(flagsmithProvider); |
| 70 | + |
| 71 | + var client = OpenFeature.Api.Instance.GetClient("my-app"); |
| 72 | + |
| 73 | + var val = client.GetBooleanValue("myBoolFlag", false, null); |
| 74 | + |
| 75 | + // Print the value of the 'myBoolFlag' feature flag |
| 76 | + System.Console.WriteLine(val.Result.ToString()); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +You also can create Flagsmith provider using ```HttpClient``` or precreated ```FlagsmithClient``` |
| 83 | + |
| 84 | +```csharp |
| 85 | +using var httpClient = new HttpClient(); |
| 86 | +var flagsmithProvider = new FlagsmithProvider(providerConfig, config, httpClient); |
| 87 | +``` |
| 88 | +```csharp |
| 89 | +using var flagsmithClient = new FlagsmithClient(flagsmithOptions); |
| 90 | +var flagsmithProvider = new FlagsmithProvider(providerConfig, flagsmithClient); |
| 91 | +``` |
| 92 | +### Configuring the FlagsmithProvider |
| 93 | + |
| 94 | +To configure FlagsmithConfiguration just use [an example](https://github.com/Flagsmith/flagsmith-dotnet-client/tree/main/Example) from Flagsmith GitHub. |
| 95 | +For FlagsmithProviderConfiguration you can configure next parameters using custom implementation or just ```FlagsmithProviderConfiguration```: |
| 96 | +```csharp |
| 97 | +public interface IFlagsmithProviderConfiguration |
| 98 | +{ |
| 99 | + /// <summary> |
| 100 | + /// Key that will be used as identity for Flagsmith requests. |
| 101 | + /// </summary> |
| 102 | + public string TargetingKey { get; } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Determines whether to resolve a feature value as a boolean or use |
| 106 | + /// the isFeatureEnabled as the flag itself. These values will be false |
| 107 | + /// and true respectively. |
| 108 | + /// Default: false |
| 109 | + /// </summary> |
| 110 | + public bool UsingBooleanConfigValue { get; } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | + |
0 commit comments