इसे छोड़कर कंटेंट पर जाएं
DocsTry Aspire
DocsTry

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:

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:

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:

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.

As an alternative to the native provider, you can connect to flagd using the OFREP (OpenFeature Remote Evaluation Protocol). OFREP is a standardized HTTP/REST-based protocol for remote feature flag evaluation, making it language-agnostic and well-suited for polyglot environments.

Install the 📦 OpenFeature.Providers.Ofrep NuGet package in the client-consuming project:

Terminal window
dotnet add package OpenFeature.Providers.Ofrep

In your AppHost, retrieve the OFREP endpoint from the flagd resource and pass it to your consuming project as an environment variable:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var flagd = builder.AddFlagd("flagd")
.WithBindFileSync("./flags/");
var ofrepEndpoint = flagd.GetEndpoint("ofrep");
builder.AddProject<Projects.ExampleProject>()
.WaitFor(flagd)
.WithEnvironment("OFREP_ENDPOINT", ofrepEndpoint);
// After adding all resources, run the app...

In the Program.cs file of your client-consuming project, register the OpenFeature SDK with the OFREP provider using dependency injection:

using OpenFeature.Providers.Ofrep;
using OpenFeature.Providers.Ofrep.DependencyInjection;
builder.Services.AddOpenFeature(featureBuilder =>
{
featureBuilder.AddOfrepProvider();
});

The OFREP provider automatically reads the OFREP_ENDPOINT environment variable to discover the flagd OFREP endpoint. You can then evaluate feature flags by injecting IFeatureClient:

using OpenFeature;
app.MapGet("/", async (IFeatureClient flagClient) =>
{
var welcomeBanner = await flagClient.GetBooleanValueAsync(
"welcome-banner",
defaultValue: false);
var backgroundColor = await flagClient.GetStringValueAsync(
"background-color",
defaultValue: "#000000");
return Results.Ok(new { welcomeBanner, backgroundColor });
});

For more information on the OFREP specification, see the OFREP specification.