diff --git a/OpenFeature.slnx b/OpenFeature.slnx index 8c3b8a2bf..d6778e50e 100644 --- a/OpenFeature.slnx +++ b/OpenFeature.slnx @@ -39,10 +39,17 @@ + + + + + + + diff --git a/README.md b/README.md index d21c111e6..ecaeb5257 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/build/Common.samples.props b/build/Common.samples.props new file mode 100644 index 000000000..a5b06c9b9 --- /dev/null +++ b/build/Common.samples.props @@ -0,0 +1,14 @@ + + + net9.0 + enable + enable + true + false + + + + + $(NoWarn);CA2007 + + diff --git a/samples/AspNetCore/Program.cs b/samples/AspNetCore/Program.cs new file mode 100644 index 000000000..462370861 --- /dev/null +++ b/samples/AspNetCore/Program.cs @@ -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>())) + .AddInMemoryProvider("InMemory", provider => new Dictionary() + { + { + "welcome-message", new Flag( + new Dictionary { { "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(); diff --git a/samples/AspNetCore/Properties/launchSettings.json b/samples/AspNetCore/Properties/launchSettings.json new file mode 100644 index 000000000..3c858af2b --- /dev/null +++ b/samples/AspNetCore/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/samples/AspNetCore/README.md b/samples/AspNetCore/README.md new file mode 100644 index 000000000..690206938 --- /dev/null +++ b/samples/AspNetCore/README.md @@ -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. diff --git a/samples/AspNetCore/Samples.AspNetCore.csproj b/samples/AspNetCore/Samples.AspNetCore.csproj new file mode 100644 index 000000000..01e452d77 --- /dev/null +++ b/samples/AspNetCore/Samples.AspNetCore.csproj @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/samples/AspNetCore/appsettings.Development.json b/samples/AspNetCore/appsettings.Development.json new file mode 100644 index 000000000..23fbf6b62 --- /dev/null +++ b/samples/AspNetCore/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "OpenFeature.*": "Information" + } + } +} diff --git a/samples/AspNetCore/appsettings.json b/samples/AspNetCore/appsettings.json new file mode 100644 index 000000000..10f68b8c8 --- /dev/null +++ b/samples/AspNetCore/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props new file mode 100644 index 000000000..7e9694ce7 --- /dev/null +++ b/samples/Directory.Build.props @@ -0,0 +1,3 @@ + + +