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
3 changes: 3 additions & 0 deletions OpenFeature.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<Folder Name="/samples/aspnetcore/">
<Project Path="samples/AspNetCore/Samples.AspNetCore.csproj" />
</Folder>
<Folder Name="/samples/console/">
<File Path="samples/Console/app.cs" />
</Folder>
<Folder Name="/src/">
<Project Path="src/OpenFeature.Hosting/OpenFeature.Hosting.csproj" />
<Project Path="src/OpenFeature.Providers.MultiProvider/OpenFeature.Providers.MultiProvider.csproj" />
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ public async Task Example()

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. |
| Sample Name | Description |
| ----------------------------------------------------- | ----------------------------------------- |
| [AspNetCore](/samples/AspNetCore/README.md) | Feature flags in an ASP.NET Core Web API. |
| [Console](/samples/Console/README.md) | Feature flags in a .NET console app. |

**Getting Started with a Sample:**

Expand Down
45 changes: 45 additions & 0 deletions samples/Console/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# OpenFeature .NET Console Sample

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`.

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 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.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 Console sample project directory:

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

1. Run the following command to start the application:

```shell
dotnet run app.cs
```

## Feature Flags

The sample defines the following flags using the `InMemoryProvider`:

| Flag Key | Type | Variants | Default Variant |
| -------------- | -------- | ----------------------------------------------------------------- | --------------- |
| `bool-flag` | `bool` | `on` → `true`, `off` → `false` | `on` |
| `numeric-flag` | `int` | `one` → `1`, `two` → `2` | `one` |
| `string-flag` | `string` | `greeting` → `"Hello, World!"`, `farewell` → `"Goodbye, World!"` | `greeting` |
| `float-flag` | `double` | `pi` → `3.14159`, `euler` → `0.577215` | `pi` |
| `object-flag` | `object` | `user1` → `"Ralph"`, `user2` → `"Lewis"` | `user2` |

## NativeAOT

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.
46 changes: 46 additions & 0 deletions samples/Console/app.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#:project ../../src/OpenFeature/OpenFeature.csproj
#:property PublishAot=true

using OpenFeature;
using OpenFeature.Model;
using OpenFeature.Providers.Memory;

var flags = new Dictionary<string, Flag>
{
{ "bool-flag", new Flag<bool>(new Dictionary<string, bool> { { "on", true }, { "off", false } }, defaultVariant: "on") },
{ "numeric-flag", new Flag<int>(new Dictionary<string, int> { { "one", 1 }, { "two", 2 } }, defaultVariant: "one") },
{ "string-flag", new Flag<string>(new Dictionary<string, string> { { "greeting", "Hello, World!" }, { "farewell", "Goodbye, World!" } }, defaultVariant: "greeting") },
{ "float-flag", new Flag<double>(new Dictionary<string, double> { { "pi", 3.14159 }, { "euler", 0.577215 } }, defaultVariant: "pi") },
{ "object-flag", new Flag<Value>(new Dictionary<string, Value> { { "user1", new Value("Ralph") }, { "user2", new Value("Lewis") } }, defaultVariant: "user2" ) }
};

await Api.Instance.SetProviderAsync(new InMemoryProvider(flags));

IFeatureClient client = Api.Instance.GetClient();

// Evaluate the `bool-flag` flag and print the result to the console
var helloWorldResult = await client.GetBooleanValueAsync("bool-flag", false);
if (helloWorldResult)
{
Console.WriteLine("The `bool-flag` flag was enabled!");
}
else
{
Console.WriteLine("The `bool-flag` flag was disabled!");
}

// Evaluate the `numeric-flag` flag and print the result to the console
var numericResult = await client.GetIntegerValueAsync("numeric-flag", 0);
Console.WriteLine("The `numeric-flag` flag returned {0}", numericResult);

// Evaluate the `string-flag` flag and print the result to the console
var stringResult = await client.GetStringValueAsync("string-flag", "default");
Console.WriteLine("The `string-flag` flag returned {0}", stringResult);

// Evaluate the `float-flag` flag and print the result to the console
var floatResult = await client.GetDoubleValueAsync("float-flag", 0.0);
Console.WriteLine("The `float-flag` flag returned {0}", floatResult);

// Evaluate the `object-flag` flag and print the result to the console
var objectResult = await client.GetObjectValueAsync("object-flag", new Value("Ben"));
Console.WriteLine("The `object-flag` flag returned {0}", objectResult.AsString);