Path: redb-route/redb.Route.Core/Extensions/RedbContextDslExtensions.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/Extensions/RedbContextDslExtensions.cs ---- using System.Reflection; using Microsoft.Extensions.DependencyInjection; using redb.Core; using redb.Route.Abstractions; using redb.Route.Components; using redb.Route.RedbCore.Repositories; namespace redb.Route.RedbCore.Extensions; /// /// Context-level redb bridge: scheme synchronization at context startup and named idempotent /// repositories in the context registry — the pieces a packaged module declares in /// context.xml (they run before any route starts, on the engine's fail-fast /// OnContextStarting hook) and a C# host calls directly. /// public static class RedbContextDslExtensions { /// /// Synchronizes the redb scheme for when the context starts — /// before any route accepts traffic, failing the start when the scheme cannot be brought up /// (the engine's OnContextStarting semantics). /// /// Route context. /// The props CLR type whose scheme to synchronize. /// Named ; null/empty — the default one. public static IRouteContext SyncRedbScheme(this IRouteContext context, Type propsType, string? storage = null) { ArgumentNullException.ThrowIfNull(context); ArgumentNullException.ThrowIfNull(propsType); // Reflection once at registration; the listener invokes a prepared delegate. var sync = (Func)SyncHelperDef .MakeGenericMethod(propsType) .CreateDelegate(typeof(Func)); context.AddLifecycleListener(new StartupAction(async ctx => { var redb = string.IsNullOrEmpty(storage) ? ctx.GetRedbService() : ctx.GetRedbService(storage); await sync(redb).ConfigureAwait(false); })); return context; } /// /// Registers a in the context registry under /// , as is — the name /// IdempotentConsumer(keyExpression, repositoryName) and the markup's /// <idempotentConsumer repository="…"> look up. Built lazily when the context /// starts (the repository needs the context's service provider for per-exchange scoping). /// /// Route context. /// Registry name of the repository. /// Idempotency-entry lifetime; null — keep forever. /// Logical processor name partitioning the entries; default — the registry name. /// /// The named redb instance the entries are kept in (context.RegisterRedbService, or a Tsak Redb /// section); null for the context's default redb, as resolves it. /// public static IRouteContext AddRedbIdempotentRepository( this IRouteContext context, string name, TimeSpan? ttl = null, string? processorName = null, string? storage = null) { ArgumentNullException.ThrowIfNull(context); ArgumentException.ThrowIfNullOrEmpty(name); context.AddLifecycleListener(new StartupAction(async ctx => { var options = new RedbIdempotentOptions { ProcessorName = processorName ?? name, Ttl = ttl, }; if (string.IsNullOrEmpty(storage)) { // The context's default redb: a scope of the context's service provider per operation. var scopeFactory = ctx.GetServiceProvider()?.GetService() ?? throw new InvalidOperationException( $"AddRedbIdempotentRepository('{name}'): the context has no service provider — " + "the repository resolves a scoped IRedbService per exchange and needs DI."); ctx.AddIdempotentRepository(name, new RedbIdempotentRepository(scopeFactory, options)); return; } // A named instance: a scope of it per operation, from the same source the redb steps of that storage use. Func openScope = () => ctx.CreateRedbScope(storage); // Opened once at start, so a storage name that resolves to nothing fails the start, not the first message. await using (openScope().ConfigureAwait(false)) { } ctx.AddIdempotentRepository(name, new RedbIdempotentRepository(openScope, options)); })); return context; } // ── helpers ────────────────────────────────────────────────────── private static readonly MethodInfo SyncHelperDef = typeof(RedbContextDslExtensions) .GetMethod(nameof(SyncHelper), BindingFlags.NonPublic | BindingFlags.Static)!; private static Task SyncHelper(IRedbService redb) where TProps : class => redb.SyncSchemeAsync(); /// A lifecycle listener wrapping one startup action (fail-fast by the hook's contract). private sealed class StartupAction(Func action) : IRouteLifecycleListener { public Task OnContextStarting(IRouteContext context, CancellationToken ct) => action(context); } }