Path: redb-route/redb.Route/Processors/ContextStampProcessor.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/Processors/ContextStampProcessor.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Processors;
///
/// Outermost per-route wrapper that stamps for the duration of the
/// route and restores the previous value on exit.
///
///
/// Save/set/restore rather than set-once, so "current" means the route actually executing: inside
/// a direct: or vm: sub-route the exchange sees the inner route's context — Camel's
/// reading — and the caller sees its own again when the sub-route returns. An exchange that never
/// entered a route keeps null, including after it comes back from one.
///
internal sealed class ContextStampProcessor : IProcessor
{
private readonly IProcessor _inner;
private readonly IRouteContext _context;
public ContextStampProcessor(IProcessor inner, IRouteContext context)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
_context = context ?? throw new ArgumentNullException(nameof(context));
}
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
if (exchange is not Exchange stampable)
{
await _inner.Process(exchange, ct).ConfigureAwait(false);
return;
}
var previous = stampable.Context;
stampable.Context = _context;
try
{
await _inner.Process(exchange, ct).ConfigureAwait(false);
}
finally
{
stampable.Context = previous;
}
}
}