1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/website/docs/reference/sdks/dotnet.md

117 lines
4.4 KiB
Markdown
Raw Normal View History

---
title: .NET SDK
---
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
In this guide we explain how to use feature toggles in a .NET application using Unleash-hosted. We will be using the open source Unleash [.net Client SDK](https://github.com/Unleash/unleash-client-dotnet).
refactor: move docs into new structure / fix links for SEO (#2416) ## What This (admittedly massive) PR updates the "physical" documentation structure and fixes url inconsistencies and SEO problems reported by marketing. The main points are: - remove or move directories : advanced, user_guide, deploy, api - move the files contained within to the appropriate one of topics, how-to, tutorials, or reference - update internal doc links and product links to the content - create client-side redirects for all the urls that have changed. A number of the files have been renamed in small ways to better match their url and to make them easier to find. Additionally, the top-level api directory has been moved to /reference/api/legacy/unleash (see the discussion points section for more on this). ## Why When moving our doc structure to diataxis a while back, we left the "physical' files lying where they were, because it didn't matter much to the new structure. However, that did introduce some inconsistencies with where you place docs and how we organize them. There's also the discrepancies in whether urls us underscores or hyphens (which isn't necessarily the same as their file name), which has been annoying me for a while, but now has also been raised by marketing as an issue in terms of SEO. ## Discussion points The old, hand-written API docs have been moved from /api to /reference/api/legacy/unleash. There _is_ a /reference/api/unleash directory, but this is being populated by the OpenAPI plugin, and mixing those could only cause trouble. However, I'm unsure about putting /legacy/ in the title, because the API isn't legacy, the docs are. Maybe we could use another path? Like /old-docs/ or something? I'd appreciate some input on this.
2022-11-22 10:05:30 +01:00
> You will need your `API URL` and your `API token` in order to connect the Client SDK to you Unleash instance. You can find this information in the “Admin” section Unleash management UI. [Read more](../../how-to/how-to-create-api-tokens.mdx)
## Step 1: Install client SDK {#step-1-install-client-sdk}
First we must add Unleash Client SDK as a dependency to your project. Below is an example of how you would add it via the .NET cli. Please see [NuGet](https://www.nuget.org/packages/Unleash.Client/) for other alternatives.
```sh
dotnet add package unleash.client
```
## Step 2: Create a new Unleash Instance {#step-2-create-a-new-unleash-instance}
Next we must initialize a new instance of the Unleash Client.
:::tip Synchronous initialization
By default, the client SDK asynchronously fetches toggles from the Unleash API on initialization. This means it can take a few hundred milliseconds for the client to reach the correct state.
You can use the `synchronousInitialization` option of the `UnleashClientFactory` class's `CreateClientAsync` method to block the client until it has successfully synced with the server. See the following "synchronous initialization" code sample.
Read more about the [Unleash architecture](https://www.getunleash.io/blog/our-unique-architecture) to learn how it works.
:::
<Tabs>
<TabItem value="async" label="Asynchronous initialization" default>
```csharp
var settings = new UnleashSettings()
{
AppName = "dot-net-client",
Environment = "local",
UnleashApi = new Uri("API URL"),
CustomHttpHeaders = new Dictionary()
{
2021-05-21 22:26:56 +02:00
{"Authorization","API token" }
}
};
IUnleash unleash = new DefaultUnleash(settings);
```
</TabItem>
<TabItem value="sync" label="Synchronous initializiation">
```csharp
var settings = new UnleashSettings()
{
AppName = "dot-net-client",
Environment = "local",
UnleashApi = new Uri("API URL"),
CustomHttpHeaders = new Dictionary()
{
{"Authorization","API token" }
}
};
var unleashFactory = new UnleashClientFactory();
// this `unleash` will fetch feature toggles and write them to its cache before returning from the await call.
// if network errors or disk permissions prevent this from happening, the await will throw an exception.
IUnleash unleash = await unleashFactory.CreateClientAsync(settings, synchronousInitialization: true);
```
</TabItem>
</Tabs>
2021-05-04 16:08:21 +02:00
In your app you typically just want one instance of Unleash, and inject that where you need it.
2021-05-21 22:26:56 +02:00
You should change the URL and the Authorization header (API token) with the correct values for your instance, which you may locate under “Instance admin” in the menu.
## Step 3: Use the feature toggle {#step-3-use-the-feature-toggle}
2021-05-04 16:08:21 +02:00
Now that we have initialized the client SDK we can start using feature toggles defined in Unleash in our application. To achieve this we have the “isEnabled” method available, which will allow us to check the value of a feature toggle. This method will return true or false based on whether the feature should be enabled or disabled for the current request.
```csharp
2021-05-04 16:08:21 +02:00
if (unleash.IsEnabled("Demo"))
{
//do some magic
2021-05-04 16:08:21 +02:00
}
else
{
//do old boring stuff
}
```
## Step 4: Provide Unleash Context {#step-4-provide-unleash-context}
2021-05-04 16:08:21 +02:00
It is the client SDK that computes whether a feature toggle should be considered enabled or disabled for specific use request. This is the job of the activation strategies, which are implemented in the client SDK.
The activation strategies is an implementation of rules based on data, which you provide as part of the Unleash Context.
**a) As argument to the isEnabled call**
The simplest way to provide the Unleash Context is as part of the “isEnabled” call:
```csharp
var context = new UnleashContext
{
UserId = "61"
};
unleash.IsEnabled("someToggle", context);
```
b) Via a UnleashContextProvider
This is a bit more advanced approach, where you configure an unleash-context provider. By doing this, you do not have to rebuild or to pass the unleash-context object to every place you are calling `unleash.IsEnabled`. You can read more, and get [examples about this option on GitHub](https://github.com/Unleash/unleash-client-dotnet#unleashcontextprovider).