-
Notifications
You must be signed in to change notification settings - Fork 21
docs: Add AspNetCore sample app #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
askpt
merged 4 commits into
open-feature:main
from
kylejuliandev:feature/add-aspnetcore-sample-app
May 21, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <Project> | ||
| <!-- 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally> | ||
| </PropertyGroup> | ||
askpt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <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> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning", | ||
| "OpenFeature.*": "Information" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.