Перейти к содержимому
Docs Try Aspire
Docs Try

Connect to flagd

Это содержимое пока не доступно на вашем языке.

⭐ Community Toolkit flagd logo

This page describes how consuming apps connect to a flagd resource that’s already modeled in your AppHost. For the AppHost API surface — adding a flagd resource, file sync, port configuration, log levels, and health checks — see flagd Hosting integration.

When you reference a flagd resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can then configure the appropriate OpenFeature flagd provider using that connection information — the pattern works the same from C#, Go, Python, or TypeScript.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called flagd becomes FLAGD_URI.

The flagd resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the flagd server
PortThe port number of the flagd HTTP/gRPC endpoint
UriThe endpoint URI, with the format http://{Host}:{Port}

Example connection string:

Uri: http://localhost:8013

Pick the language your consuming app is written in. Each example assumes your AppHost adds a flagd resource named flagd and references it from the consuming app.

For C# apps, use the OpenFeature SDK with the flagd provider. Install the 📦 OpenFeature.Contrib.Providers.Flagd NuGet package in the client-consuming project:

.NET CLI — Add OpenFeature.Contrib.Providers.Flagd package
dotnet add package OpenFeature.Contrib.Providers.Flagd

In Program.cs, read the Aspire-injected connection string and register the flagd provider with the OpenFeature SDK:

C# — Program.cs
using OpenFeature;
using OpenFeature.Contrib.Providers.Flagd;
var connectionString = builder.Configuration.GetConnectionString("flagd");
await OpenFeature.Api.Instance.SetProviderAsync(
new FlagdProvider(new Uri(connectionString!)));

Obtain a client and evaluate feature flags:

C# — ExampleService.cs
var flagClient = OpenFeature.Api.Instance.GetClient();
var welcomeBanner = await flagClient.GetBooleanValueAsync(
"welcome-banner",
defaultValue: false);
var backgroundColor = await flagClient.GetStringValueAsync(
"background-color",
defaultValue: "#000000");

If you prefer to read the connection URI from environment variables directly rather than through IConfiguration:

C# — Program.cs
using OpenFeature;
using OpenFeature.Contrib.Providers.Flagd;
var uri = Environment.GetEnvironmentVariable("FLAGD_URI");
await OpenFeature.Api.Instance.SetProviderAsync(
new FlagdProvider(new Uri(uri!)));

For more information, see the OpenFeature .NET SDK documentation and the flagd .NET provider.