|
| 1 | +# Unleash .NET Provider |
| 2 | + |
| 3 | +The Unleash provider allows you to use [Unleash](https://www.getunleash.io/) with the OpenFeature .NET SDK. |
| 4 | + |
| 5 | +# .Net SDK usage |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- open-feature/dotnet-sdk v2.x |
| 10 | +- Unleash .NET SDK v6.x |
| 11 | + |
| 12 | +## Install dependencies |
| 13 | + |
| 14 | +The first thing we will do is install the **OpenFeature SDK** and the **Unleash Feature Flag provider**. |
| 15 | + |
| 16 | +### .NET Cli |
| 17 | + |
| 18 | +```shell |
| 19 | +dotnet add package OpenFeature.Contrib.Providers.Unleash |
| 20 | +``` |
| 21 | + |
| 22 | +### Package Manager |
| 23 | + |
| 24 | +```shell |
| 25 | +NuGet\Install-Package OpenFeature.Contrib.Providers.Unleash |
| 26 | +``` |
| 27 | + |
| 28 | +### Package Reference |
| 29 | + |
| 30 | +```xml |
| 31 | +<PackageReference Include="OpenFeature.Contrib.Providers.Unleash" /> |
| 32 | +``` |
| 33 | + |
| 34 | +### Packet cli |
| 35 | + |
| 36 | +```shell |
| 37 | +paket add OpenFeature.Contrib.Providers.Unleash |
| 38 | +``` |
| 39 | + |
| 40 | +### Cake |
| 41 | + |
| 42 | +```shell |
| 43 | +// Install OpenFeature.Contrib.Providers.Unleash as a Cake Addin |
| 44 | +#addin nuget:?package=OpenFeature.Contrib.Providers.Unleash |
| 45 | + |
| 46 | +// Install OpenFeature.Contrib.Providers.Unleash as a Cake Tool |
| 47 | +#tool nuget:?package=OpenFeature.Contrib.Providers.Unleash |
| 48 | +``` |
| 49 | + |
| 50 | +## Using the Unleash Provider with the OpenFeature SDK |
| 51 | + |
| 52 | +```csharp |
| 53 | +using OpenFeature; |
| 54 | +using OpenFeature.Contrib.Providers.Unleash; |
| 55 | +using Unleash; |
| 56 | + |
| 57 | +var settings = new UnleashSettings |
| 58 | +{ |
| 59 | + AppName = "my-app", |
| 60 | + UnleashApi = new Uri("http://localhost:4242/api/"), |
| 61 | + CustomHttpHeaders = new Dictionary<string, string> |
| 62 | + { |
| 63 | + { "Authorization", "*:development.your-api-token" } |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +var provider = new UnleashProvider(settings); |
| 68 | + |
| 69 | +// Set the provider for the OpenFeature SDK |
| 70 | +await Api.Instance.SetProviderAsync(provider); |
| 71 | + |
| 72 | +// Get an OpenFeature client |
| 73 | +var client = Api.Instance.GetClient(); |
| 74 | + |
| 75 | +// Boolean evaluation (uses IsEnabled) |
| 76 | +var enabled = await client.GetBooleanValueAsync("my-feature", false); |
| 77 | + |
| 78 | +// String evaluation (uses variant payload) |
| 79 | +var value = await client.GetStringValueAsync("my-variant-flag", "default"); |
| 80 | + |
| 81 | +// Integer evaluation (parses variant payload) |
| 82 | +var count = await client.GetIntegerValueAsync("my-int-flag", 0); |
| 83 | + |
| 84 | +// Double evaluation (parses variant payload) |
| 85 | +var rate = await client.GetDoubleValueAsync("my-double-flag", 0.0); |
| 86 | +``` |
| 87 | + |
| 88 | +## EvaluationContext and Unleash Context relationship |
| 89 | + |
| 90 | +The provider maps OpenFeature `EvaluationContext` fields to `UnleashContext`: |
| 91 | + |
| 92 | +| EvaluationContext Key | Unleash Context Field | |
| 93 | +|-----------------------|------------------------| |
| 94 | +| `TargetingKey` | `UserId` | |
| 95 | +| `sessionId` | `SessionId` | |
| 96 | +| `remoteAddress` | `RemoteAddress` | |
| 97 | +| `environment` | `Environment` | |
| 98 | +| `appName` | `AppName` | |
| 99 | +| `currentTime` | `CurrentTime` | |
| 100 | +| All other keys | `Properties` | |
| 101 | + |
| 102 | +## Variant payload type metadata |
| 103 | + |
| 104 | +When evaluating variants (string, integer, double, structure), the provider exposes the Unleash payload `type` field (e.g., `"string"`, `"number"`, `"json"`, `"csv"`) as `payload-type` in the resolution details flag metadata. |
| 105 | + |
| 106 | +## Events |
| 107 | + |
| 108 | +The provider emits `ProviderConfigurationChanged` events when Unleash fires `TogglesUpdatedEvent` (i.e., when toggle state is refreshed from the server). |
| 109 | + |
| 110 | +## Known issues and limitations |
| 111 | + |
| 112 | +- The provider does not accept an external `IUnleash` instance because lifecycle events (`ReadyEvent`, `ErrorEvent`, `TogglesUpdatedEvent`) can only be subscribed during client construction. |
0 commit comments