Skip to content

C# Dependency Injection

The C# SDK can be integrated with .NET’s built-in dependency injection container for clean service lifetime management.

Register the client as a singleton in your DI container:

Program.cs
using Flags.GG.Client;
using Flags.GG.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(_ => FlagsClient.Create(options =>
{
options.Auth = new Auth
{
ProjectId = builder.Configuration["Flags:ProjectId"]!,
AgentId = builder.Configuration["Flags:AgentId"]!,
EnvironmentId = builder.Configuration["Flags:EnvironmentId"]!
};
}));
var app = builder.Build();
public class HomeController : Controller
{
private readonly FlagsClient _flags;
public HomeController(FlagsClient flags)
{
_flags = flags;
}
public async Task<IActionResult> Index()
{
if (await _flags.Is("new-homepage").EnabledAsync())
{
return View("NewHome");
}
return View();
}
}
app.MapGet("/", async (FlagsClient flags) =>
{
var enabled = await flags.Is("welcome-banner").EnabledAsync();
return Results.Ok(new { banner = enabled });
});
{
"Flags": {
"ProjectId": "your-project-id",
"AgentId": "your-agent-id",
"EnvironmentId": "your-environment-id"
}
}

Register as a singleton — the client handles its own caching and thread safety internally. Creating multiple instances would duplicate API calls and cache storage.