Skip to content

Cratis Chronicle ASP.NET Core namespace resolution

The Cratis Chronicle ASP.NET Core client selects a namespace from the current request when the scoped event store is first resolved. Use a header, subdomain, or custom resolver to route tenant operations to that namespace. Your application must authorize the caller for the selected tenant; choosing a namespace does not grant access to it.

If you already use Cratis Arc tenancy, use its tenant-to-namespace mapping rather than introducing a second tenant selection rule. See Arc tenancy.

The default resolver reads x-cratis-tenant-id. Configure the header name through ChronicleAspNetCoreOptions:

using Microsoft.AspNetCore.Builder;
public static class NamespacesAspNetCoreHttpHeaderResolver
{
public static void Configure(WebApplicationBuilder builder) =>
builder.AddCratisChronicle(options =>
{
options.EventStore = "my-event-store";
options.WithHttpHeaderNamespaceResolver("x-cratis-tenant-id"); // Default header name
});
}

View C# snippet source on GitHub

When the header is present, its value becomes the namespace. If the header or HTTP context is missing, the resolver returns the default namespace.

The subdomain resolver uses the first host segment when the host has more than two dot-separated segments: tenant1.example.com selects tenant1.

using Microsoft.AspNetCore.Builder;
public static class NamespacesAspNetCoreSubdomainResolver
{
public static void Configure(WebApplicationBuilder builder) =>
builder.AddCratisChronicle(options =>
{
options.EventStore = "my-event-store";
options.WithSubdomainNamespaceResolver();
});
}

View C# snippet source on GitHub

A host with two or fewer segments, or no HTTP context, selects the default namespace. The resolver parses the host; it does not check whether the caller belongs to that tenant. Validate the host and tenant access before using the event store.

Implement IEventStoreNamespaceResolver when your tenant comes from another source. The example reads a configured namespace, so every request uses the same value until that configuration changes:

using Cratis.Chronicle;
using Microsoft.Extensions.Configuration;
public class CustomNamespaceResolver : IEventStoreNamespaceResolver
{
readonly IConfiguration _configuration;
public CustomNamespaceResolver(IConfiguration configuration)
{
_configuration = configuration;
}
public EventStoreNamespaceName Resolve() =>
_configuration["Tenant:Namespace"] ?? EventStoreNamespaceName.Default;
}

View C# snippet source on GitHub

For per-request tenancy, read the current tenant through an accessor instead of storing a tenant id on the resolver. Chronicle registers the resolver as a singleton, so it must be safe to share across concurrent requests. Choose one of the following registration methods.

Pass a resolver instance using the IChronicleBuilder configure callback:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
public static class NamespacesAspNetCoreBuilderCustomResolver
{
public static void Configure(WebApplicationBuilder builder, IConfiguration someConfiguration) =>
builder.AddCratisChronicle(
configureOptions: options => options.EventStore = "my-event-store",
configure: b => b.WithNamespaceResolver(new CustomNamespaceResolver(someConfiguration)));
}

View C# snippet source on GitHub

Configure EventStoreNamespaceResolverType through ChronicleAspNetCoreOptions. Chronicle creates the resolver using dependency injection (DI), which supplies its constructor dependencies:

using Cratis.Chronicle.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
public static class NamespacesAspNetCoreTypeBasedCustomResolver
{
public static void Configure(WebApplicationBuilder builder) =>
builder.Services.Configure<ChronicleAspNetCoreOptions>(options =>
{
options.EventStore = "my-event-store";
options.EventStoreNamespaceResolverType = typeof(CustomNamespaceResolver);
});
}

View C# snippet source on GitHub

  1. Resolver set via IChronicleBuilder.WithNamespaceResolver() or IChronicleBuilder.NamespaceResolver
  2. Type configuration (EventStoreNamespaceResolverType) when provided
  3. Default HTTP header resolver

This endpoint appends an event to the namespace selected by x-tenant-id, not the default header name. Use it behind the trusted tenant-header boundary described above; the endpoint itself does not authorize the tenant.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Microsoft.AspNetCore.Builder;
[EventType]
public record NamespacesAspNetCoreItemAddedToCart(string ProductId, int Quantity);
public static class NamespacesAspNetCoreWebAppExample
{
public static void ConfigureApp(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.AddCratisChronicle(options =>
{
options.EventStore = "production-store";
options.WithHttpHeaderNamespaceResolver("x-tenant-id");
});
var app = builder.Build();
app.MapPost("/api/cart/{cartId}/items", async (string cartId, IEventLog eventLog) =>
{
var itemAdded = new NamespacesAspNetCoreItemAddedToCart(ProductId: "product-123", Quantity: 1);
await eventLog.Append(cartId, itemAdded);
return Microsoft.AspNetCore.Http.Results.Ok();
});
app.Run();
}
}

View C# snippet source on GitHub

Check the routing with two authorized tenant requests and confirm that each event reaches its own namespace. Also check a missing tenant header against your application’s rejection or default-namespace policy.

For worker services and direct client construction, continue with DotNET client usage.