Chronicle implicit subscriptions from shared event contracts
Cratis Chronicle can register forwarding and route C# observers automatically when their event contracts identify a source event store. Use implicit subscriptions when services share those contracts: the consumer handles the events it needs, and Chronicle routes it to that source’s inbox without a separate subscription API call.
The source still needs to publish events to its outbox. Adding [EventStore] to a contract identifies its origin; it does not publish events or move them out of the source’s ordinary event log.
How Implicit Subscriptions Work
Section titled “How Implicit Subscriptions Work”During client registration, Chronicle gathers the event types consumed by reactors, projections, and reducers and groups them by source store. For external sources, it registers a persistent forwarding subscription on the kernel and routes each observer to the corresponding inbox in the consumer’s store.
For reactors and reducers using automatic sequence inference, one declared external source selects its inbox. A source matching the current store, or no declared source, selects the log. Fluent projections infer an inbox from one declared source even when it names the current store. These rules do not locate local publications. When producers reuse their own attributed contracts, explicitly select the sequence containing those publications, normally EventSequenceId.Outbox, as described below.
The [EventStore] Attribute
Section titled “The [EventStore] Attribute”Apply [EventStore] to an event type to name the store that owns it:
using Cratis.Chronicle.Events;
[EventType][EventStore("fulfillment-service")]public record SubscriptionsImplicitShipmentDispatched(Guid OrderId, string TrackingNumber);
[EventType][EventStore("fulfillment-service")]public record SubscriptionsImplicitShipmentFailed(Guid OrderId, string Reason);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 string argument is the name of the source event store — the same name you would use if setting up an explicit subscription.
Publishing Event Types in a NuGet Package
Section titled “Publishing Event Types in a NuGet Package”A shared NuGet package lets the source service publish event contracts that name their origin. Consumers reference those types rather than redeclaring the contract and source name independently:
using Cratis.Chronicle.Events;
[EventType][EventStore("fulfillment-service")]public record SubscriptionsImplicitPkgShipmentDispatched(Guid OrderId, string TrackingNumber);
[EventType][EventStore("fulfillment-service")]public record SubscriptionsImplicitPkgShipmentFailed(Guid OrderId, string Reason);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.Assembly-Level Configuration with [EventStore]
Section titled “Assembly-Level Configuration with [EventStore]”When contracts in an assembly share one source, apply [EventStore] once in GlobalUsings.cs or AssemblyInfo.cs. A type-level attribute takes precedence if a particular contract needs a different source.
The .Contracts Project Pattern
Section titled “The .Contracts Project Pattern”Create a separate, thin project with the naming convention {ServiceName}.Events.Contracts to publish your event types:
FulfillmentService.Events.Contracts/├── AssemblyInfo.cs├── ShipmentDispatched.cs├── ShipmentFailed.cs└── FulfillmentService.Events.Contracts.csprojKeep the .Contracts project focused on event definitions and the dependencies needed to describe them. Consumers can then reference the contracts without taking a dependency on the source service’s implementation.
Project File Configuration
Section titled “Project File Configuration”Set the package identity and metadata in the .Contracts project file. Assembly-level [EventStore] requires Cratis.Chronicle 15.12.7 or later; the example references 19.162.0, a published release. The contracts project’s own PackageVersion is a placeholder for your package:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <TargetFramework>net10.0</TargetFramework> <AssemblyName>FulfillmentService.Events.Contracts</AssemblyName> <PackageId>FulfillmentService.Events.Contracts</PackageId> <PackageVersion>1.0.0</PackageVersion> <Title>Fulfillment Service Event Contracts</Title> <Description>Shared event types published by FulfillmentService</Description> <Authors>Your Team</Authors> <RepositoryUrl>https://github.com/yourorg/FulfillmentService</RepositoryUrl> <RepositoryType>git</RepositoryType> </PropertyGroup>
<ItemGroup> <PackageReference Include="Cratis.Chronicle" Version="19.162.0" /> </ItemGroup>
</Project>Assembly-Level Attribute Definition
Section titled “Assembly-Level Attribute Definition”In your .Contracts project, create an AssemblyInfo.cs file or use GlobalUsings.cs to apply [EventStore] once:
Option 1: Using AssemblyInfo.cs
Section titled “Option 1: Using AssemblyInfo.cs”using Cratis.Chronicle.Events;
[assembly: EventStore("fulfillment-service")]Option 2: Using GlobalUsings.cs (Recommended)
Section titled “Option 2: Using GlobalUsings.cs (Recommended)”global using System;global using System.Collections.Generic;global using Cratis.Chronicle.Events;
[assembly: EventStore("fulfillment-service")]Option 3: Using <AssemblyAttribute> in the .csproj (No C# File Needed)
Section titled “Option 3: Using <AssemblyAttribute> in the .csproj (No C# File Needed)”You can apply the assembly-level [EventStore] attribute directly from your .csproj, without creating any C# file. Add an <AssemblyAttribute> item to your project:
<ItemGroup> <AssemblyAttribute Include="Cratis.Chronicle.Events.EventStoreAttribute"> <_Parameter1>fulfillment-service</_Parameter1> </AssemblyAttribute></ItemGroup>MSBuild emits [assembly: Cratis.Chronicle.Events.EventStoreAttribute("fulfillment-service")] in the generated AssemblyInfo.cs file, so you do not need a separate C# file for the attribute.
Note: This approach requires
<GenerateAssemblyInfo>to betrue(the default). If your project sets<GenerateAssemblyInfo>false</GenerateAssemblyInfo>, the<AssemblyAttribute>item will have no effect since it relies on the auto-generated file.
Event Type Definitions
Section titled “Event Type Definitions”Now your event types only need [EventType]:
using Cratis.Chronicle.Events;
namespace SubscriptionsImplicitAssemblyExample{ [EventType] public record ShipmentDispatched(Guid OrderId, string TrackingNumber);
[EventType] public record ShipmentFailed(Guid OrderId, string Reason);}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.These event types inherit fulfillment-service as their source. A type-level [EventStore] overrides that assembly default.
Publishing and Consuming
Section titled “Publishing and Consuming”In FulfillmentService, reference the .Contracts project and publish the integration events to the fulfillment-service outbox. The attribute identifies the source but does not replace that publication step.
In the consuming service, reference FulfillmentService.Events.Contracts and create observers for the types you need. Configure Chronicle with the consumer’s own event store name, as shown below. Chronicle registers forwarding and routes those observers to inbox-fulfillment-service; you do not need a separate Subscribe call.
using Microsoft.AspNetCore.Builder;
public static class SubscriptionsImplicitConsumerSetup{ public static void Configure(string[] args) { var builder = WebApplication.CreateBuilder(args);
builder.AddCratisChronicle(options => options.EventStore = "order-service");
// Just reference the events from the NuGet package using var app = builder.Build(); // Reactors/projections that observe FulfillmentService.Events types // are automatically routed to the inbox }}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.Automatic Inbox Routing for Observers
Section titled “Automatic Inbox Routing for Observers”Automatic inbox routing is documented per observer type:
- Reactors: Subscribe Reactors to External Event Stores
- Reducers: Subscribe Reducers to External Event Stores
Projections
Section titled “Projections”A fluent projection can build the consumer’s view from forwarded events. Without an explicit sequence, one declared source selects inbox-{sourceEventStore} even when that source is the current store:
using Cratis.Chronicle.Projections;
public record SubscriptionsImplicitFulfillmentReadModel(string TrackingNumber, string Status);
public class SubscriptionsImplicitFulfillmentProjection : IProjectionFor<SubscriptionsImplicitFulfillmentReadModel>{ public void Define(IProjectionBuilderFor<SubscriptionsImplicitFulfillmentReadModel> builder) => builder .From<SubscriptionsImplicitShipmentDispatched>(_ => _ .Set(m => m.Status).ToValue("Dispatched")) .From<SubscriptionsImplicitShipmentFailed>(_ => _ .Set(m => m.Status).ToValue("Failed"));}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.If the source service reuses these attributed contracts in its own fluent projection, select the sequence containing its publications. Normally, call FromEventSequence(EventSequenceId.Outbox) on the projection builder. These contracts register as public events, which the kernel rejects in the log. EventSequenceId is in the Cratis.Chronicle.EventSequences namespace. Without this explicit selection, the projection reads the inbox named for its own store, not its local publications.
After publishing an event to the source outbox, check for its copy in the consumer inbox and then for the read-model update. If the event arrives but the view does not change, investigate the projection rather than recreating the forwarding subscription.
Subscription Lifecycle
Section titled “Subscription Lifecycle”Once registered, the forwarding definition survives client disconnections and kernel restarts. The kernel can continue forwarding matching outbox events while the consumer is offline. Consumer observers still need to run to process the inbox events.
Removing the consumer’s handlers does not automatically remove the stored forwarding subscription. Implicit registrations use the source store name as the subscription ID. If the relationship is no longer needed, remove the consuming observers and remove the stored subscription through the explicit subscription API.
Mixing Event Stores Is Not Allowed
Section titled “Mixing Event Stores Is Not Allowed”With automatic sequence inference, multiple distinct declared [EventStore] sources in one observer throw MultipleEventStoresDefined during registration:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Reactors;
[EventType][EventStore("ordering-service")]public record SubscriptionsImplicitOrderPlaced(Guid OrderId, decimal Amount);
// ❌ This will throw MultipleEventStoresDefinedpublic class SubscriptionsImplicitInvalidReactor : IReactor{ // SubscriptionsImplicitShipmentDispatched has [EventStore("fulfillment-service")] public Task Handle(SubscriptionsImplicitShipmentDispatched @event) => Task.CompletedTask;
// SubscriptionsImplicitOrderPlaced has [EventStore("ordering-service")] public Task Handle(SubscriptionsImplicitOrderPlaced @event) => Task.CompletedTask;}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.Unannotated local event types have no declared source and are ignored by this check. Mixing them with one external source does not throw: automatic routing selects that source’s inbox, so the observer does not receive local events from its own log. Avoid mixing local and external handlers in one observer.
Keep each observer on one source by splitting the handlers:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Reactors;
public class SubscriptionsImplicitFulfillmentReactor : IReactor{ public Task ShipmentDispatched(SubscriptionsImplicitShipmentDispatched @event, EventContext context) => HandleFulfillmentAsync(@event);
Task HandleFulfillmentAsync(SubscriptionsImplicitShipmentDispatched @event) => Task.CompletedTask;}
public class SubscriptionsImplicitOrderingReactor : IReactor{ public Task OrderPlaced(SubscriptionsImplicitOrderPlaced @event, EventContext context) => HandleOrderAsync(@event);
Task HandleOrderAsync(SubscriptionsImplicitOrderPlaced @event) => Task.CompletedTask;}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.Schema Registration and Metadata
Section titled “Schema Registration and Metadata”Event type registration includes the source event store name in its metadata. Keep that name aligned with the publisher’s configured store: Chronicle uses it to select the source outbox and derive the consumer’s inbox-{sourceEventStore} sequence.
When to Use Implicit Subscriptions
Section titled “When to Use Implicit Subscriptions”Use implicit subscriptions when the shared C# contracts describe where events come from and the consumer’s observers determine which types it needs. Only those consumed types are selected for forwarding, not every type in the source store.
Use explicit subscriptions when the consumer needs to choose a stable subscription ID, manage the relationship at runtime, or select types through the API instead. For either approach, Subscription recovery helps locate a delivery failure before you replay events.
See Also
Section titled “See Also”- Explicit Event Store Subscriptions — manual subscription using the Subscribe API
- Outbox and Inbox — conceptual explanation of how events flow between stores
- Reactors — processing inbox events with reactors
- Projections — building read models from inbox events