Skip to content

Commit a797824

Browse files
authored
docs: update dotnet workshop docs (#1289)
Signed-off-by: André Silva <[email protected]> <!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR This pull request updates the .NET getting started tutorial to use the latest OpenFeature library patterns and dependency injection, improving clarity and aligning with modern best practices. The most important changes are grouped below: **Dependency Injection and Library Usage Updates:** * Added installation instructions for the `OpenFeature.Hosting` NuGet package to support dependency injection and modern OpenFeature usage. * Updated provider registration to use `builder.Services.AddOpenFeature` with lambda configuration, replacing direct calls to `Api.Instance.SetProviderAsync` for both in-memory and flagd providers. [[1]](diffhunk://#diff-a8a7045d253a6bda66f8b29be80b12bc61d24e1b8fda49b96d4cbb117265257aL67-R84) [[2]](diffhunk://#diff-a8a7045d253a6bda66f8b29be80b12bc61d24e1b8fda49b96d4cbb117265257aR187-R201) **Code Modernization and API Usage:** * Changed feature flag evaluation in route handlers to inject `IFeatureClient` via `[FromServices]`, removing manual client instantiation and improving testability and code clarity. [[1]](diffhunk://#diff-a8a7045d253a6bda66f8b29be80b12bc61d24e1b8fda49b96d4cbb117265257aL105-R118) [[2]](diffhunk://#diff-a8a7045d253a6bda66f8b29be80b12bc61d24e1b8fda49b96d4cbb117265257aL193-R213) **Provider Integration Improvements:** * Updated flagd provider integration to use `OpenFeature.Contrib.Providers.Flagd.DependencyInjection` and the `.AddFlagdProvider()` extension for simpler configuration. [[1]](diffhunk://#diff-a8a7045d253a6bda66f8b29be80b12bc61d24e1b8fda49b96d4cbb117265257aL165-R174) [[2]](diffhunk://#diff-a8a7045d253a6bda66f8b29be80b12bc61d24e1b8fda49b96d4cbb117265257aR187-R201) --------- Signed-off-by: André Silva <[email protected]>
1 parent cb1961c commit a797824

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

docs/tutorials/getting-started/dotnet.mdx

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ With NuGet you can install the latest [OpenFeature](https://www.nuget.org/packag
4545

4646
```shell
4747
dotnet add package OpenFeature
48+
dotnet add package OpenFeature.Hosting
4849
```
4950

5051
### Step 3: Add code
@@ -54,7 +55,9 @@ Open a code editor and add the C# code below to the Program.cs.
5455

5556
```csharp
5657
// diff-add-block-start
58+
using Microsoft.AspNetCore.Mvc;
5759
using OpenFeature;
60+
using OpenFeature.Hosting.Providers.Memory;
5861
using OpenFeature.Providers.Memory;
5962
// diff-add-block-end
6063
@@ -64,13 +67,23 @@ var builder = WebApplication.CreateBuilder(args);
6467
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
6568
builder.Services.AddOpenApi();
6669

67-
var app = builder.Build();
68-
6970
// diff-add-block-start
7071
// Register your feature flag provider
71-
await Api.Instance.SetProviderAsync(new InMemoryProvider());
72+
builder.Services.AddOpenFeature(featureBuilder =>
73+
{
74+
featureBuilder
75+
.AddInMemoryProvider(_ => new Dictionary<string, Flag>()
76+
{
77+
{
78+
"welcome-message", new Flag<bool>(
79+
new Dictionary<string, bool> { { "show", true }, { "hide", false } }, "show")
80+
}
81+
});
82+
});
7283
// diff-add-block-end
7384
85+
var app = builder.Build();
86+
7487
// Configure the HTTP request pipeline.
7588
if (app.Environment.IsDevelopment())
7689
{
@@ -102,11 +115,9 @@ app.MapGet("/weatherforecast", () =>
102115
.WithName("GetWeatherForecast");
103116
//diff-remove-block-end
104117
//diff-add-block-start
105-
app.MapGet("/hello", async () =>
118+
app.MapGet("/hello", async ([FromServices] IFeatureClient featureClient) =>
106119
{
107-
var client = Api.Instance.GetClient();
108-
109-
if (await client.GetBooleanValueAsync("welcome-message", false))
120+
if (await featureClient.GetBooleanValueAsync("welcome-message", false))
110121
{
111122
return "Hello, welcome to this OpenFeature-enabled website!";
112123
}
@@ -160,11 +171,15 @@ Finally, let's add the required code change to enable the flagd provider in our
160171

161172
```csharp
162173

174+
using Microsoft.AspNetCore.Mvc;
163175
using OpenFeature;
164-
//diff-add
165-
using OpenFeature.Contrib.Providers.Flagd
166-
//diff-remove
176+
//diff-add-block-start
177+
using OpenFeature.DependencyInjection.Providers.Flagd;
178+
//diff-add-block-end
179+
//diff-remove-block-start
167180
using OpenFeature.Providers.Memory;
181+
using OpenFeature.Hosting.Providers.Memory;
182+
//diff-remove-block-end
168183
169184
var builder = WebApplication.CreateBuilder(args);
170185

@@ -175,12 +190,20 @@ builder.Services.AddOpenApi();
175190
var app = builder.Build();
176191

177192
// Register your feature flag provider
178-
//diff-remove
179-
await Api.Instance.SetProviderAsync(new InMemoryProvider());
193+
builder.Services.AddOpenFeature(featureBuilder =>
194+
{
195+
featureBuilder
196+
.AddInMemoryProvider(_ => new Dictionary<string, Flag>()
197+
{
198+
{
199+
"welcome-message", new Flag<bool>(
200+
new Dictionary<string, bool> { { "show", true }, { "hide", false } }, "show")
201+
}
202+
});
180203
//diff-add-block-start
181-
var flagdProvider = new FlagdProvider(new Uri("http://localhost:8013"));
182-
await Api.Instance.SetProviderAsync(flagdProvider);
204+
.AddFlagdProvider();
183205
//diff-add-block-end
206+
});
184207

185208
// Configure the HTTP request pipeline.
186209
if (app.Environment.IsDevelopment())
@@ -190,11 +213,9 @@ if (app.Environment.IsDevelopment())
190213

191214
app.UseHttpsRedirection();
192215

193-
app.MapGet("/hello", async () =>
216+
app.MapGet("/hello", async ([FromServices] IFeatureClient featureClient) =>
194217
{
195-
var client = Api.Instance.GetClient();
196-
197-
if (await client.GetBooleanValueAsync("welcome-message", false))
218+
if (await featureClient.GetBooleanValueAsync("welcome-message", false))
198219
{
199220
return "Hello, welcome to this OpenFeature-enabled website!";
200221
}

0 commit comments

Comments
 (0)