Skip to content

Commit 9742a0d

Browse files
docs: Add AspNetCore sample app (#477)
<!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR <!-- add the description of the PR here --> - Adds simple a AspNetCore web application with a `/welcome` minimal API that will show one of two messages based the configuration of the `InMemory` feature provider. ### Related Issues <!-- add here the GitHub issue that this PR resolves if applicable --> ### Notes <!-- any additional notes for this PR --> While the [README](https://github.com/open-feature/dotnet-sdk/blob/6c44db9237748735a22a162a88f61b2de7f3e9bd/README.md) for OpenFeature dotnet is pretty comprehensive, I think it can be quite intimidating to developers who are unfamilar with feature flagging and OpenFeature. Providing a basic application that can be cloned locally and run with minimal setup would improve the onboarding and upskill experience. Additionally, as a contributor I find myself frequently creating simple applications to test out changes to OpenFeature. This would save the effort of stashing local changes and make it easier to more quickly test or prototype changes. The example could be extended with endpoints that explore the other types of feature flags and how you might utilise them within an application. ### Follow-up Tasks <!-- anything that is related to this PR but not done here should be noted under this section --> <!-- if there is a need for a new issue, please link it here --> Update the [README](https://github.com/open-feature/dotnet-sdk/blob/6c44db9237748735a22a162a88f61b2de7f3e9bd/README.md) with a reference to the sample application to help guide developers. ### How to test <!-- if applicable, add testing instructions under this section --> --------- Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
1 parent 6c44db9 commit 9742a0d

File tree

10 files changed

+177
-0
lines changed

10 files changed

+177
-0
lines changed

OpenFeature.slnx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@
3939
<Folder Name="/.root/build/">
4040
<File Path="build/Common.prod.props" />
4141
<File Path="build/Common.props" />
42+
<File Path="build/Common.samples.props" />
4243
<File Path="build/Common.tests.props" />
4344
<File Path="build/openfeature-icon.png" />
4445
<File Path="build/xunit.runner.json" />
4546
</Folder>
47+
<Folder Name="/samples/">
48+
<File Path="samples/Directory.Build.props" />
49+
</Folder>
50+
<Folder Name="/samples/aspnetcore/">
51+
<Project Path="samples/AspNetCore/Samples.AspNetCore.csproj" />
52+
</Folder>
4653
<Folder Name="/src/">
4754
<File Path="src/Directory.Build.props" />
4855
<File Path="src/Directory.Build.targets" />

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ public async Task Example()
7575
}
7676
```
7777

78+
### Samples
79+
80+
The [`samples/`](./samples) folder contains example applications demonstrating how to use OpenFeature in different .NET scenarios.
81+
82+
| Sample Name | Description |
83+
|---------------------------------------------------|----------------------------------------------------------------|
84+
| [AspNetCore](/samples/AspNetCore/README.md) | Feature flags in an ASP.NET Core Web API. |
85+
86+
**Getting Started with a Sample:**
87+
88+
1. Navigate to the sample directory
89+
90+
```shell
91+
cd samples/AspNetCore
92+
```
93+
94+
2. Restore dependencies and run
95+
96+
```shell
97+
dotnet run
98+
```
99+
100+
Want to contribute a new sample? See our [CONTRIBUTING](#-contributing) guide!
101+
78102
## 🌟 Features
79103

80104
| Status | Features | Description |

build/Common.samples.props

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project>
2+
<PropertyGroup>
3+
<TargetFramework>net9.0</TargetFramework>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
7+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
8+
</PropertyGroup>
9+
10+
<!-- Stops warning to use the .ConfigureAwait method. Tests should configure the await context as opposed to library code -->
11+
<PropertyGroup>
12+
<NoWarn>$(NoWarn);CA2007</NoWarn>
13+
</PropertyGroup>
14+
</Project>

samples/AspNetCore/Program.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using OpenFeature;
3+
using OpenFeature.DependencyInjection.Providers.Memory;
4+
using OpenFeature.Hooks;
5+
using OpenFeature.Providers.Memory;
6+
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
builder.Services.AddProblemDetails();
11+
12+
builder.Services.AddOpenFeature(builder =>
13+
{
14+
builder.AddHostedFeatureLifecycle()
15+
.AddHook(sp => new LoggingHook(sp.GetRequiredService<ILogger<LoggingHook>>()))
16+
.AddInMemoryProvider("InMemory", provider => new Dictionary<string, Flag>()
17+
{
18+
{
19+
"welcome-message", new Flag<bool>(
20+
new Dictionary<string, bool> { { "show", true }, { "hide", false } }, "show")
21+
}
22+
});
23+
});
24+
25+
var app = builder.Build();
26+
27+
// Configure the HTTP request pipeline.
28+
app.UseExceptionHandler();
29+
30+
app.MapGet("/welcome", async ([FromServices] IFeatureClient featureClient) =>
31+
{
32+
var welcomeMessageEnabled = await featureClient.GetBooleanValueAsync("welcome-message", false);
33+
34+
if (welcomeMessageEnabled)
35+
{
36+
return TypedResults.Ok("Hello world! The welcome-message feature flag was enabled!");
37+
}
38+
39+
return TypedResults.Ok("Hello world!");
40+
});
41+
42+
app.Run();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"launchUrl": "welcome",
9+
"applicationUrl": "http://localhost:5412",
10+
"environmentVariables": {
11+
"ASPNETCORE_ENVIRONMENT": "Development"
12+
}
13+
},
14+
"https": {
15+
"commandName": "Project",
16+
"dotnetRunMessages": true,
17+
"launchBrowser": true,
18+
"launchUrl": "welcome",
19+
"applicationUrl": "https://localhost:7381;http://localhost:5412",
20+
"environmentVariables": {
21+
"ASPNETCORE_ENVIRONMENT": "Development"
22+
}
23+
}
24+
}
25+
}

samples/AspNetCore/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# OpenFeature Dotnet Web Sample
2+
3+
This sample demonstrates how to use the OpenFeature .NET Web Application. It includes a simple .NET 9 web application that retrieves and evaluates feature flags using the OpenFeature client. The sample is set up with the `InMemoryProvider` and a relatively simple boolean `welcome-message` feature flag.
4+
5+
The sample can easily be extended with alternative providers, which you can find in the [dotnet-sdk-contrib](https://github.com/open-feature/dotnet-sdk-contrib) repository.
6+
7+
## Prerequisites
8+
9+
- [.NET 9 SDK](https://dotnet.microsoft.com/download/dotnet/9.0) installed on your machine.
10+
11+
## Setup
12+
13+
1. Clone the repository:
14+
15+
```shell
16+
git clone https://github.com/open-feature/dotnet-sdk.git openfeature-dotnet-sdk
17+
```
18+
19+
1. Navigate to the Web sample project directory:
20+
21+
```shell
22+
cd openfeature-dotnet-sdk/samples/AspNetCore
23+
```
24+
25+
1. Run the following command to start the application:
26+
27+
```shell
28+
dotnet run
29+
```
30+
31+
1. Open your web browser and navigate to `http://localhost:5412/welcome` to see the application in action.
32+
33+
### Enable OpenFeature debug logging
34+
35+
You can enable OpenFeature debug logging by setting the `Logging:LogLevel:OpenFeature.*` setting in [appsettings.Development.json](appsettings.Development.json) to `Debug`. This will provide detailed logs of the OpenFeature SDK's operations, which can be helpful for troubleshooting and understanding how feature flags are being evaluated.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\src\OpenFeature.DependencyInjection\OpenFeature.DependencyInjection.csproj" />
5+
<ProjectReference Include="..\..\src\OpenFeature.Hosting\OpenFeature.Hosting.csproj" />
6+
<ProjectReference Include="..\..\src\OpenFeature\OpenFeature.csproj" />
7+
</ItemGroup>
8+
9+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"OpenFeature.*": "Information"
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

samples/Directory.Build.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Project>
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'OpenFeature.slnx'))\build\Common.samples.props" />
3+
</Project>

0 commit comments

Comments
 (0)