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

Connect to KurrentDB

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

⭐ Community Toolkit KurrentDB logo

This page describes how consuming apps connect to a KurrentDB resource that’s already modeled in your AppHost. For the AppHost API surface — adding a KurrentDB resource, data volumes, data bind mounts, and more — see KurrentDB Hosting integration.

When you reference a KurrentDB resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire KurrentDB client integration for automatic dependency injection, health checks, and telemetry.

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

The KurrentDB resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the KurrentDB server
PortThe gRPC port the KurrentDB server is listening on (default: 2113)
UriThe connection URI, with the format kurrentdb://{Host}:{Port}

Example connection string:

Uri: kurrentdb://localhost:2113?tls=false

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

For C# apps, the recommended approach is the Aspire KurrentDB client integration. It registers a KurrentDBClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 CommunityToolkit.Aspire.KurrentDB NuGet package in the client-consuming project:

.NET CLI — Add CommunityToolkit.Aspire.KurrentDB package
dotnet add package CommunityToolkit.Aspire.KurrentDB

In Program.cs, call AddKurrentDBClient on your IHostApplicationBuilder to register a KurrentDBClient:

C# — Program.cs
builder.AddKurrentDBClient(connectionName: "kurrentdb");

Resolve the client through dependency injection:

C# — ExampleService.cs
public class ExampleService(KurrentDBClient client)
{
// Use client...
}

To register multiple KurrentDBClient instances with different connection names, use AddKeyedKurrentDBClient:

C# — Program.cs
builder.AddKeyedKurrentDBClient(name: "events");
builder.AddKeyedKurrentDBClient(name: "audit");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("events")] KurrentDBClient eventsClient,
[FromKeyedServices("audit")] KurrentDBClient auditClient)
{
// Use clients...
}

For more information on keyed services, see .NET dependency injection: Keyed services.

The Aspire KurrentDB client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddKurrentDBClient:

C# — Program.cs
builder.AddKurrentDBClient("kurrentdb");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"kurrentdb": "kurrentdb://localhost:2113?tls=false"
}
}

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads KurrentDBSettings from appsettings.json (or any other configuration source) by using the Aspire:KurrentDB:Client key:

JSON — appsettings.json
{
"Aspire": {
"KurrentDB": {
"Client": {
"ConnectionString": "kurrentdb://localhost:2113?tls=false",
"DisableHealthChecks": false,
"DisableTracing": false
}
}
}
}

Inline delegates. Pass an Action<KurrentDBSettings> to configure settings inline, for example to disable health checks:

C# — Program.cs
builder.AddKurrentDBClient(
"kurrentdb",
static settings => settings.DisableHealthChecks = true);

Aspire client integrations enable health checks by default. The KurrentDB client integration adds a health check that verifies the KurrentDB instance is reachable and can execute commands. The health check is wired into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire KurrentDB client integration automatically configures logging and tracing through OpenTelemetry.

Logging categories:

  • Aspire.KurrentDB

Tracing activities:

  • Aspire.KurrentDB — emitted when tracing is not disabled via configuration.

Any of these telemetry features can be disabled through the configuration options above.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection URI from the environment and pass it to the 📦 KurrentDB.Client NuGet package directly:

C# — Program.cs
using KurrentDB.Client;
var connectionString = Environment.GetEnvironmentVariable("KURRENTDB_URI");
var settings = KurrentDBClientSettings.Create(connectionString!);
var client = new KurrentDBClient(settings);
// Use client to read and write events...