Pular para o conteúdo

KurrentDB integration

Este conteúdo não está disponível em sua língua ainda.

⭐ Community Toolkit KurrentDB logo

KurrentDB is an event store database designed for Event Sourcing, offering high availability and reliability for storing events. The Aspire KurrentDB integration enables you to connect to existing KurrentDB instances or create new instances from Aspire using the KurrentDB container image.

To run the KurrentDB container, install the 📦 CommunityToolkit.Aspire.Hosting.KurrentDB NuGet package in the AppHost project.

Aspire CLI — Adicionar pacote CommunityToolkit.Aspire.Hosting.KurrentDB
aspire add communitytoolkit-kurrentdb

A Aspire CLI é interativa; escolha o resultado adequado quando solicitado:

Aspire CLI — Exemplo de saída
Select an integration to add:
> communitytoolkit-kurrentdb (CommunityToolkit.Aspire.Hosting.KurrentDB)
> Other results listed as selectable options...

In the AppHost project, register and consume the KurrentDB integration using the AddKurrentDB extension method to add the KurrentDB container to the application builder.

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kurrentdb = builder.AddKurrentDB("kurrentdb");
builder.AddProject<Projects.ExampleProject>()
.WithReference(kurrentdb);
// After adding all resources, run the app...

To add a data volume to the KurrentDB resource, call the WithDataVolume method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kurrentdb = builder.AddKurrentDB("kurrentdb")
.WithDataVolume();
builder.AddProject<Projects.ExampleProject>()
.WithReference(kurrentdb);
// After adding all resources, run the app...

The data volume is used to persist the KurrentDB data outside the lifecycle of its container. The data volume is mounted at the /var/lib/kurrentdb path in the KurrentDB container.

Add KurrentDB resource with data bind mount

Section titled “Add KurrentDB resource with data bind mount”

To add a data bind mount to the KurrentDB resource, call the WithDataBindMount method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kurrentdb = builder.AddKurrentDB("kurrentdb")
.WithDataBindMount(source: @"C:\KurrentDB\Data");
builder.AddProject<Projects.ExampleProject>()
.WithReference(kurrentdb);
// After adding all resources, run the app...

The KurrentDB hosting integration automatically adds a health check for the KurrentDB resource. The health check verifies that the KurrentDB instance is running and that a connection can be established to it.

To get started with the Aspire KurrentDB client integration, 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 the Program.cs file of your client-consuming project, call the AddKurrentDBClient extension to register an KurrentDBClient for use via the dependency injection container.

builder.AddKurrentDBClient(connectionName: "kurrentdb");

You can then retrieve the KurrentDBClient instance using dependency injection. For example, to retrieve the client from a service:

public class ExampleService(KurrentDBClient client)
{
// Use client...
}

There might be situations where you want to register multiple KurrentDBClient instances with different connection names. To register keyed KurrentDB clients, call the AddKeyedKurrentDBClient method:

builder.AddKeyedKurrentDBClient(name: "events");
builder.AddKeyedKurrentDBClient(name: "audit");

Then retrieve the instances using dependency injection:

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 KurrentDB client integration provides multiple configuration approaches and options to meet the requirements and conventions of your project.

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling AddKurrentDBClient:

builder.AddKurrentDBClient("kurrentdb");

Then the connection string will be retrieved from the ConnectionStrings configuration section:

{
"ConnectionStrings": {
"kurrentdb": "kurrentdb://localhost:22113?tls=false"
}
}

The KurrentDB client integration supports Microsoft.Extensions.Configuration. It loads the KurrentDBSettings from configuration using the Aspire:KurrentDB:Client key. Example appsettings.json:

{
"Aspire": {
"KurrentDB": {
"Client": {
"ConnectionString": "kurrentdb://localhost:22113?tls=false",
"DisableHealthChecks": false,
"DisableTracing": false
}
}
}
}

You can pass the delegate to set up options inline:

builder.AddKurrentDBClient(
"kurrentdb",
static settings => settings.DisableHealthChecks = true);

By default, Aspire integrations enable health checks for all services. The KurrentDB integration adds a health check that verifies the KurrentDB instance is reachable and can execute commands.

The KurrentDB integration uses the following log categories:

  • Aspire.KurrentDB

The KurrentDB integration emits tracing activities using OpenTelemetry when tracing is not disabled.

Pergunte & RespondaColaboreComunidadeDiscutirAssistir