Skip to content

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.

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

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()));
}

View C# snippet source on GitHub

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.

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));
}
}

View C# snippet source on GitHub

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));
}
}

View C# snippet source on GitHub

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.

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.

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 => [];
}

View C# snippet source on GitHub

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()));
}
}

View C# snippet source on GitHub

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.

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);
}
}

View C# snippet source on GitHub

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.