Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions OpenFeature.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@
<Folder Name="/.root/build/">
<File Path="build/Common.prod.props" />
<File Path="build/Common.props" />
<File Path="build/Common.samples.props" />
<File Path="build/Common.tests.props" />
<File Path="build/openfeature-icon.png" />
<File Path="build/xunit.runner.json" />
</Folder>
<Folder Name="/samples/">
<File Path="samples/Directory.Build.props" />
</Folder>
<Folder Name="/samples/aspnetcore/">
<Project Path="samples/AspNetCore/Samples.AspNetCore.csproj" />
</Folder>
<Folder Name="/src/">
<File Path="src/Directory.Build.props" />
<File Path="src/Directory.Build.targets" />
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ public async Task Example()
}
```

### Samples

The [`samples/`](./samples) folder contains example applications demonstrating how to use OpenFeature in different .NET scenarios.

| Sample Name | Description |
|---------------------------------------------------|----------------------------------------------------------------|
| [AspNetCore](/samples/AspNetCore/README.md) | Feature flags in an ASP.NET Core Web API. |

**Getting Started with a Sample:**

1. Navigate to the sample directory

```shell
cd samples/AspNetCore
```

2. Restore dependencies and run

```shell
dotnet run
```

Want to contribute a new sample? See our [CONTRIBUTING](#-contributing) guide!

## 🌟 Features

| Status | Features | Description |
Expand Down
14 changes: 14 additions & 0 deletions build/Common.samples.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>

<!-- Stops warning to use the .ConfigureAwait method. Tests should configure the await context as opposed to library code -->
<PropertyGroup>
<NoWarn>$(NoWarn);CA2007</NoWarn>
</PropertyGroup>
</Project>
42 changes: 42 additions & 0 deletions samples/AspNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Mvc;
using OpenFeature;
using OpenFeature.DependencyInjection.Providers.Memory;
using OpenFeature.Hooks;
using OpenFeature.Providers.Memory;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddProblemDetails();

builder.Services.AddOpenFeature(builder =>
{
builder.AddHostedFeatureLifecycle()
.AddHook(sp => new LoggingHook(sp.GetRequiredService<ILogger<LoggingHook>>()))
.AddInMemoryProvider("InMemory", provider => new Dictionary<string, Flag>()
{
{
"welcome-message", new Flag<bool>(
new Dictionary<string, bool> { { "show", true }, { "hide", false } }, "show")
}
});
});

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseExceptionHandler();

app.MapGet("/welcome", async ([FromServices] IFeatureClient featureClient) =>
{
var welcomeMessageEnabled = await featureClient.GetBooleanValueAsync("welcome-message", false);

if (welcomeMessageEnabled)
{
return TypedResults.Ok("Hello world! The welcome-message feature flag was enabled!");
}

return TypedResults.Ok("Hello world!");
});

app.Run();
25 changes: 25 additions & 0 deletions samples/AspNetCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "welcome",
"applicationUrl": "http://localhost:5412",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "welcome",
"applicationUrl": "https://localhost:7381;http://localhost:5412",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
35 changes: 35 additions & 0 deletions samples/AspNetCore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# OpenFeature Dotnet Web Sample

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.

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.

## Prerequisites

- [.NET 9 SDK](https://dotnet.microsoft.com/download/dotnet/9.0) installed on your machine.

## Setup

1. Clone the repository:

```shell
git clone https://github.com/open-feature/dotnet-sdk.git openfeature-dotnet-sdk
```

1. Navigate to the Web sample project directory:

```shell
cd openfeature-dotnet-sdk/samples/AspNetCore
```

1. Run the following command to start the application:

```shell
dotnet run
```

1. Open your web browser and navigate to `http://localhost:5412/welcome` to see the application in action.

### Enable OpenFeature debug logging

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.
9 changes: 9 additions & 0 deletions samples/AspNetCore/Samples.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<ProjectReference Include="..\..\src\OpenFeature.DependencyInjection\OpenFeature.DependencyInjection.csproj" />
<ProjectReference Include="..\..\src\OpenFeature.Hosting\OpenFeature.Hosting.csproj" />
<ProjectReference Include="..\..\src\OpenFeature\OpenFeature.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions samples/AspNetCore/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"OpenFeature.*": "Information"
}
}
}
9 changes: 9 additions & 0 deletions samples/AspNetCore/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
3 changes: 3 additions & 0 deletions samples/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'OpenFeature.slnx'))\build\Common.samples.props" />
</Project>