Path: redb-route/redb.Route.Core/Transactions/RedbTransactedActionExtensions.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Core/Transactions/RedbTransactedActionExtensions.cs ---- using System.Collections.Concurrent; using redb.Core; using redb.Route.Abstractions; using redb.Route.RedbCore.Extensions; using redb.Route.Transactions; namespace redb.Route.RedbCore.Transactions; /// /// DSL extensions for opening an explicit redb transaction inside a route pipeline /// and enrolling it in the standard commit/rollback flow. /// /// Pairs with the existing .Transacted(...) / .BeginTransaction(...) / /// .CommitTransaction() / .RollbackTransaction() primitives: /// when a RedbTransactedAction is registered in exchange.Properties["TRANSACT_ACTION"], /// it is committed or rolled back together with all other transports at the standard /// boundary. /// /// /// Resolution model: the underlying is resolved through /// , /// so anonymous singletons, named singletons, and named per-exchange scoped factories all work /// (the same model used by ProcessWithRedb(name, ...)). /// /// public static class RedbTransactedActionExtensions { /// /// Property-key prefix used when registering a in /// exchange.Properties["TRANSACT_ACTION"]. The full key is "redb:{name}" /// (or "redb:" for the default unnamed instance). /// public const string ActionKeyPrefix = "redb:"; /// /// Opens an explicit on the default /// and enrolls it as an in the exchange so that the /// surrounding (or imperative Commit/RollbackTransaction) /// will close it. /// [Obsolete("Use .Transacted() instead: under an ambient transaction scope redb enlists automatically " + "via core's AmbientConnectionRegistry, so BeginRedbTransaction is a no-op there and only opens " + "its own transaction when no ambient scope is active.")] public static IRouteDefinition BeginRedbTransaction(this IRouteDefinition route) => BeginRedbTransactionCore(route, name: null); /// /// Opens an explicit on a named /// (per-exchange scoped when a factory is registered, see /// ) /// and enrolls it as an in the exchange. /// /// Route definition. /// Named redb instance (e.g. "orders-db"). [Obsolete("Use .Transacted() instead: under an ambient transaction scope redb enlists automatically " + "via core's AmbientConnectionRegistry, so BeginRedbTransaction is a no-op there and only opens " + "its own transaction when no ambient scope is active.")] public static IRouteDefinition BeginRedbTransaction(this IRouteDefinition route, string name) { ArgumentException.ThrowIfNullOrEmpty(name); return BeginRedbTransactionCore(route, name); } private static IRouteDefinition BeginRedbTransactionCore(IRouteDefinition route, string? name) { ArgumentNullException.ThrowIfNull(route); return route.Process(async (exchange, ct) => { var context = route.GetContext() ?? throw new InvalidOperationException( "RouteContext is not available. Ensure the route builder has been configured."); // Under an ambient TransactionScope (.Transacted()), redb already writes into that // transaction via core's AmbientConnectionRegistry, and redb.Context.BeginTransactionAsync() // is rejected there (core 5dc3741e). Attach without opening: nothing to enroll — the scope // owns commit/rollback. Only the legacy path (no ambient scope, e.g. .Transacted(Suppress) // or no transaction at all) opens its own redb transaction. if (System.Transactions.Transaction.Current is not null) return; // Always resolve a PER-EXCHANGE scoped redb (its own connection) — never a captive // singleton. The unnamed path routes through the scoped resolver (see GetRedbService). var redb = context.GetRedbService(name ?? string.Empty, exchange); var actionKey = ActionKeyPrefix + (name ?? string.Empty); // Idempotency: if a redb tx with this key is already enrolled in this exchange, // do not open a second one (Required-like semantics on the redb side). var actions = GetOrCreateActionsBag(exchange); if (actions.ContainsKey(actionKey)) return; var tx = await redb.Context.BeginTransactionAsync().ConfigureAwait(false); actions[actionKey] = new RedbTransactedAction(tx); }); } private static ConcurrentDictionary GetOrCreateActionsBag(IExchange exchange) { if (exchange.Properties.TryGetValue(TransactedProcessor.TransactActionPropertyKey, out var raw) && raw is ConcurrentDictionary existing) { return existing; } var bag = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); exchange.Properties[TransactedProcessor.TransactActionPropertyKey] = bag; return bag; } }