Fluens.Messaging.Sagas
0.9.4
dotnet add package Fluens.Messaging.Sagas --version 0.9.4
NuGet\Install-Package Fluens.Messaging.Sagas -Version 0.9.4
<PackageReference Include="Fluens.Messaging.Sagas" Version="0.9.4" />
<PackageVersion Include="Fluens.Messaging.Sagas" Version="0.9.4" />
<PackageReference Include="Fluens.Messaging.Sagas" />
paket add Fluens.Messaging.Sagas --version 0.9.4
#r "nuget: Fluens.Messaging.Sagas, 0.9.4"
#:package Fluens.Messaging.Sagas@0.9.4
#addin nuget:?package=Fluens.Messaging.Sagas&version=0.9.4
#tool nuget:?package=Fluens.Messaging.Sagas&version=0.9.4
Fluens.Messaging.Sagas
Process Manager / Saga abstraction layered on top of Fluens.Messaging. State lives in the module's own
FluensDbContext, the saga write commits in the same SaveChangesAsync as the inbox ACK and any
business outbox row, and the four governing ADRs are 0028 (saga abstraction), 0029 (portable
uint Version concurrency), 0030 (timeouts via deferred outbox), and 0031 (operator surfaces +
saga metrics).
Installation
dotnet add package Fluens.Messaging.Sagas
Usage
Registration
WithSagas(...) is the single entry point hung off MessagingModuleBuilder; invoke it from inside
the AddModuleMessaging<TDbContext> callback so the builder's IServiceCollection + TDbContext
are seeded. Every saga tuple is registered via RegisterSaga<TSaga, TData>(); WithSagas reflects
over each saga's IStartedBy<T> / IHandleSagaMessage<T> / IHandleSagaTimeout<T> markers and
wires one closed-generic SagaDispatcher<TSaga, TData, TMessage> per consumed message type as the
module's IMessageConsumer<TMessage> (ADR 20260603000002 Rule 7).
module.AddMessaging<OrdersDbContext>(msg =>
{
msg.Publishes<OrderCreatedMessage>();
msg.WithSagas(s =>
{
s.RegisterSaga<OrderFulfillmentSaga, OrderFulfillmentData>();
});
});
WithSagas(...) auto-wires the SagaSaveChangesInterceptor singleton on every module
FluensDbContext opting into sagas (the interceptor is registered both as its concrete type and as
an ISaveChangesInterceptor enumerable entry so FluensDbContext.OnConfiguring picks it up
without you touching OnConfiguring; mirrors AddOutboxInterceptor's idempotency pattern per
ADR 20260603000003 Rule 4 — the double-registration guard lives on OnConfiguring, not on the extension
method). An empty WithSagas block is a structural no-op (mirrors the AddXxx() convention).
Authoring a saga
A saga is an abstract subclass of Saga<TData> where TData : SagaData, new(). The base
SagaData carries the infrastructure columns Guid Id / DateTimeOffset CreatedAt /
DateTimeOffset? CompletedAt / [ConcurrencyCheck] uint Version — handler code never reads or
writes them.
Lazy-loading proxies.
SagaDataitself isabstract, so EF Core'sProxyBindingRewriterskips it at model finalization — only your concreteSagaDatasubclass is affected. If the module'sFluensDbContextenablesUseLazyLoadingProxies()(see theFluens.MessagingREADME), yourSagaDatasubclass MUST NOT besealedand MUST keep an accessible (non-private) parameterless constructor, or the host crashes at model finalization / first query / row materialization respectively — the same three requirementsFluens.Messaging's own entities satisfy (ADR 20260910000001).
public class OrderFulfillmentData : SagaData
{
public Guid OrderId { get; set; }
public string CustomerEmail { get; set; } = string.Empty;
public bool PaymentCaptured { get; set; }
public Guid? OutstandingTimeoutToken { get; set; }
}
public sealed class OrderFulfillmentSaga
: Saga<OrderFulfillmentData>,
IStartedBy<OrderPlaced>,
IHandleSagaMessage<PaymentCaptured>,
IHandleSagaTimeout<PaymentTimeout>
{
protected override void Configure(ISagaCorrelation<OrderFulfillmentData> c)
{
c.Correlate<OrderPlaced>(m => m.OrderId, d => d.OrderId);
c.Correlate<PaymentCaptured>(m => m.OrderId, d => d.OrderId);
}
public async Task Handle(OrderPlaced msg, ISagaContext ctx, CancellationToken ct)
{
Data.OrderId = msg.OrderId;
Data.CustomerEmail = msg.CustomerEmail;
Data.OutstandingTimeoutToken =
ctx.RequestTimeout(new PaymentTimeout(msg.OrderId), TimeSpan.FromMinutes(15));
await ctx.Publisher.Publish(new RequestPayment(msg.OrderId));
}
public Task Handle(PaymentCaptured msg, ISagaContext ctx, CancellationToken ct)
{
Data.PaymentCaptured = true;
MarkAsComplete();
return Task.CompletedTask;
}
public Task Handle(PaymentTimeout payload, ISagaContext ctx, CancellationToken ct)
{
var firedToken = Guid.Parse(ctx.Headers["fluens-saga-timeout-token"]);
if (Data.OutstandingTimeoutToken != firedToken)
return Task.CompletedTask; // stale firing — silent-ignore
// compensating action
return Task.CompletedTask;
}
}
Correlation
Saga lookup is by business correlation key declared in Configure via
ISagaCorrelation<TData>.Correlate(messageKey, dataKey). The dispatcher emits a single
SELECT … WHERE <dataKey> = @value and only uses Guid Id lookup when the saga author explicitly
correlates on Id. The pure SagaCorrelationMap<TData> planner is unit-testable without a DB.
A non-start message arriving before its start message follows the silent-ignore path
(ACK + Information log, no orphan row). The start message arriving twice (idempotent inbox retry)
finds the existing row and re-enters Handle, not creating a second row.
SagaPublisherWrapper auto-stamps the fluens-saga-correlation: <sagaType>:<correlationKey>
header on every non-timeout outbound message before publish — cross-app traceability is the
default branch, there is no [CrossAppSaga] opt-in.
Completion and retention
MarkAsComplete() is soft — sets CompletedAt, never deletes the row. The partial active index
WHERE completed_at IS NULL excludes completed rows from the saga's hot-path; it is created by
FluensMigration.CreateSagaTable (see Saga table) — the EF model declares no index.
Subsequent messages for a completed correlation key route to silent-ignore (ADR 20260603000002 Rule 6).
Retention of completed rows is handled by the per-saga-type SagaRetentionSweeper<TData> plugged
into the existing per-module HousekeepingWorker via the generic IRetentionSweeper hook
(Fluens.Messaging.Maintenance), registered as a keyed service under the module name: the module's
HousekeepingWorker runs only the sweepers keyed by its own name, and a sweeper registered without a key
is not run. There is no separate saga retention worker. The cutoff is
MessagingOptions.SagaRetentionDays (default 7, validated > 0). The sweeper deletes in batches of
batchSize (the host's Fluens:Messaging:OutboxBatchSize, default 500, passed by HousekeepingWorker),
oldest completed_at first, one DELETE per batch, and repeats while a batch was full; the retention
batch size has no option of its own.
Timeouts (ADR 20260603000004)
A saga timeout is a message type implementing the empty ISagaTimeout : IMessage marker handled
via IHandleSagaTimeout<TTimeout> where TTimeout : ISagaTimeout. The two markers
(IHandleSagaMessage<T> and IHandleSagaTimeout<T>) MUST NOT be mixed on one type. An exception thrown from a timeout handler is classified exactly like one thrown from IHandleSagaMessage<T>.Handle: a BusinessFaultException takes the ACK-and-publish path and a PermanentConsumeFailureException is dead-lettered on the first attempt, including when the handler throws before it returns its Task.
ctx.RequestTimeout<TTimeout>(payload, delay) returns the timeout token: a version 7 Guid that is
the timeout's own MessageId (the outbox row id, the inbox MessageId and the MessageContext.MessageId
the timeout handler sees all equal it). Storing it on TData for the later stale-check is the saga
author's responsibility (the framework does not auto-stamp it onto state).
PublishTimeout, which RequestTimeout calls, stamps the timeout headers: the same token value on the
fluens-saga-timeout-token header in "D" format (the header stays, so stale checks written against it
keep working) and fluens-directed-module: <currentModule> (the bare module name, per ADR 20260604000001:
the prior <currentApp>/<currentModule> shape mismatched the in-app subscription registry and dropped
every timeout), plus the DeliverAfter(delay) offset, so the timeout returns to the originating saga
module via the existing partitioned inbox. A plain Publish of an ISagaTimeout stamps only the
correlation header. The directed-module header is load-bearing: a timeout published through
PublishTimeout never crosses the mesh, because LocalFanOut.DistributeLocallyAsync honours
fluens-directed-module (ADR 20260601000005), and IngressDistributionProcessor honours it
identically (ADR 20260530000005).
The framework does NOT pre-compare the timeout token against any saga field — the saga author
writes the stale-check (if (Data.Token != ctx.Headers["fluens-saga-timeout-token"]) return;).
A stale firing is observable via the saga.timeout.stale counter when the author flags it.
ISagaControl.CancelTimeoutAsync(sagaId, timeoutToken, ct) is best-effort + race-aware: the token is
the outbox row id, so cancel is one DELETE FROM outbox WHERE id = @token AND sent_at IS NULL AND expired_at IS NULL, with no row loaded and no envelope read. It returns the number of deleted rows,
0 or 1; 0 rows affected does NOT throw. sagaId is unused. A timeout already sent or expired is kept
and cancel returns 0, so a delivered timeout cannot be cancelled; the saga's stale-token check is the
load-bearing invariant.
Sub-second timeout precision is OUT of scope — the cadence is
TransportProcessorIntervalSeconds (default 60s) for the outbox-to-inbox leg and
InboxProcessorIntervalSeconds (default 30s) for the inbox-to-handler leg.
Concurrency (ADR 20260603000003)
SagaData.Version is the portable optimistic-concurrency token: [ConcurrencyCheck] uint Version
incremented by SagaSaveChangesInterceptor on EntityState.Modified entries only (Added keeps
Version = 0, the INSERT carries that value). A DbUpdateConcurrencyException raised by the saga
commit propagates to the outer LaneWorker retry chain unchanged — the two-stage RetryPolicy
(ADR 20260530000004) classifies it as transient and replays on a fresh scope. The saga library never catches
the exception, never classifies it as IPermanentConsumeFailure, and never implements its own
retry loop. Each replay attempt observable on the saga.concurrency_conflict counter.
The retry applies only to a conflict without the inbox row. When the conflict includes the inbox row, another
process has already acknowledged the message: the lane attempt ends as already processed (rolled back, no retry,
no dead letter, one inbox.already_processed count, see Fluens.Messaging). The saga.concurrency_conflict
counter still records that conflict. ADR 20261116170717 amends ADR 20260603000002 Rule 8 and ADR 20260603000003 Rule 5.
Provider-specific tokens (byte[] RowVersion / SQL Server rowversion/timestamp, PostgreSQL
xmin) and replacement types (long / int / Guid) are forbidden — portability across SQL
Server / PostgreSQL / future providers is the design goal.
Operator surfaces (ADR 20260603000005 Rule 1)
Three SRP-aligned interfaces mirror the ADR 20260531000001 messaging split — each internal sealed,
per-module, scoped to the module's own FluensDbContext via IServiceScopeFactory. The "Admin"
suffix is forbidden (ADR 20260531000001). The consumer depends on the narrowest interface needed; absence of
a registration is a no-op / empty result, not an exception.
ISagaMaintenance— read / diagnostics, SQL-side:QuerySagasAsync(SagaFilter)→IReadOnlyList<SagaSummary>runs one database query per registeredSagaDatasubclass (found viadbContext.Model.GetEntityTypes(), the typed query closed once per type and cached), honouring everySagaFilterfield in theWHEREclause (SagaTypeselects which types are queried), newestCreatedAtfirst, andSagaFilter.Limitas a row limit; the per-type results are merged newest-first and cut toLimit.GetSagaAsync(sagaId)→SagaDetail(aWHERE id = @idlookup per saga type until a hit) whoseStateis a reflection-based projection over EF-mapped properties skipping the four base columns (Id/CreatedAt/CompletedAt/Version);GetActiveSagaCountAsync(sagaType)is a singleCOUNT(*) WHERE completed_at IS NULL.ISagaRecovery— replay / retry, forward-looking:ReplaySagaAsync(sagaId)loads the saga by id, setsCompletedAt = NULLin one transaction and stages aSagaReplaySignalinbox row only for a saga that declares replay handling (see below); any other saga only gets itsCompletedAtreset. An absent saga returns0without opening a transaction, a found saga returns1.ISagaControl— destructive override:ForceCompleteSagaAsync(sagaId, reason)setsCompletedAt = now()AND stages aSagaForceCompleteEventoutbox row carrying the operator'sreasonin one transaction (the event flows through the standard outbox path; consumers that care about saga-completion-audit subscribe normally);ISagaControl.CancelTimeoutAsync(sagaId, timeoutToken)is best-effort + race-aware: one DELETE by id (the token is the outbox row id) that skips sent and expired rows and returns 0 or 1;sagaIdis unused.
Resolving the calling module's own surface in a multi-saga-module host. WithSagas(...)
registers each of these three interfaces both unkeyed (TryAddScoped — first-registered module
wins, kept for single-saga-module back-compat) and keyed by module name
(TryAddKeyedScoped). In a host with two or more saga-enabled modules, resolve the keyed
registration to reach the calling module's own DbContext — serviceProvider .GetRequiredKeyedService<ISagaMaintenance>(moduleName) — never the unkeyed one, which silently
resolves against whichever module registered first.
Declaring a replayable saga. ReplaySagaAsync signals a saga only when its type implements
IHandleSagaMessage<SagaReplaySignal>. That marker interface alone is the declaration: WithSagas does not
inspect the correlation, and a saga that implements only IStartedBy<SagaReplaySignal> does not qualify.
The saga also declares the signal's correlation on its Id in Configure:
protected override void Configure(ISagaCorrelation<OrderFulfillmentData> c)
{
c.Correlate<SagaReplaySignal>(m => m.SagaId, d => d.Id);
}
public Task Handle(SagaReplaySignal msg, ISagaContext ctx, CancellationToken ct)
{
// re-enter the saga after the operator's fix
return Task.CompletedTask;
}
Each declaring saga type gets its own SagaReplaySignal inbox row, stamped with that saga's own dispatcher
handler-type name, so several saga types that share one saga data type each re-enter. The signal waits for the
next inbox polling cycle (InboxProcessorIntervalSeconds, default 30 s); the replay commit does not wake the
inbox. A saga without the declaration gets only its CompletedAt reset and no inbox row. A replayable saga whose
Configure lacks the Correlate<SagaReplaySignal> entry has the signal acknowledged without reaching its handler.
SagaFilter combines its set fields with AND semantics (an empty filter selects every saga):
| Field | Effect in SQL |
|---|---|
SagaType |
Restricts the query to the saga-state type with this FullName |
CreatedAfter / CreatedBefore |
created_at >= / <= the instant (inclusive, compared in UTC) |
CompletedAfter / CompletedBefore |
completed_at >= / <= the instant (inclusive, compared in UTC) |
OnlyActive / OnlyCompleted |
completed_at IS NULL / IS NOT NULL |
CorrelationKey |
id equal to the key parsed as a GUID; a key that is not a GUID matches no saga |
Limit |
Maximum sagas returned, newest CreatedAt first; null = unbounded; <= 0 throws ArgumentOutOfRangeException |
var maintenance = sp.GetRequiredKeyedService<ISagaMaintenance>("orders");
var stuck = await maintenance.QuerySagasAsync(
new SagaFilter { OnlyActive = true, CreatedBefore = DateTimeOffset.UtcNow.AddHours(-1), Limit = 50 },
cancellationToken);
Saga table
The saga table and its indexes come from Fluens.Migrations:
CreateSagaTable(tableName, schema) creates id (primary key), created_at, completed_at and version
(typed by the provider conventions' SagaVersionColumnType, bigint on PostgreSQL and SQL Server), the partial
ix_<tableName>_active (created_at DESC WHERE completed_at IS NULL), ix_<tableName>_created_at and
ix_<tableName>_completed_at, and returns the table syntax so you chain your business columns. These indexes
back the SQL-side operator queries above.
public class CreateOrderFulfillmentSaga : FluensMigration
{
public CreateOrderFulfillmentSaga(IMigrationProviderConventions conventions) : base(conventions) { }
public override void Up() =>
CreateSagaTable("order_fulfillment", "orders")
.WithColumn("order_id").AsGuid().NotNullable()
.WithColumn("customer_email").AsString(200).NotNullable()
.WithColumn("payment_captured").AsBoolean().NotNullable()
.WithColumn("outstanding_timeout_token").AsGuid().Nullable();
public override void Down() => DeleteSagaTable("order_fulfillment", "orders");
}
The saga base columns are verified at host start. When WithSagas registers a saga, the startup schema
verification of Fluens.Messaging (see its README) also checks that each saga table exists and carries the
SagaData base columns id, created_at, completed_at and version with the expected store types and
nullability; a missing table is reported with CreateSagaTable("<table>", "<schema>") as the remedy. Saga business
columns are not checked. Fluens:Messaging:VerifySchemaOnStartup set to false switches the check off.
Diagnostics (ADR 20260603000005 Rule 3)
Saga instruments ride the existing Fluens.Messaging BCL meter — no new static class, no new
meter name. Seven new instruments added to MessagingMetrics itself, all tagged with module +
saga.type:
saga.active— absoluteObservableGauge<long>backed by a per-(module, saga.type)ConcurrentDictionaryregistry (mirroringinbox.lag's pattern, ADR 20260601000001 / ADR 20260603000005 Rule 4 — restart-safe, never a+1/-1delta). Fed by the per-module hostedSagaActiveObserver<TDbContext>(cadence reusesInboxProcessorIntervalSeconds).saga.started,saga.completed,saga.timeout.fired,saga.timeout.stale,saga.concurrency_conflict— counters.saga.handle.duration— histogram (ms).
The recording methods on MessagingMetrics (SetSagaActive / RemoveSagaActive /
RecordSagaStarted / RecordSagaCompleted / RecordSagaTimeoutFired /
RecordSagaTimeoutStale / RecordSagaConcurrencyConflict / RecordSagaHandleDuration) take
only primitive types so the static class stays type-agnostic (no saga type is referenced from
Fluens.Messaging).
Leader scope (ADR 20261116190646)
Saga handlers run in the inbox lane workers, and the saga timeouts travel through the outbox transport. Both are gated through the ILeadership of the process (the BackgroundWorker.LeaderOnly flag, with the lanes bound to the leader term): only the leader of a replica runs them, and a waiting process runs no saga handler and no timeout delivery. The saga active observer (the saga.active gauge) is gated too. The start message of a saga therefore reaches a saga handler only on the leader; the other processes of the replica leave the message pending. The stop ordering and the concurrent-stop caveat are described in the Fluens.Messaging README (Deployment Scope). Keep the default sequential stop for a host that runs sagas: a saga step committed by a process that lost its lease mid-lane is still fenced by the Version check, so no saga state is written twice.
Cross-app caveat
Cross-app traceability is the default branch — there is no [CrossAppSaga] opt-in attribute.
The cross-app posture of a given outbound message is decided per-message-type by the existing
subscription topology; the saga abstraction stamps the correlation header unconditionally.
A timeout published through PublishTimeout is the single exception: the wrapper auto-stamps
fluens-directed-module so the timeout returns to the originating saga module via the existing
partitioned inbox, and it never crosses the mesh to a non-originating app, by structural invariant
(LocalFanOut + IngressDistributionProcessor honour the header symmetrically; the library
does not expose a way to disable this stamping). A plain Publish of an ISagaTimeout carries no
directed-module header and follows the subscription topology like any other message.
Configuration
The saga retention window is bound from the existing "Fluens:Messaging" configuration section:
| Property | Default | Description |
|---|---|---|
SagaRetentionDays |
7 | Retention in days for terminal saga rows (CompletedAt != null AND CompletedAt < cutoff) before SagaRetentionSweeper<TData> deletes them in batches ordered by completed_at via the per-module HousekeepingWorker. Must be > 0. |
No new configuration section — the saga library extends the existing MessagingOptions block.
Release notes
0.8.5 (breaking)
ISagaMaintenance.QuerySagasAsyncselects in SQL: everySagaFilterfield becomes aWHEREpredicate, results come back newestCreatedAtfirst, and the newSagaFilter.Limitbounds the row count. Callers that relied on the previous unspecified result order must sort themselves;Limit <= 0throwsArgumentOutOfRangeException.GetSagaAsync,GetActiveSagaCountAsync,ReplaySagaAsyncandForceCompleteSagaAsynclook the saga up by id in SQL instead of enumerating saga tables in memory.FluensMigration.CreateSagaTable/DeleteSagaTable(Fluens.Migrations) create the saga table with the partial active-saga index plus the created-at and completed-at indexes. The provider conventions gainSagaVersionColumnType, so a third-partyIMigrationProviderConventionsimplementation must add it.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Fluens (>= 0.9.4)
- Fluens.Kernel (>= 0.9.4)
- Fluens.Messaging (>= 0.9.4)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.13)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Logging (>= 10.0.13)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.13)
- Scrutor (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.9.4 | 91 | 11/30/2026 |
| 0.9.3 | 107 | 11/17/2026 |
| 0.9.2 | 137 | 10/21/2026 |
| 0.9.1 | 110 | 10/13/2026 |
| 0.9.0 | 108 | 10/13/2026 |
| 0.8.7 | 110 | 10/12/2026 |
| 0.8.6 | 107 | 10/12/2026 |
| 0.8.5 | 111 | 10/7/2026 |
| 0.8.4 | 119 | 10/6/2026 |
| 0.8.3 | 114 | 10/6/2026 |
| 0.8.2 | 117 | 10/5/2026 |
| 0.8.1 | 129 | 9/15/2026 |
| 0.8.0 | 127 | 9/15/2026 |
| 0.7.11 | 126 | 9/15/2026 |
| 0.7.10 | 127 | 9/13/2026 |
| 0.7.9 | 128 | 9/11/2026 |
| 0.7.8 | 132 | 9/10/2026 |
| 0.7.7 | 158 | 9/10/2026 |
| 0.7.6 | 157 | 7/1/2026 |
| 0.7.5 | 155 | 6/22/2026 |