Skip to content

Cratis Chronicle hosting with Microsoft Aspire

Add Cratis Chronicle to your Microsoft Aspire AppHost to run the event store alongside your application and pass its endpoint to your .NET client. The Cratis.Chronicle.Aspire package defines the container resource. Local run mode uses bundled MongoDB; publish mode selects the production image, which needs external storage and configured certificates.

Add the Cratis.Chronicle.Aspire package to your AppHost project:

Terminal window
dotnet add package Cratis.Chronicle.Aspire

When you run an Aspire AppHost, AddCratisChronicle() selects the Chronicle development image (cratis/chronicle:latest-development). It bundles MongoDB and generates its own self-signed TLS certificate at startup — so neither an external database nor a certificate is required. A configure callback can also set storage or certificates without changing that image selection.

using Aspire.Hosting;
public static class HostingAspireDevMode
{
public static void ConfigureAppHost(string[] args)
{
var builder = DistributedApplication.CreateBuilder(args);
var chronicle = builder.AddCratisChronicle();
builder.AddContainer("api", "my-org/my-api")
.WithReference(chronicle);
builder.Build().Run();
}
}

View C# snippet source on GitHub

Use this path for local development or automated checks that need a running Chronicle server. Add storage or certificate settings to the callback only when you need to replace the defaults.

The generated certificate is untrusted and regenerated on every start, with SANs localhost, chronicle, 127.0.0.1, and ::1. The .NET client still skips validation when both settings are omitted in this release, but the next major release will enable it by default. We recommend explicitly setting skipTlsValidation=false after configuring trust or a matching pin. For development only, explicitly opt out with a connection string such as chronicle://localhost:35000?skipTlsValidation=true, using your AppHost’s assigned address. An explicit Tls.SkipCertificateValidation=false still requires validation. See Client TLS configuration.

When you publish an Aspire AppHost, AddCratisChronicle() selects the production image (cratis/chronicle:latest) whether or not you pass a configure callback. It has no embedded MongoDB and does not generate certificates. Configure the following dependencies for the default TLS and internal OAuth setup:

What Method Without it
A database WithMongoDB / WithPostgreSql / WithMsSql / WithSqlite There is no storage to connect to
A TLS certificate WithTlsCertificate The server throws No TLS certificate is configured and exits
An encryption certificate WithEncryptionCertificate The server throws An encryption certificate is required in production and exits — unless you turn the OAuthAuthority feature off or point Authentication:Authority at an external authority, which skips the internal authority that needs it

Each storage example below configures only the database. For the default production setup, also add the two calls in Certificates. Setting ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT to Development does not enable certificate generation: that behavior is selected at compile time.

WithTlsCertificate and WithEncryptionCertificate each take a path to a PKCS#12 (.pfx) file on your machine and the password protecting it:

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireCertificates
{
public static IResourceBuilder<ChronicleResource> ConfigureAppHost(IDistributedApplicationBuilder builder)
{
var mongo = builder.AddConnectionString("chronicle-mongo");
return builder.AddCratisChronicle("chronicle", chronicle => chronicle
.WithMongoDB(mongo)
.WithTlsCertificate("certs/chronicle.pfx", "YourPassword")
.WithEncryptionCertificate("certs/encryption.pfx", "YourPassword"));
}
}

View C# snippet source on GitHub

Each method bind-mounts its file read-only and sets the matching Chronicle configuration path. TLS uses /certs/tls.pfx; encryption uses /certs/encryption.pfx. You do not need a separate WithBindMount call. Relative host paths resolve against the AppHost directory.

A missing host file causes CertificateFileDoesNotExist, which reports the supplied and resolved paths before the container starts. Calling a certificate method again replaces that method’s earlier configuration, so an environment-specific call can override a shared helper.

You can point both methods at the same PFX, but then TLS renewal also changes the encryption certificate. Preserve the old certificate during rotation, or use separate files. The methods map to these settings:

Method Container environment variable Chronicle setting
WithTlsCertificate Cratis__Chronicle__Tls__CertificatePath / Cratis__Chronicle__Tls__CertificatePassword Cratis:Chronicle:Tls:CertificatePath / Cratis:Chronicle:Tls:CertificatePassword
WithEncryptionCertificate Cratis__Chronicle__EncryptionCertificate__CertificatePath / Cratis__Chronicle__EncryptionCertificate__CertificatePassword Cratis:Chronicle:EncryptionCertificate:CertificatePath / Cratis:Chronicle:EncryptionCertificate:CertificatePassword

Both files must include their private keys. The password argument is optional because Chronicle reads PKCS#12 with or without a password; protect deployed files and supply their passwords from your secret store. Use Local Certificate Setup to generate a development PFX, and Production Hosting to prepare production certificates and probes.

WithEncryptionCertificate sets the active certificate for new encryption. Keep previous certificates loaded so stored data remains readable after the change. Rotation requires restarting every node with the same certificate ring, the configured set of active and previous certificates; it does not guarantee a zero-downtime deployment. Follow Rotating the certificate before changing the files.

There is no builder method for the previous list. Mount each previous certificate and configure its container path directly:

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireRotationPreviousCertificate
{
public static IResourceBuilder<ChronicleResource> Configure(
IDistributedApplicationBuilder builder) =>
builder
.AddCratisChronicle(configure: chronicle => chronicle
.WithTlsCertificate("certs/chronicle.pfx", "YourPassword")
.WithEncryptionCertificate(
"certs/encryption-2026.pfx",
"YourNewPassword"))
.WithBindMount(
"certs/encryption-2025.pfx",
"/certs/encryption-previous.pfx",
isReadOnly: true)
.WithEnvironment(
"Cratis__Chronicle__EncryptionCertificate__Previous__0__CertificatePath",
"/certs/encryption-previous.pfx")
.WithEnvironment(
"Cratis__Chronicle__EncryptionCertificate__Previous__0__CertificatePassword",
"YourOldPassword");
}

View C# snippet source on GitHub

AddCratisChronicle returns the resource builder, so chain WithBindMount and WithEnvironment onto it. The environment variable must name the mounted path inside the container, not the host path.

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireMongo
{
public static IResourceBuilder<ChronicleResource> ConfigureAppHost(IDistributedApplicationBuilder builder)
{
var mongo = builder.AddConnectionString("chronicle-mongo");
return builder.AddCratisChronicle("chronicle", c =>
c.WithMongoDB(mongo));
}
}

View C# snippet source on GitHub

The WithMongoDB method sets the Cratis__Chronicle__Storage__Type and Cratis__Chronicle__Storage__ConnectionDetails environment variables on the Chronicle container using the connection string from the provided MongoDB resource.

WithMongoDB changes Chronicle’s connection string, not its image. In run mode, the development image still starts its bundled replica set when the storage type is MongoDB, even though Chronicle connects to your external database. SQL storage types skip that process. To use external MongoDB without starting the bundled database, select latest-development-slim with WithImageTag("latest-development-slim"); the production image also has no embedded MongoDB.

mongo can be any IResourceBuilder<IResourceWithConnectionString> that resolves to a replica set, including:

  • A connection string for a MongoDB Atlas replica set (builder.AddConnectionString("...")).
  • Any self-hosted or cloud MongoDB cluster reached through a connection string.

For local development with an external MongoDB replica set, AddCratisChronicleMongoDB() provisions one for you — a MongoDB container that initializes itself as a single-node replica set, handed back as a connection string carrying ?directConnection=true:

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireMongoReplicaSetHelper
{
public static IResourceBuilder<ChronicleResource> ConfigureAppHost(IDistributedApplicationBuilder builder)
{
var mongo = builder.AddCratisChronicleMongoDB();
return builder.AddCratisChronicle(configure: chronicle => chronicle.WithMongoDB(mongo));
}
}

View C# snippet source on GitHub

The helper configures only storage. If you override the image tag to run the production image locally, also configure the certificates in Certificates. Without an override, Aspire run mode still selects the development image.

If you would rather define the container yourself, the same recipe written out in full is:

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireMongoReplicaSet
{
public static IResourceBuilder<ChronicleResource> ConfigureAppHost(IDistributedApplicationBuilder builder)
{
// Chronicle uses MongoDB transactions and change streams, so the database must run as a
// replica set. This command starts a single-node replica set that initializes itself on
// first run, keeping mongod as PID 1 for correct signal handling and privilege drop.
const string replicaSetCommand =
"( until mongosh --quiet --eval 'db.adminCommand({ ping: 1 })' >/dev/null 2>&1; do sleep 0.3; done; " +
"mongosh --quiet --eval 'try { rs.status() } catch (error) { rs.initiate({ _id: \"rs0\", members: [{ _id: 0, host: \"localhost:27017\" }] }) }' ) & " +
"exec docker-entrypoint.sh mongod --replSet rs0 --bind_ip_all";
var mongo = builder.AddContainer("mongo", "mongo", "8.2")
.WithEndpoint(targetPort: 27017, name: "tcp")
.WithEntrypoint("/bin/sh")
.WithArgs("-c", replicaSetCommand);
var mongoEndpoint = mongo.GetEndpoint("tcp");
// directConnection=true stops the driver from following the advertised replica-set member
// host (localhost:27017, only reachable inside the container) back out and hanging.
var mongoConnection = builder.AddConnectionString(
"chronicle-mongo",
ReferenceExpression.Create(
$"mongodb://{mongoEndpoint.Property(EndpointProperty.Host)}:{mongoEndpoint.Property(EndpointProperty.Port)}/?directConnection=true"));
return builder.AddCratisChronicle("chronicle", c => c.WithMongoDB(mongoConnection));
}
}

View C# snippet source on GitHub

directConnection=true makes the driver use Aspire’s supplied address rather than the member’s advertised localhost:27017, which only works inside the MongoDB container. This example creates one replica-set member. Plan replication and failover separately for a production database; the helper does not create a multi-member deployment.

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspirePostgres
{
public static IResourceBuilder<ChronicleResource> ConfigureAppHost(IDistributedApplicationBuilder builder)
{
var postgres = builder.AddConnectionString("chronicle-postgres");
return builder.AddCratisChronicle("chronicle", c =>
c.WithPostgreSql(postgres));
}
}

View C# snippet source on GitHub

postgres can be any IResourceBuilder<IResourceWithConnectionString>, including:

  • A connection string (builder.AddConnectionString("..."))
  • A PostgreSQL container: builder.AddPostgres("postgres").AddDatabase("chronicle")
using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireSqlServer
{
public static IResourceBuilder<ChronicleResource> ConfigureAppHost(IDistributedApplicationBuilder builder)
{
var sql = builder.AddConnectionString("chronicle-sql");
return builder.AddCratisChronicle("chronicle", c =>
c.WithMsSql(sql));
}
}

View C# snippet source on GitHub

sql can be any IResourceBuilder<IResourceWithConnectionString>, including:

  • A connection string (builder.AddConnectionString("..."))
  • A SQL Server container: builder.AddSqlServer("sql").AddDatabase("chronicle")

For SQLite, provide the connection string directly (SQLite is file-based and does not require a network resource):

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireSqlite
{
public static IResourceBuilder<ChronicleResource> ConfigureAppHost(IDistributedApplicationBuilder builder) =>
builder.AddCratisChronicle("chronicle", c =>
c.WithSqlite("Data Source=/data/chronicle.db"));
}

View C# snippet source on GitHub

The database file lives at the path you configure inside the Chronicle container. Mount a writable volume at its parent directory with Aspire’s standard WithVolume method to keep it when the container is replaced. Without a volume, the directory must already exist in the image and the data is lost with the container.

Pass the Chronicle resource as a connection string reference to your application projects so they receive the correct endpoint at runtime:

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspireConnectingClient
{
public static void ConfigureAppHost(IDistributedApplicationBuilder builder, IResourceBuilder<ChronicleResource> chronicle)
{
// Reference any of your application's projects the same way -
// builder.AddProject<Projects.MyApi>("api") when Projects.MyApi is generated from your solution
builder.AddContainer("api", "my-org/my-api")
.WithReference(chronicle);
}
}

View C# snippet source on GitHub

WithReference(chronicle) supplies ConnectionStrings__<resource name> to the application, exposed by .NET configuration as ConnectionStrings:<resource name>. In the application, call builder.AddCratisChronicle() and configure your event store as usual. Both the generic-host and ASP.NET Core entry points read ConnectionStrings:chronicle by default, so Aspire’s assigned host and port replace the localhost endpoint.

If you give the Chronicle resource another name, pass that same name in the application’s call: builder.AddCratisChronicle(configureOptions: null, configSection: null, configure: null, connectionStringName: "events") reads ConnectionStrings:events. Supply the preceding arguments explicitly in this overload; use null for defaults. This name selects the connection string, not the event store.

Connection strings take precedence in this order, highest first:

  1. ConnectionString set by the configureOptions callback to AddCratisChronicle.
  2. Cratis:Chronicle:ConnectionString, or ConnectionString under your custom configSection.
  3. ConnectionStrings:<connectionStringName> (chronicle by default).
  4. The existing development default: localhost on port 35000 with development client credentials.

Other Chronicle settings continue to bind from Cratis:Chronicle (or your custom section). The named fallback is parsed only after the section and callback have configured the options, and only if neither supplied a connection string. An explicit choice of the development connection string also takes precedence. A missing or blank named connection string leaves the development default unchanged.

The connection string exposed by ChronicleResource uses the chronicle://host:port format and points to the gRPC endpoint (container port 35000, with the host port assigned by Aspire). A URL without credentials still uses Chronicle’s existing client-credentials flow with the development client credentials; it does not disable authentication. For a production kernel, configure the required credentials explicitly rather than relying on the development defaults.

Chronicle exposes the following ports:

Port Endpoint Name Description
35000 grpc Primary Chronicle service — gRPC (HTTP/2) and Workbench/API/OAuth/health (HTTP/1.1)

AddCratisChronicle() registers the grpc endpoint. There is no separate management endpoint: use chronicle.Resource.GrpcEndpoint or the resource’s connection string for the shared service port. Remove any management endpoint reference copied from an older sample.

This AppHost Program.cs configures MongoDB and both certificates for the production image’s default TLS and internal OAuth setup:

using Aspire.Hosting;
public static class HostingAspireCompleteExample
{
public static void ConfigureAppHost(string[] args)
{
var builder = DistributedApplication.CreateBuilder(args);
var mongo = builder.AddConnectionString("chronicle-mongo");
var chronicle = builder.AddCratisChronicle("chronicle", c => c
.WithMongoDB(mongo)
.WithTlsCertificate("certs/chronicle.pfx", "YourPassword")
.WithEncryptionCertificate("certs/encryption.pfx", "YourPassword"));
builder.AddContainer("api", "my-org/my-api")
.WithReference(chronicle);
builder.Build().Run();
}
}

View C# snippet source on GitHub

For development with bundled storage, omit the callback. Run mode selects the development image with a MongoDB replica set, generated TLS certificate, and ephemeral OAuth keys:

using Aspire.Hosting;
public static class HostingAspireCompleteExampleDev
{
public static void ConfigureAppHost(string[] args)
{
var builder = DistributedApplication.CreateBuilder(args);
var chronicle = builder.AddCratisChronicle();
builder.AddContainer("api", "my-org/my-api")
.WithReference(chronicle);
builder.Build().Run();
}
}

View C# snippet source on GitHub

Image tag Description Selected by
cratis/chronicle:latest Production image — no embedded MongoDB, no self-generated certificates Aspire publish mode
cratis/chronicle:latest-development Development image — includes embedded MongoDB, generates a TLS certificate and ephemeral OAuth keys Aspire run mode
cratis/chronicle:latest-development-slim Development slim image — no embedded MongoDB Explicit WithImageTag("latest-development-slim") or run it directly

All three tags above float. Two deployments of the same AppHost can therefore run different Chronicle versions. Chain WithImageTag after AddCratisChronicle to pin the version you tested:

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Cratis.Chronicle.Aspire;
public static class HostingAspirePinImage
{
public static IResourceBuilder<ChronicleResource> Configure(
IDistributedApplicationBuilder builder) =>
builder
.AddCratisChronicle(configure: chronicle => chronicle
.WithTlsCertificate("certs/chronicle.pfx", "YourPassword")
.WithEncryptionCertificate(
"certs/encryption.pfx",
"YourPassword"))
.WithImageTag("16.26.0");
}

View C# snippet source on GitHub

WithImageTag overrides the tag selected for the Aspire execution mode while leaving the image and registry alone. You can use it to run the production image locally or pin a released Chronicle version. Choose a tag for the image class you intend to run: a production tag needs external storage and certificates even in Aspire run mode. Treat the upgrade as a deliberate change.

See Production Hosting for guidance on running Chronicle in production environments.