Skip to Content

C# / .NET SDK

GoGreen feature flag and experimentation SDK for .NET (C#). Evaluate flags, stream real-time updates, and track events. Supports .NET 8.0 and .NET Standard 2.0. Includes an OpenFeature  provider.

Installation

From a project directory:

dotnet add package GoGreen.Sdk

Or add to your .csproj:

<PackageReference Include="GoGreen.Sdk" Version="1.0.0" />

If the package is not yet published to NuGet, build from source:

cd sdk/dotnet/GoGreen.Sdk dotnet pack -c Release # Consume the .nupkg from bin/Release/

Quick start

using GoGreen.Sdk; // Build config with your SDK key and environment var config = GoGreenConfig.CreateBuilder() .WithSdkKey("your-sdk-key") .WithEnvId("your-env-id") .WithApiBaseUrl("https://app.gogreenflags.com") .WithDefaultContext(EvaluationContext.CreateBuilder("user-123").Build()) .Build(); // Create client (async) var client = await GoGreenClient.CreateAsync(config); // Evaluate flags bool enabled = await client.BoolVariationAsync("my-feature", false); string plan = await client.StringVariationAsync("plan", "free"); double discount = await client.NumberVariationAsync("discount", 0.0); // With detail (variation id, reason) var detail = await client.BoolVariationDetailAsync("my-feature", false); Console.WriteLine($"Value: {detail.Value}, Reason: {detail.Reason}"); // Change context client.SetContext(EvaluationContext.CreateBuilder("other-user").SetCustom("country", "US").Build()); // Track custom events for experimentation client.Track("purchase", new Dictionary<string, object?> { ["amount"] = 99.99 }); // Release resources client.Close();

Configuration

OptionDescriptionDefault
SdkKeySDK key for authenticationrequired
EnvIdEnvironment identifierrequired
ApiBaseUrlEvaluation API base URLhttp://localhost:8092
StreamUrlSSE stream URLsame as ApiBaseUrl
EventsUrlEvents ingestion URLsame as ApiBaseUrl
EnableStreamingReal-time SSE updatestrue
OfflineOffline mode (cache/bootstrap only)false
RequestTimeoutMsHTTP timeout (ms)10000
FlushIntervalMsEvent flush interval (ms)30000
DefaultContextDefault evaluation contextrequired
BootstrapPre-populated flags (e.g. for testing)optional

OpenFeature provider

using GoGreen.Sdk; using GoGreen.Sdk.Provider; using OpenFeature; var client = await GoGreenClient.CreateAsync(config); var provider = new GoGreenProvider(client); await Api.Instance.SetProviderAsync(provider); var featureClient = Api.Instance.GetClient(); bool value = await featureClient.GetBooleanValueAsync("my-flag", false);

Testing

Use the test client for unit tests (no network, predefined flag values):

var flags = new Dictionary<string, object?> { ["feature-on"] = true, ["plan"] = "pro" }; var client = GoGreenClient.CreateTestClient(flags); Assert.True(await client.BoolVariationAsync("feature-on", false)); Assert.Equal("pro", await client.StringVariationAsync("plan", "free")); client.Close();

API reference

  • GoGreenClient: CreateAsync, CreateTestClient, BoolVariationAsync, StringVariationAsync, NumberVariationAsync, JsonVariationAsync, *DetailAsync variants, SetContext, IdentifyAsync, Track, EnterOfflineMode, ExitOfflineModeAsync, Close, CloseAsync, WaitForInitialization
  • GoGreenConfig.Builder: WithSdkKey, WithEnvId, WithApiBaseUrl, WithStreamUrl, WithEventsUrl, WithEnableStreaming, WithOffline, WithDefaultContext, WithBootstrap, Build
  • EvaluationContext: CreateBuilder(key), Key, Custom
  • EvaluationDetail<T>: Value, VariationId, VariationIndex, Reason, RuleIndex, PrerequisiteKey