Path: redb-route/redb.Route/Core/ProducerTemplate.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/ProducerTemplate.cs ---- using System.Diagnostics; using System.Collections.Concurrent; using Microsoft.Extensions.DependencyInjection; using redb.Route.Abstractions; namespace redb.Route.Core; /// /// Default implementation of . /// Provides a client API for sending messages to endpoints outside of route pipelines. /// Must be started via before use. /// Thread-safe: all public methods check started state, endpoint resolution is delegated /// to which uses concurrent collections. /// Caches producers per endpoint for performance (avoids CreateProducer on every call). /// public class ProducerTemplate : IProducerTemplate, IDisposable, IAsyncDisposable { private readonly ConcurrentDictionary _producerCache = new(StringComparer.OrdinalIgnoreCase); private volatile bool _started; private volatile bool _disposed; // Captured once at construction; null when context has no DI provider (e.g. unit-test routes). // When non-null, every Exchange we create owns a fresh DI scope so per-request scoped // services (IRedbService and its NpgsqlConnection, OpenIddict stores, ...) are isolated // between concurrent ProducerTemplate calls. Each Send/RequestBody method below MUST // dispose the exchange in finally so the scope (and connection) is released promptly. private readonly IServiceScopeFactory? _scopeFactory; /// /// Initializes a new instance with the specified route context. /// /// The route context for endpoint resolution. /// Thrown when is null. public ProducerTemplate(IRouteContext context) { Context = context ?? throw new ArgumentNullException(nameof(context)); _scopeFactory = context.GetServiceProvider()?.GetService(); } /// public IRouteContext Context { get; } /// public bool IsStarted => _started; // ── Synchronous Send ── /// public void Send(string endpointUri, object body) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); Send(endpoint, body); } /// public void Send(IEndpoint endpoint, object body) { EnsureStarted(); var exchange = CreateExchange(body); try { Send(endpoint, exchange); } finally { exchange.DisposeAsync().AsTask().GetAwaiter().GetResult(); } } /// public void Send(IEndpoint endpoint, IExchange exchange) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); ArgumentNullException.ThrowIfNull(exchange); var producer = GetOrCreateProducer(endpoint); ProcessCounted(endpoint, producer, exchange).GetAwaiter().GetResult(); } /// public void Send(string endpointUri, IExchange exchange) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); Send(endpoint, exchange); } /// public void Send(IEndpoint endpoint, IMessage message) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); ArgumentNullException.ThrowIfNull(message); var exchange = Exchange.Create(message, _scopeFactory); try { Send(endpoint, exchange); } finally { exchange.DisposeAsync().AsTask().GetAwaiter().GetResult(); } } /// public void Send(string endpointUri, IMessage message) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); Send(endpoint, message); } /// public void Send(IEndpoint endpoint, IProcessor processor) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); ArgumentNullException.ThrowIfNull(processor); var exchange = CreateExchange(null); try { processor.Process(exchange).GetAwaiter().GetResult(); var producer = GetOrCreateProducer(endpoint); ProcessCounted(endpoint, producer, exchange).GetAwaiter().GetResult(); } finally { exchange.DisposeAsync().AsTask().GetAwaiter().GetResult(); } } // ── Async Send ── /// public async Task SendAsync(IEndpoint endpoint, object body) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); var exchange = CreateExchange(body); try { var producer = GetOrCreateProducer(endpoint); // ConnectableProducer-based transports demand Start() before Process(); // idempotent — the started flag short-circuits subsequent calls. await producer.Start().ConfigureAwait(false); await ProcessCounted(endpoint, producer, exchange).ConfigureAwait(false); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// public async Task SendAsync(string endpointUri, object body) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); await SendAsync(endpoint, body).ConfigureAwait(false); } /// public async Task SendAsync(IEndpoint endpoint, IMessage message) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); ArgumentNullException.ThrowIfNull(message); var exchange = Exchange.Create(message, _scopeFactory); try { var producer = GetOrCreateProducer(endpoint); // ConnectableProducer-based transports (http, kafka, amqp, …) // demand Start() before Process(); idempotent — second Start is // a no-op on the started flag. await producer.Start().ConfigureAwait(false); await ProcessCounted(endpoint, producer, exchange).ConfigureAwait(false); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// public async Task SendAsync(string endpointUri, IMessage message) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); await SendAsync(endpoint, message).ConfigureAwait(false); } // ── Async Send / Request of a pre-built exchange (caller-owned) ── /// public async Task SendAsync(IEndpoint endpoint, IExchange exchange, CancellationToken cancellationToken = default) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); ArgumentNullException.ThrowIfNull(exchange); // Caller-owned exchange: unlike the body/message overloads we do NOT create, clone or // dispose it, and we attach no DI scope — whatever scope the exchange carries (or lacks) // is the caller's responsibility. A snapshot cloned from a completed exchange may reference // a disposed scope; the caller must give it a live scope before replay if it needs one. var producer = GetOrCreateProducer(endpoint); await producer.Start(cancellationToken).ConfigureAwait(false); await ProcessCounted(endpoint, producer, exchange, cancellationToken).ConfigureAwait(false); } /// public async Task SendAsync(string endpointUri, IExchange exchange, CancellationToken cancellationToken = default) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); await SendAsync(endpoint, exchange, cancellationToken).ConfigureAwait(false); } /// public async Task RequestAsync(IEndpoint endpoint, IExchange exchange, CancellationToken cancellationToken = default) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); ArgumentNullException.ThrowIfNull(exchange); // Caller-owned (see SendAsync(IExchange) above): not cloned or disposed here. exchange.Pattern = ExchangePattern.InOut; var producer = GetOrCreateProducer(endpoint); await producer.Start(cancellationToken).ConfigureAwait(false); await ProcessCounted(endpoint, producer, exchange, cancellationToken).ConfigureAwait(false); return exchange; } /// public async Task RequestAsync(string endpointUri, IExchange exchange, CancellationToken cancellationToken = default) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); return await RequestAsync(endpoint, exchange, cancellationToken).ConfigureAwait(false); } // ── Request/Reply ── /// public async Task RequestBody(IEndpoint endpoint, object body, CancellationToken cancellationToken = default) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); var exchange = CreateExchange(body); try { exchange.Pattern = ExchangePattern.InOut; var producer = GetOrCreateProducer(endpoint); // ConnectableProducer-based transports demand Start() before Process(). await producer.Start(cancellationToken).ConfigureAwait(false); await ProcessCounted(endpoint, producer, exchange, cancellationToken).ConfigureAwait(false); return ReplyBody(exchange, endpoint); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// public async Task RequestBody(string endpointUri, object body, CancellationToken cancellationToken = default) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); return await RequestBody(endpoint, body, cancellationToken).ConfigureAwait(false); } /// public async Task RequestBody(IEndpoint endpoint, IMessage message, CancellationToken cancellationToken = default) { EnsureStarted(); ArgumentNullException.ThrowIfNull(endpoint); ArgumentNullException.ThrowIfNull(message); var exchange = Exchange.Create(message, _scopeFactory); try { exchange.Pattern = ExchangePattern.InOut; var producer = GetOrCreateProducer(endpoint); // ConnectableProducer-based transports demand Start() before Process(). await producer.Start(cancellationToken).ConfigureAwait(false); await ProcessCounted(endpoint, producer, exchange, cancellationToken).ConfigureAwait(false); return ReplyBody(exchange, endpoint); } finally { await exchange.DisposeAsync().ConfigureAwait(false); } } /// public async Task RequestBody(string endpointUri, IMessage message, CancellationToken cancellationToken = default) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); return await RequestBody(endpoint, message, cancellationToken).ConfigureAwait(false); } /// public async Task RequestBody(IEndpoint endpoint, object body, CancellationToken cancellationToken = default) { var result = await RequestBody(endpoint, body, cancellationToken).ConfigureAwait(false); if (result is null) return default; if (result is T typed) return typed; // A nullable target converts to its underlying type; a reply that does not convert is an error, not a default. var target = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); try { return (T)RouteDecimal.ChangeType(result, target); } catch (Exception ex) when (ex is InvalidCastException or FormatException or OverflowException) { throw new InvalidCastException( $"The reply of '{endpoint.Uri}' is a {result.GetType().Name} and does not convert to {target.Name}.", ex); } } /// /// The reply body of a request whose exchange ends when the caller returns. A body that reads from resources of the /// exchange () would be dead by then, so it is refused here, at the call, instead of /// failing at its first read. /// private static object? ReplyBody(IExchange exchange, IEndpoint endpoint) { var body = exchange.Out?.Body ?? exchange.In.Body; if (body is IExchangeBoundBody) throw new InvalidOperationException( $"The reply of '{endpoint.Uri}' is a {ReadableName(body.GetType())} that reads from resources of its exchange, and " + "RequestBody ends the exchange before it returns, so the body could not be read. Use " + "RequestAsync(endpoint, exchange), read the body, then dispose the exchange."); return body; } /// StreamedQueryResult<Order> rather than the runtime name StreamedQueryResult`1. private static string ReadableName(Type type) => type.IsGenericType ? $"{type.Name[..type.Name.IndexOf('`')]}<{string.Join(", ", type.GetGenericArguments().Select(ReadableName))}>" : type.Name; /// public async Task RequestBody(string endpointUri, object body, CancellationToken cancellationToken = default) { EnsureStarted(); var endpoint = Context.GetEndpoint(endpointUri); return await RequestBody(endpoint, body, cancellationToken).ConfigureAwait(false); } // ── Lifecycle ── /// public void Start() { ObjectDisposedException.ThrowIf(_disposed, this); if (_started) throw new InvalidOperationException("ProducerTemplate is already started."); _started = true; } /// /// Stops the template and the producers it created — they flush and close, as a Camel template stops its producer /// cache. A later creates new ones. /// public void Stop() { if (!_started) throw new InvalidOperationException("ProducerTemplate is not started."); _started = false; StopProducersAsync().GetAwaiter().GetResult(); } /// Stops the producers the template created (they flush and close) and releases them. public void Dispose() { if (_disposed) return; _disposed = true; _started = false; GC.SuppressFinalize(this); StopProducersAsync().GetAwaiter().GetResult(); } /// Stops the producers the template created (they flush and close) and releases them. public async ValueTask DisposeAsync() { if (_disposed) return; _disposed = true; _started = false; GC.SuppressFinalize(this); await StopProducersAsync().ConfigureAwait(false); } // A producer is on the context's shutdown list from its creation, so the host stopping flushes it even with the // template left open; whoever stops it first takes it off, so it is stopped once. private async Task StopProducersAsync() { var producers = _producerCache.Values.ToArray(); _producerCache.Clear(); var context = Context as RouteContext; List? failures = null; foreach (var producer in producers) { if (context is not null && !context.UntrackProducer(producer)) continue; // the context stopped it already try { await producer.Stop().ConfigureAwait(false); } catch (Exception ex) { (failures ??= []).Add(ex); } } if (failures is not null) throw new AggregateException("Stopping the producers of a ProducerTemplate failed.", failures); } // ── Private helpers ── private IProducer GetOrCreateProducer(IEndpoint endpoint) { var key = endpoint.Uri.NormalizedKey; if (_producerCache.TryGetValue(key, out var cached)) return cached; var created = endpoint.CreateProducerOnItsSide(); var producer = _producerCache.GetOrAdd(key, created); // Only the instance that won the race is used, so only it goes on the context's shutdown list. if (ReferenceEquals(producer, created)) (Context as RouteContext)?.TrackProducer(created); return producer; } private Exchange CreateExchange(object? body) { var message = new Message(); if (body is not null) message.Body = body; return Exchange.Create(message, _scopeFactory); } private void EnsureStarted() { ObjectDisposedException.ThrowIf(_disposed, this); if (!_started) throw new InvalidOperationException( "ProducerTemplate is not started. Call Start() before use."); } /// /// Runs the producer with the same endpoint statistics /// keeps for a routed .To(): MessagesOut, Errors, ProcessingTime. The template used to /// bypass them, so a template send was invisible unless the connector self-recorded — and a /// self-recording connector then double-counted in routes. One owner now: the core records /// pipeline statistics on both paths, connectors record only what the core cannot see /// (wire bytes, transport-level failures, producer-side inbound operations). /// private static Task ProcessCounted(IEndpoint endpoint, IProducer producer, IExchange exchange, CancellationToken ct = default) => CountedSend.Process(endpoint, producer, exchange, ct); }