Chronicle .NET client structural dependencies
Supply structural dependencies when the Chronicle .NET client needs your application’s identity provider, namespace resolver, or artifact discovery. These dependencies are objects that define client behavior, so you pass them to ChronicleClient or set them during host registration rather than bind them from appsettings.json.
Keep connection strings, timeouts, and TLS settings in ChronicleOptions. For a hosted application, the separate configure callback receives an IChronicleBuilder before the dependency injection (DI) container is built. Use it to select the providers the client will use.
Structural dependencies
Section titled “Structural dependencies”| Dependency | Purpose | Default |
|---|---|---|
IClientArtifactsProvider |
Supplies event types, projections, reactors, and other client artifacts | DefaultClientArtifactsProvider.Default, using project- and package-referenced assemblies |
IServiceProvider |
Activates client artifacts and their services | DefaultServiceProvider for direct construction, using default-constructor activation; pass serviceProvider when artifacts need registered dependencies. Hosted clients use the host’s provider |
IIdentityProvider |
Supplies identity for event metadata | BaseIdentityProvider, returning the current identity or Identity.System when none is set |
ICorrelationIdAccessor |
Provides the current correlation ID for event metadata | CorrelationIdAccessor, returning the current async-local value or Guid.Empty |
IEventStoreNamespaceResolver |
Selects the event store namespace | DefaultEventStoreNamespaceResolver, returning the default namespace |
INamingPolicy |
Names serialized properties, projection mappings, and read model containers | DefaultNamingPolicy, preserving property names and pluralizing container names |
ILoggerFactory |
Creates loggers for the Chronicle client internals | A LoggerFactory without logging providers for direct construction; the host’s factory for hosted registration |
Passing to ChronicleClient directly
Section titled “Passing to ChronicleClient directly”For console applications or other non-DI scenarios, pass structural dependencies as named constructor parameters:
using Microsoft.Extensions.Logging;
public static class StructuralDependenciesDirectClient{ public static ChronicleClient Create(ChronicleOptions options, IClientArtifactsProvider myProvider) => new( options, artifactsProvider: myProvider, loggerFactory: LoggerFactory.Create(b => b.AddConsole()));}import io.cratis.chronicle.ChronicleClientimport io.cratis.chronicle.ChronicleOptionsimport io.cratis.chronicle.artifacts.IArtifactActivatorimport io.cratis.chronicle.artifacts.IClientArtifactsimport io.cratis.chronicle.connection.ChronicleConnectionString
/** * Unlike the .NET client, structural dependencies are not separate constructor parameters on * [ChronicleClient] — they are carried on [ChronicleOptions] itself, which is where every named * dependency below actually lives. */fun createClient(artifacts: IClientArtifacts, artifactActivator: IArtifactActivator): ChronicleClient { val options = ChronicleOptions( connectionString = ChronicleConnectionString.DEVELOPMENT, artifacts = artifacts, artifactActivator = artifactActivator ) return ChronicleClient(options)}import io.cratis.chronicle.ChronicleOptions;import io.cratis.chronicle.artifacts.IArtifactActivator;import io.cratis.chronicle.artifacts.IClientArtifacts;import io.cratis.chronicle.connection.ChronicleConnectionString;import io.cratis.chronicle.java.BlockingChronicleClient;
// Unlike the .NET client, structural dependencies are not separate constructor parameters on the// client — they are carried on ChronicleOptions itself, which is where every named dependency// below actually lives.class StructuralDependenciesDirectClient { BlockingChronicleClient create(IClientArtifacts artifacts, IArtifactActivator artifactActivator) { ChronicleOptions options = new ChronicleOptions( ChronicleConnectionString.Companion.getDEVELOPMENT(), "Unknown", io.cratis.chronicle.sinks.WellKnownSinkTypes.MONGODB, true, artifacts, artifactActivator); return BlockingChronicleClient.connect(options); }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.The myProvider argument is an IClientArtifactsProvider supplied by your application; artifactsProvider: myProvider makes the client use it for artifact discovery. LoggerFactory.Create(b => b.AddConsole()) sends the client’s logs to the console. Omitted dependencies use the direct-construction defaults above.
Configuring with IChronicleBuilder
Section titled “Configuring with IChronicleBuilder”For ASP.NET Core or a worker host, pass provider instances through the configure callback on AddCratisChronicle. It runs during registration, so it is not a place to resolve services from the finished DI container.
using Cratis.Chronicle.Identities;using Microsoft.Extensions.Hosting;
public static class StructuralDependenciesChronicleBuilderRegistration{ public static void Configure(string[] args, IClientArtifactsProvider myCustomProvider, IIdentityProvider myIdentityProvider) { var builder = Host.CreateApplicationBuilder(args);
builder.AddCratisChronicle( configureOptions: options => // runtime config — bindable from appsettings.json { options.EventStore = "my-store"; options.ConnectionString = "chronicle://server:35000"; }, configure: b => b // structural dependencies .WithArtifactsProvider(myCustomProvider) .WithIdentityProvider(myIdentityProvider)); }}Kotlin does not support this workflow yet.Java does not support this workflow yet.Elixir does not support this workflow yet.TypeScript does not support this workflow yet.The same pattern works with WebApplicationBuilder in ASP.NET Core:
using Cratis.Chronicle.Identities;using Microsoft.AspNetCore.Builder;
public static class StructuralDependenciesAspNetCoreBuilderRegistration{ public static void Configure(string[] args, IIdentityProvider myIdentityProvider) { // ASP.NET Core — WebApplicationBuilder var builder = WebApplication.CreateBuilder(args); builder.AddCratisChronicle( configureOptions: options => options.EventStore = "my-store", configure: b => b .WithIdentityProvider(myIdentityProvider)); }}Kotlin does not support this workflow yet.Java does not support this workflow yet.Elixir does not support this workflow yet.TypeScript does not support this workflow yet.Use configureOptions for bound settings: it runs after configuration binding and can override those values. Use configure for provider instances: configuration values cannot replace those objects.
When no custom provider is set on the builder, hosted registration can use a provider already registered in DI. For ASP.NET Core’s defaults, see Client Configuration and the host setup it links to.
IChronicleBuilder fluent methods
Section titled “IChronicleBuilder fluent methods”| Method | Sets |
|---|---|
WithArtifactsProvider(IClientArtifactsProvider) |
Custom artifact discovery |
WithIdentityProvider(IIdentityProvider) |
Custom identity resolution |
WithCorrelationIdAccessor(ICorrelationIdAccessor) |
Custom correlation ID accessor |
WithNamespaceResolver(IEventStoreNamespaceResolver) |
Custom namespace resolution |
WithCamelCaseNamingPolicy() |
Camel case property and container naming |
For another naming policy, assign IChronicleBuilder.NamingPolicy directly. Hosted clients use the host’s ILoggerFactory; the builder has no logging-factory setter.
Custom artifact discovery
Section titled “Custom artifact discovery”Implement IClientArtifactsProvider when you need to supply the type lists Chronicle uses, such as when a modular application controls which modules participate. The example shows explicit lists for the provider’s artifact categories:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record StructuralDepsBookBorrowed(string BookId);
[EventType]public record StructuralDepsBookReturned(string BookId);
public record StructuralDepsBorrowedBook(string BookId);
public class StructuralDepsBorrowedBooksProjection : IProjectionFor<StructuralDepsBorrowedBook>{ public void Define(IProjectionBuilderFor<StructuralDepsBorrowedBook> builder) => builder .From<StructuralDepsBookBorrowed>();}
public class StructuralDepsMyArtifactsProvider : IClientArtifactsProvider{ public IEnumerable<Type> EventTypes => [typeof(StructuralDepsBookBorrowed), typeof(StructuralDepsBookReturned)]; public IEnumerable<Type> Projections => [typeof(StructuralDepsBorrowedBooksProjection)]; public IEnumerable<Type> ModelBoundProjections => []; public IEnumerable<Type> Reactors => []; public IEnumerable<Type> ReadModelReactors => []; public IEnumerable<Type> Reducers => []; public IEnumerable<Type> ReactorMiddlewares => []; public IEnumerable<Type> ComplianceForTypesProviders => []; public IEnumerable<Type> ComplianceForPropertiesProviders => []; public IEnumerable<Type> AdditionalEventInformationProviders => []; public IEnumerable<Type> ConstraintTypes => []; public IEnumerable<Type> UniqueConstraints => []; public IEnumerable<Type> UniqueEventTypeConstraints => []; public IEnumerable<Type> RemoveConstraintEventTypes => []; public IEnumerable<Type> EventTypeMigrators => []; public IEnumerable<Type> EventSeeders => [];}import io.cratis.chronicle.artifacts.IClientArtifactsimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionForimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.readModels.ReadModelimport kotlin.reflect.KClass
@EventTypedata class StructuralDepsBookBorrowed(val bookId: String = "")
@EventTypedata class StructuralDepsBookReturned(val bookId: String = "")
@ReadModeldata class StructuralDepsBorrowedBook(val bookId: String = "")
class StructuralDepsBorrowedBooksProjection : IProjectionFor<StructuralDepsBorrowedBook> { override fun define(builder: IProjectionBuilderFor<StructuralDepsBorrowedBook>) { builder.from(StructuralDepsBookBorrowed::class) { it.set(StructuralDepsBorrowedBook::bookId).toProperty("bookId") } }}
class StructuralDepsMyArtifacts : IClientArtifacts { override val eventTypes: List<KClass<*>> = listOf(StructuralDepsBookBorrowed::class, StructuralDepsBookReturned::class) override val eventTypeMigrations: List<KClass<*>> = emptyList() override val readModels: List<KClass<*>> = listOf(StructuralDepsBorrowedBook::class) override val projections: List<KClass<*>> = listOf(StructuralDepsBorrowedBooksProjection::class) override val modelBoundProjections: List<KClass<*>> = emptyList() override val reactors: List<KClass<*>> = emptyList() override val reducers: List<KClass<*>> = emptyList() override val constraints: List<KClass<*>> = emptyList() override val eventSeeders: List<KClass<*>> = emptyList() override val webhooks: List<KClass<*>> = emptyList() override val captures: List<KClass<*>> = emptyList() override val reactorMiddlewares: List<KClass<*>> = emptyList() override val reactorArgumentResolvers: List<KClass<*>> = emptyList()}import io.cratis.chronicle.artifacts.IClientArtifacts;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;import io.cratis.chronicle.readModels.ReadModel;import io.cratis.chronicle.java.ProjectionBuilderJavaBridge;
import java.util.Collections;import java.util.List;
import kotlin.reflect.KClass;import kotlin.jvm.JvmClassMappingKt;
@EventTyperecord StructuralDepsBookBorrowed(String bookId) {}
@EventTyperecord StructuralDepsBookReturned(String bookId) {}
@ReadModelclass StructuralDepsBorrowedBook { public String bookId = "";}
class StructuralDepsBorrowedBooksProjection implements IProjectionFor<StructuralDepsBorrowedBook> { @Override public void define(IProjectionBuilderFor<StructuralDepsBorrowedBook> builder) { // AutoMap matches StructuralDepsBookBorrowed.bookId to StructuralDepsBorrowedBook.bookId by name. ProjectionBuilderJavaBridge.from(builder, StructuralDepsBookBorrowed.class); }}
class StructuralDepsMyArtifacts implements IClientArtifacts { private static KClass<?> kotlin(Class<?> type) { return JvmClassMappingKt.getKotlinClass(type); }
@Override public List<KClass<?>> getEventTypes() { return List.of(kotlin(StructuralDepsBookBorrowed.class), kotlin(StructuralDepsBookReturned.class)); }
@Override public List<KClass<?>> getEventTypeMigrations() { return Collections.emptyList(); }
@Override public List<KClass<?>> getReadModels() { return List.of(kotlin(StructuralDepsBorrowedBook.class)); }
@Override public List<KClass<?>> getProjections() { return List.of(kotlin(StructuralDepsBorrowedBooksProjection.class)); }
@Override public List<KClass<?>> getModelBoundProjections() { return Collections.emptyList(); }
@Override public List<KClass<?>> getReactors() { return Collections.emptyList(); }
@Override public List<KClass<?>> getReducers() { return Collections.emptyList(); }
@Override public List<KClass<?>> getConstraints() { return Collections.emptyList(); }
@Override public List<KClass<?>> getEventSeeders() { return Collections.emptyList(); }
@Override public List<KClass<?>> getWebhooks() { return Collections.emptyList(); }
@Override public List<KClass<?>> getCaptures() { return Collections.emptyList(); }
@Override public List<KClass<?>> getReactorMiddlewares() { return Collections.emptyList(); }
@Override public List<KClass<?>> getReactorArgumentResolvers() { return Collections.emptyList(); }}Elixir does not support this workflow yet.import { field, Constructor } from '@cratis/fundamentals';import { eventType, IClientArtifactsProvider, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@eventType()class StructuralDepsBookBorrowed { @field(String) readonly bookId: string;
constructor(bookId: string) { this.bookId = bookId; }}
@eventType()class StructuralDepsBookReturned { @field(String) readonly bookId: string;
constructor(bookId: string) { this.bookId = bookId; }}
class StructuralDepsBorrowedBook { bookId = '';}
@projection()class StructuralDepsBorrowedBooksProjection implements IProjectionFor<StructuralDepsBorrowedBook> { define(builder: IProjectionBuilderFor<StructuralDepsBorrowedBook>): void { builder.from(StructuralDepsBookBorrowed, _ => _ .set(m => m.bookId).to(e => e.bookId)); }}
class StructuralDepsMyArtifactsProvider implements IClientArtifactsProvider { readonly eventTypes: Constructor[] = [StructuralDepsBookBorrowed, StructuralDepsBookReturned]; readonly readModels: Constructor[] = []; readonly reactors: Constructor[] = []; readonly reducers: Constructor[] = []; readonly seeders: Constructor[] = []; readonly constraints: Constructor[] = []; readonly projections: Constructor[] = [StructuralDepsBorrowedBooksProjection]; readonly webhooks: Constructor[] = []; readonly eventTypeMigrations: Constructor[] = []; readonly globalForHandlers: Constructor[] = [];}Pass that provider to the client during construction:
using Microsoft.Extensions.Hosting;
public static class StructuralDepsCustomArtifactsProviderUsageRegistration{ public static void Configure(string[] args) { var builder = Host.CreateApplicationBuilder(args);
builder.AddCratisChronicle( configureOptions: options => options.EventStore = "my-store", configure: b => b.WithArtifactsProvider(new StructuralDepsMyArtifactsProvider())); }}import io.cratis.chronicle.ChronicleOptionsimport io.cratis.chronicle.connection.ChronicleConnectionString
fun optionsWithCustomArtifacts(): ChronicleOptions = ChronicleOptions( connectionString = ChronicleConnectionString.DEVELOPMENT, artifacts = StructuralDepsMyArtifacts())import io.cratis.chronicle.ChronicleOptions;import io.cratis.chronicle.connection.ChronicleConnectionString;
class StructuralDepsCustomArtifactsProviderUsage { ChronicleOptions create() { return new ChronicleOptions( ChronicleConnectionString.Companion.getDEVELOPMENT(), "Unknown", io.cratis.chronicle.sinks.WellKnownSinkTypes.MONGODB, true, new StructuralDepsMyArtifacts()); }}Elixir does not support this workflow yet.import { ChronicleOptions } from '@cratis/chronicle';
function createStructuralDependenciesCustomArtifactsProviderOptions(): ChronicleOptions { return ChronicleOptions.fromConnectionString('chronicle://localhost:35000', { clientArtifactsProvider: new StructuralDepsMyArtifactsProvider() });}If you only need to declare particular event types or observers in code, start with Explicit artifacts. Those declarations merge with discovered artifacts without requiring a replacement discovery provider.
DefaultClientArtifactsProvider
Section titled “DefaultClientArtifactsProvider”Keep DefaultClientArtifactsProvider when Chronicle’s discovery rules fit and only the assembly set needs to change. It discovers artifacts on first property access. Supply a custom assembly provider to control which assemblies it searches:
using Cratis.Types;
public static class StructuralDepsDefaultArtifactsProvider{ public static DefaultClientArtifactsProvider Create() { var assembliesProvider = new CompositeAssemblyProvider( ProjectReferencedAssemblies.Instance, PackageReferencedAssemblies.Instance);
return new DefaultClientArtifactsProvider(assembliesProvider); }}import io.cratis.chronicle.artifacts.ClientArtifactsimport io.cratis.chronicle.artifacts.IClientArtifacts
// Scans only the given packages and everything beneath them, instead of the whole classpath.fun scopedArtifacts(): IClientArtifacts = ClientArtifacts("com.acme.ordering", "com.acme.shipping")
// The classpath-wide instance used when no artifacts are configured, shared across every event// store in the process so the classpath is scanned at most once.fun defaultArtifacts(): IClientArtifacts = ClientArtifacts.defaultimport io.cratis.chronicle.artifacts.ClientArtifacts;import io.cratis.chronicle.artifacts.IClientArtifacts;
class StructuralDepsDefaultArtifactsProvider { // Scans only the given packages and everything beneath them, instead of the whole classpath. ClientArtifacts create() { return new ClientArtifacts("com.acme.ordering", "com.acme.shipping"); }
// The classpath-wide instance used when no artifacts are configured, shared across every // event store in the process so the classpath is scanned at most once. IClientArtifacts getDefault() { return ClientArtifacts.Companion.getDefault(); }}Elixir does not support this workflow yet.import { DefaultClientArtifactsProvider, TypeDiscoverer } from '@cratis/chronicle';
// TypeScript artifacts register with TypeDiscoverer.default when their decorated classes load.// ChronicleOptions' discoveryPatterns can also import files that match glob patterns.function createStructuralDependenciesDefaultArtifactsProvider(): DefaultClientArtifactsProvider { return new DefaultClientArtifactsProvider(TypeDiscoverer.default);}The static DefaultClientArtifactsProvider.Default instance uses project- and package-referenced assemblies. No explicit initialization call is required.
Continue to Namespaces to implement request-specific namespace selection, or Camel Casing to configure the naming policy.