Skip to content

Commit d83a107

Browse files
docs: Add .NET 10 Console sample application (#722)
* Add .NET 10 Console sample application Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com> * Address Gemini code review comments Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com> * Convert Console app to single file app Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com> --------- Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
1 parent 8a0ed59 commit d83a107

4 files changed

Lines changed: 98 additions & 3 deletions

File tree

OpenFeature.slnx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
<Folder Name="/samples/aspnetcore/">
4848
<Project Path="samples/AspNetCore/Samples.AspNetCore.csproj" />
4949
</Folder>
50+
<Folder Name="/samples/console/">
51+
<File Path="samples/Console/app.cs" />
52+
</Folder>
5053
<Folder Name="/src/">
5154
<Project Path="src/OpenFeature.Hosting/OpenFeature.Hosting.csproj" />
5255
<Project Path="src/OpenFeature.Providers.MultiProvider/OpenFeature.Providers.MultiProvider.csproj" />

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ public async Task Example()
8585

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

88-
| Sample Name | Description |
89-
| ------------------------------------------- | ----------------------------------------- |
90-
| [AspNetCore](/samples/AspNetCore/README.md) | Feature flags in an ASP.NET Core Web API. |
88+
| Sample Name | Description |
89+
| ----------------------------------------------------- | ----------------------------------------- |
90+
| [AspNetCore](/samples/AspNetCore/README.md) | Feature flags in an ASP.NET Core Web API. |
91+
| [Console](/samples/Console/README.md) | Feature flags in a .NET console app. |
9192

9293
**Getting Started with a Sample:**
9394

samples/Console/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# OpenFeature .NET Console Sample
2+
3+
This sample demonstrates how to use the OpenFeature .NET SDK in a console application. It includes a simple .NET 10 console app that defines several feature flags using the `InMemoryProvider` and evaluates them across all supported flag types: `bool`, `int`, `string`, `double`, and `object`.
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 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.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 Console sample project directory:
20+
21+
```shell
22+
cd openfeature-dotnet-sdk/samples/Console
23+
```
24+
25+
1. Run the following command to start the application:
26+
27+
```shell
28+
dotnet run app.cs
29+
```
30+
31+
## Feature Flags
32+
33+
The sample defines the following flags using the `InMemoryProvider`:
34+
35+
| Flag Key | Type | Variants | Default Variant |
36+
| -------------- | -------- | ----------------------------------------------------------------- | --------------- |
37+
| `bool-flag` | `bool` | `on``true`, `off``false` | `on` |
38+
| `numeric-flag` | `int` | `one``1`, `two``2` | `one` |
39+
| `string-flag` | `string` | `greeting``"Hello, World!"`, `farewell``"Goodbye, World!"` | `greeting` |
40+
| `float-flag` | `double` | `pi``3.14159`, `euler``0.577215` | `pi` |
41+
| `object-flag` | `object` | `user1``"Ralph"`, `user2``"Lewis"` | `user2` |
42+
43+
## NativeAOT
44+
45+
This sample is published with [NativeAOT](https://learn.microsoft.com/dotnet/core/deploying/native-aot/) enabled (`PublishAot=true`), demonstrating that the OpenFeature .NET SDK is fully compatible with NativeAOT compilation. See the [AOT Compatibility Guide](../../../docs/AOT_COMPATIBILITY.md) for more details.

samples/Console/app.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#:project ../../src/OpenFeature/OpenFeature.csproj
2+
#:property PublishAot=true
3+
4+
using OpenFeature;
5+
using OpenFeature.Model;
6+
using OpenFeature.Providers.Memory;
7+
8+
var flags = new Dictionary<string, Flag>
9+
{
10+
{ "bool-flag", new Flag<bool>(new Dictionary<string, bool> { { "on", true }, { "off", false } }, defaultVariant: "on") },
11+
{ "numeric-flag", new Flag<int>(new Dictionary<string, int> { { "one", 1 }, { "two", 2 } }, defaultVariant: "one") },
12+
{ "string-flag", new Flag<string>(new Dictionary<string, string> { { "greeting", "Hello, World!" }, { "farewell", "Goodbye, World!" } }, defaultVariant: "greeting") },
13+
{ "float-flag", new Flag<double>(new Dictionary<string, double> { { "pi", 3.14159 }, { "euler", 0.577215 } }, defaultVariant: "pi") },
14+
{ "object-flag", new Flag<Value>(new Dictionary<string, Value> { { "user1", new Value("Ralph") }, { "user2", new Value("Lewis") } }, defaultVariant: "user2" ) }
15+
};
16+
17+
await Api.Instance.SetProviderAsync(new InMemoryProvider(flags));
18+
19+
IFeatureClient client = Api.Instance.GetClient();
20+
21+
// Evaluate the `bool-flag` flag and print the result to the console
22+
var helloWorldResult = await client.GetBooleanValueAsync("bool-flag", false);
23+
if (helloWorldResult)
24+
{
25+
Console.WriteLine("The `bool-flag` flag was enabled!");
26+
}
27+
else
28+
{
29+
Console.WriteLine("The `bool-flag` flag was disabled!");
30+
}
31+
32+
// Evaluate the `numeric-flag` flag and print the result to the console
33+
var numericResult = await client.GetIntegerValueAsync("numeric-flag", 0);
34+
Console.WriteLine("The `numeric-flag` flag returned {0}", numericResult);
35+
36+
// Evaluate the `string-flag` flag and print the result to the console
37+
var stringResult = await client.GetStringValueAsync("string-flag", "default");
38+
Console.WriteLine("The `string-flag` flag returned {0}", stringResult);
39+
40+
// Evaluate the `float-flag` flag and print the result to the console
41+
var floatResult = await client.GetDoubleValueAsync("float-flag", 0.0);
42+
Console.WriteLine("The `float-flag` flag returned {0}", floatResult);
43+
44+
// Evaluate the `object-flag` flag and print the result to the console
45+
var objectResult = await client.GetObjectValueAsync("object-flag", new Value("Ben"));
46+
Console.WriteLine("The `object-flag` flag returned {0}", objectResult.AsString);

0 commit comments

Comments
 (0)