Skip to content

Cratis Chronicle .NET namespace resolution

The Cratis Chronicle .NET client uses IEventStoreNamespaceResolver to select a namespace when you call GetEventStore without an explicit namespace. The returned event store stays bound to that namespace. Configure a resolver when a worker service or console application needs to select tenant data from its current execution context.

using Cratis.Chronicle;
public class NamespacesDotNetClientSampleResolver : IEventStoreNamespaceResolver
{
public EventStoreNamespaceName Resolve() => EventStoreNamespaceName.Default;
}

View C# snippet source on GitHub

Resolve() returns an EventStoreNamespaceName for the current context. Passing a namespace explicitly to GetEventStore bypasses the resolver for that call.

DefaultEventStoreNamespaceResolver always returns EventStoreNamespaceName.Default. Direct clients and non-web hosts use it when no resolver is supplied. Choose it when the application uses a single namespace.

ClaimsBasedNamespaceResolver reads a tenant claim from an authenticated principal. By default, it reads ClaimsPrincipal.Current and looks for tenant_id; the example shows how to choose a different claim type:

using Cratis.Chronicle;
public static class NamespacesDotNetClientClaimsBased
{
public static ChronicleClient CreateWithDefaultClaimType(ChronicleOptions options) =>
// Uses the default claim type "tenant_id"
new(options, namespaceResolver: new ClaimsBasedNamespaceResolver("tenant_id"));
public static ChronicleClient CreateWithCustomClaimType(ChronicleOptions options) =>
// Uses a custom claim type
new(options, namespaceResolver: new ClaimsBasedNamespaceResolver("custom_tenant_claim"));
}

View C# snippet source on GitHub

The resolver also accepts a principal-accessor callback when the principal comes from another context. If the principal is absent or unauthenticated, or the claim is missing or empty, it returns the default namespace. If your application requires a named tenant, reject that missing context before accessing the event store rather than treating the fallback as authorization.

For a console application or another directly constructed client, pass the resolver through the namespaceResolver constructor parameter. This example explicitly selects the same default resolver used when you omit the parameter:

using Cratis.Chronicle;
public static class NamespacesDotNetClientDefaultResolver
{
public static ChronicleClient Create(ChronicleOptions options)
{
var resolver = new DefaultEventStoreNamespaceResolver();
return new ChronicleClient(options, namespaceResolver: resolver);
}
}

View C# snippet source on GitHub

For a worker service using IHostApplicationBuilder, either set ChronicleClientOptions.EventStoreNamespaceResolverType or supply an instance through IChronicleBuilder. The instance takes priority over the configured type:

using Microsoft.Extensions.Hosting;
public static class NamespacesDotNetClientHostedAppConfiguration
{
public static void ConfigureViaOptions(IHostApplicationBuilder builder) =>
// Via options (type is resolved from DI)
builder.AddCratisChronicle(options =>
{
options.EventStore = "my-store";
options.EventStoreNamespaceResolverType = typeof(TenantNamespaceResolver);
});
public static void ConfigureViaBuilder(IHostApplicationBuilder builder, ITenantContext tenantContext) =>
// Via builder (instance is used directly)
builder.AddCratisChronicle(
configureOptions: options => options.EventStore = "my-store",
configure: b => b.WithNamespaceResolver(new TenantNamespaceResolver(tenantContext)));
}

View C# snippet source on GitHub

Chronicle creates type-based resolvers with dependency injection (DI) and keeps the resolver as a singleton. It resolves the namespace when the scoped IEventStore is first requested. Set the tenant context before resolving that service, and use a new scope for another tenant.

If your application uses Cratis Arc tenancy, use its existing tenant-to-namespace mapping. See Arc tenancy.

For another tenant context, implement a resolver. This example returns ITenantContext.CurrentTenantId directly, making that value the Chronicle namespace:

using Cratis.Chronicle;
public interface ITenantContext
{
EventStoreNamespaceName CurrentTenantId { get; }
}
public class TenantNamespaceResolver : IEventStoreNamespaceResolver
{
readonly ITenantContext _tenantContext;
public TenantNamespaceResolver(ITenantContext tenantContext)
{
_tenantContext = tenantContext;
}
public EventStoreNamespaceName Resolve() =>
_tenantContext.CurrentTenantId;
}

View C# snippet source on GitHub

Pass the resolver to a directly constructed client. Its tenant context must expose the current execution’s tenant without sharing a mutable tenant id across concurrent operations:

using Cratis.Chronicle;
public static class NamespacesDotNetClientTenantResolverUsage
{
public static ChronicleClient Create(ChronicleOptions options, ITenantContext tenantContext) =>
new(options, namespaceResolver: new TenantNamespaceResolver(tenantContext));
}

View C# snippet source on GitHub

Keep tenant selection stable for the lifetime of an event store instance. Changing the resolver’s context does not retarget an event store you already obtained; call GetEventStore again for the next tenant, or create a new DI scope in a hosted application.

Keep resolution quick and avoid blocking I/O in Resolve(). Decide explicitly whether a missing tenant may use the default namespace. Before relying on the setup, check that two tenant contexts select different namespaces and that missing context follows your chosen policy.

For request-based tenant selection and its authorization boundary, continue with ASP.NET Core namespace resolution.