Skip to content
Open
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
41 changes: 41 additions & 0 deletions .github/workflows/openfeature-server-provider.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: openfeature-server-provider
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 9 * * *'
push:
branches: [ main, 'feat/**' ]
paths:
- 'openfeature-server-provider/**'
- '.github/workflows/openfeature-server-provider.yml'
pull_request:
branches: [ main, 'feat/**' ]
paths:
- 'openfeature-server-provider/**'
- '.github/workflows/openfeature-server-provider.yml'

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
fail-fast: false
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3

- name: Setup dotnet build tools
uses: actions/setup-dotnet@9a946fdbd5fb07b82b2f5a4466058b876ab72bb2 # v5.3.0
with:
dotnet-version: 8.0

- name: Build openfeature-server-provider examples
shell: bash
run: |
set -e
for proj in $(find openfeature-server-provider -name '*.csproj' | sort); do
echo "::group::Building $proj"
dotnet build "$proj"
echo "::endgroup::"
done
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ or the [.NET SDK reference guide](https://docs.launchdarkly.com/sdk/server-side/

## SDKs

| SDK | Package | Examples |
|--------------------|-----------------------------|------------------------------------------|
| .NET Server SDK | `LaunchDarkly.ServerSdk` | [`server-sdk/`](./server-sdk/) |
| .NET Server AI SDK | `LaunchDarkly.ServerSdk.Ai` | [`server-sdk-ai/`](./server-sdk-ai/) |
| SDK | Package | Examples |
|----------------------------------|-------------------------------------------|-----------------------------------------------------------------|
| .NET Server SDK | `LaunchDarkly.ServerSdk` | [`server-sdk/`](./server-sdk/) |
| .NET Server AI SDK | `LaunchDarkly.ServerSdk.Ai` | [`server-sdk-ai/`](./server-sdk-ai/) |
| .NET OpenFeature server provider | `LaunchDarkly.OpenFeature.ServerProvider` | [`openfeature-server-provider/`](./openfeature-server-provider/) |

## Requirements

Expand Down
11 changes: 11 additions & 0 deletions openfeature-server-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# LaunchDarkly .NET OpenFeature server provider examples

Examples for the [`LaunchDarkly.OpenFeature.ServerProvider`](https://www.nuget.org/packages/LaunchDarkly.OpenFeature.ServerProvider) package.

For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [.NET SDK reference guide](https://docs.launchdarkly.com/sdk/server-side/dotnet).

## Getting Started

| Example | Description |
|--------------------------------------|-------------------------------------------------------------|
| [Flag Retrieval](./getting-started/) | Initialize the OpenFeature provider and evaluate a feature flag |
51 changes: 51 additions & 0 deletions openfeature-server-provider/getting-started/Hello.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Threading.Tasks;
using LaunchDarkly.OpenFeature.ServerProvider;
using LaunchDarkly.Sdk.Server;
using OpenFeature.Model;

namespace HelloOpenFeatureDotnetServer
{
internal static class Hello
{
public static async Task Main(string[] args)
{
var sdkKey = Environment.GetEnvironmentVariable("LAUNCHDARKLY_SDK_KEY");
var featureFlagKey = Environment.GetEnvironmentVariable("LAUNCHDARKLY_FLAG_KEY") ?? "sample-feature";

if (string.IsNullOrEmpty(sdkKey))
{
Console.WriteLine("Please set an SDK key using the LAUNCHDARKLY_SDK_KEY environment variable.");
Environment.Exit(1);
}

var config = Configuration.Builder(sdkKey).Build();

var provider = new Provider(config);

await OpenFeature.Api.Instance.SetProviderAsync(provider);

var client = OpenFeature.Api.Instance.GetClient();

// Set up the user properties. This user should appear on your LaunchDarkly users dashboard
// soon after you run the demo.
// Remember when using OpenFeature to use `targetingKey` instead of `key`.
var context = EvaluationContext.Builder()
.Set("kind", "user")
.Set("targetingKey", "example-user-key")
.Set("name", "Sandy")
.Build();

var flagValue = await client.GetBooleanValueAsync(featureFlagKey, false, context);

Console.WriteLine($"The {featureFlagKey} feature flag evaluates to {(flagValue ? "true" : "false")}");

// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
// the user properties and flag usage statistics will not appear on your dashboard. In a
// normal long-running application, the SDK would continue running and events would be
// delivered automatically in the background.
await OpenFeature.Api.Instance.ShutdownAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LaunchDarkly.OpenFeature.ServerProvider" Version="2.*" />
<PackageReference Include="LaunchDarkly.ServerSdk" Version="8.*" />
<PackageReference Include="OpenFeature" Version="2.*" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions openfeature-server-provider/getting-started/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LaunchDarkly .NET OpenFeature server provider getting-started example
Copyright 2022 Catamorphic, Co.

This product includes software developed at LaunchDarkly (https://launchdarkly.com/).
20 changes: 20 additions & 0 deletions openfeature-server-provider/getting-started/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# LaunchDarkly sample OpenFeature .NET server application

We've built a simple console application that demonstrates how LaunchDarkly's OpenFeature server-side provider works.

## Build instructions

1. Set the environment variable `LAUNCHDARKLY_SDK_KEY` to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `LAUNCHDARKLY_FLAG_KEY` to the flag key; otherwise, a boolean flag of `sample-feature` will be assumed.

```bash
export LAUNCHDARKLY_SDK_KEY="1234567890abcdef"
export LAUNCHDARKLY_FLAG_KEY="my-boolean-flag"
```

2. Run the application from the command line:

```bash
dotnet run
```

You should see the message `"The <flagKey> feature flag evaluates to <true/false>"`.
Loading