-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thomas Poignant <[email protected]> Signed-off-by: Thomas Poignant <[email protected]>
- Loading branch information
1 parent
01161cc
commit 5dcbed4
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
--- | ||
sidebar_position: 52 | ||
description: How to use the OpenFeature .Net SDK | ||
--- | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
|
||
|
||
# .Net SDK usage | ||
|
||
## Install dependencies | ||
|
||
The first things we will do is install the **Open Feature SDK** and the **GO Feature Flag provider**. | ||
|
||
<Tabs groupId="code"> | ||
<TabItem value="netcli" label=".NET CLI"> | ||
|
||
```shell | ||
dotnet add package OpenFeature.Contrib.Providers.GOFeatureFlag | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="pm" label="Package Manager"> | ||
|
||
```shell | ||
NuGet\Install-Package OpenFeature.Contrib.Providers.GOFeatureFlag | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="pr" label="Package Reference"> | ||
|
||
```xml | ||
<PackageReference Include="OpenFeature.Contrib.Providers.GOFeatureFlag" /> | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="pc" label="Packet cli"> | ||
|
||
```shell | ||
paket add OpenFeature.Contrib.Providers.GOFeatureFlag | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="cake" label="Cake"> | ||
|
||
```shell | ||
// Install OpenFeature.Contrib.Providers.GOFeatureFlag as a Cake Addin | ||
#addin nuget:?package=OpenFeature.Contrib.Providers.GOFeatureFlag | ||
|
||
// Install OpenFeature.Contrib.Providers.GOFeatureFlag as a Cake Tool | ||
#tool nuget:?package=OpenFeature.Contrib.Providers.GOFeatureFlag | ||
``` | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
## Initialize your Open Feature client | ||
|
||
To evaluate the flags you need to have an Open Feature configured in you app. | ||
This code block shows you how you can create a client that you can use in your application. | ||
|
||
<Tabs groupId="code"> | ||
<TabItem value="csharp" label="C#"> | ||
|
||
```csharp | ||
using OpenFeature; | ||
using OpenFeature.Contrib.Providers.GOFeatureFlag; | ||
|
||
// ... | ||
var goFeatureFlagProvider = new GoFeatureFlagProvider(new GoFeatureFlagProviderOptions | ||
{ | ||
Endpoint = "http://localhost:1031/", | ||
Timeout = new TimeSpan(1000 * TimeSpan.TicksPerMillisecond) | ||
}); | ||
Api.Instance.SetProvider(goFeatureFlagProvider); | ||
var client = Api.Instance.GetClient("my-app"); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Evaluate your flag | ||
|
||
This code block explain how you can create an `EvaluationContext` and use it to evaluate your flag. | ||
|
||
:::note | ||
In this example we are evaluating a `boolean` flag, but other types are available. | ||
|
||
**Refer to the [Open Feature documentation](https://docs.openfeature.dev/docs/reference/concepts/evaluation-api#basic-evaluation) to know more about it.** | ||
::: | ||
|
||
<Tabs groupId="code"> | ||
<TabItem value="csharp" label="C#"> | ||
|
||
```csharp | ||
// Context of your flag evaluation. | ||
// With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user. | ||
var userContext = EvaluationContext.Builder() | ||
.Set("targetingKey", "1d1b9238-2591-4a47-94cf-d2bc080892f1") // user unique identifier (mandatory) | ||
.Set("firstname", "john") | ||
.Set("lastname", "doe") | ||
.Set("email", "[email protected]") | ||
.Set("admin", true) // this field is used in the targeting rule of the flag "flag-only-for-admin" | ||
.Set("anonymous", false) | ||
.Build(); | ||
|
||
var adminFlag = await client.GetBooleanValue("flag-only-for-admin", false, userContext); | ||
if (adminFlag) { | ||
// flag "flag-only-for-admin" is true for the user | ||
} else { | ||
// flag "flag-only-for-admin" is false for the user | ||
} | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> |