Path: redb-route/redb.Route/Abstractions/IRouteContext.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/Abstractions/IRouteContext.cs
----
#pragma warning disable CS0619
using redb.Route.Core;
namespace redb.Route.Abstractions;
///
/// Central runtime context for route management: endpoint cache, component registry,
/// named object registry, context-level properties, service locator, and route lifecycle.
/// Provides three-tier exception handling (local route → global → error handler fallback).
/// Thread-safe via .
///
public interface IRouteContext : IDisposable
{
// ── Identity ──
/// Unique context identifier (GUID by default).
string ContextId { get; }
/// Whether has been called and the context is running.
bool IsStarted { get; }
// ── Endpoint Management ──
/// Resolves or creates an endpoint by URI string. Caches by NormalizedKey.
/// Endpoint URI (e.g., "direct:orders", "kafka://topic?brokers=localhost").
/// Cached or newly created endpoint.
IEndpoint GetEndpoint(string uri);
/// Returns a snapshot of all currently cached endpoints.
IReadOnlyList GetEndpoints();
// ── Thread pool profiles ──
///
/// Registers a named thread pool profile that a detached step (WireTap, asynchronous OnCompletion)
/// picks with executorService. A module that builds its routes in code registers its profile here before its
/// routes are compiled. Registering a name again with the same bounds does nothing; with other bounds it throws,
/// naming both, so two modules cannot silently size one pool.
///
/// The profile's name.
/// Its bounds; the context keeps a copy.
void AddThreadPoolProfile(string name, Configuration.ThreadPoolProfile profile);
///
/// The profile a detached step builds its pool from: the named one, or the context's default when
/// is null. An unknown name throws, naming it.
///
/// The profile's name, or null for the default profile.
Configuration.ThreadPoolProfile GetThreadPoolProfile(string? name);
// ── Component Management ──
/// Registers a component for its URI scheme.
/// The component to register.
void AddComponent(IComponent component);
/// Gets a component by type, optionally filtering by scheme name.
/// Component type.
/// Optional scheme to match. If null, returns first matching type.
/// The component, or default if not found.
T? GetComponent(string? scheme = null) where T : class, IComponent;
/// Returns whether a component with the specified scheme is registered.
bool HasComponent(string scheme);
/// Returns a snapshot of all registered component scheme names.
IReadOnlyList GetComponentNames();
/// Removes a component by scheme. Returns true if removed.
bool RemoveComponent(string scheme);
// ── Properties (context-level shared state) ──
/// Gets or sets a context-level property by key. Returns null if not found.
object? this[string key] { get; set; }
/// Gets a typed property value with automatic conversion.
/// Target type.
/// Property key.
/// Converted value, or default.
T? GetProperty(string key);
/// Sets a typed property. Returns this context for chaining.
IRouteContext SetProperty(string key, T value);
// ── Named Object Registry ──
/// Stores a named object in the registry (factories, shared state, etc.).
/// Registry key.
/// Object to store.
/// This context for chaining.
IRouteContext AddToRegistry(string key, object value);
/// Retrieves a typed object from the registry.
/// Expected type.
/// Registry key. A leading # prefix (registry reference) is stripped automatically.
/// The object, or default if not found or wrong type.
T? GetFromRegistry(string key);
// ── Service Locator ──
/// Registers a service instance by type.
/// Service type key.
/// Service instance.
void AddService(Type serviceType, object service);
/// Retrieves a service by type.
/// Service type.
/// Service instance, or default if not registered.
T? GetService() where T : class;
// ── Dependency Injection ──
///
/// Stores an for DI container integration.
/// Services resolved from this provider are available to processors and components.
///
/// DI service provider.
/// This context for chaining.
IRouteContext SetServiceProvider(IServiceProvider serviceProvider);
///
/// Retrieves the registered , or null if not set.
///
IServiceProvider? GetServiceProvider();
// ── Exception Handling ──
///
/// Gets or sets the context's error handler — Apache Camel's context-level error handler: on every route, the
/// last resort for a failure no OnException took and no local or global handler matches. A handler that
/// marks the exchange handled (, as a dead letter channel does) ends the
/// failure; otherwise the failure goes on to the caller. Set to null to disable.
///
IErrorHandler? ErrorHandler { get; set; }
///
/// Checks whether an exception handler is registered (globally or locally)
/// for the specified exception type.
///
/// Exception type to check.
/// true if a handler is registered.
bool HasExceptionRoute() where TException : Exception;
///
/// Handles an exception using three-tier search order:
/// 1. Local route handler (by ).
/// 2. Global handler (registered via ).
/// 3. Fallback error handler.
/// Walks the exception type hierarchy to find the best matching handler; throws when none matches. The same
/// lookup is the outermost layer of every route's error scope, so the engine does not call this method itself —
/// it is for a caller outside a route.
///
/// The exchange that caused the exception.
/// The exception to handle.
/// Cancellation token.
Task HandleException(IExchange exchange, Exception exception, CancellationToken ct = default);
// ── Replay checkpoints ──
///
/// Re-runs the tail of a route from a captured checkpoint snapshot. Runs the compiled body of
/// the .Replayable(markerName) marker directly (no synthetic endpoint) with the supplied
/// snapshot as the exchange. Intended for the platform (Tsak) to replay a failed exchange from a
/// save-point. The snapshot is caller-owned (typically a
/// read from a failed exchange's property, or a
/// rehydrated one from durable storage).
///
/// Route the marker belongs to.
/// Name of the .Replayable marker to replay into.
/// The exchange snapshot to run the tail with.
/// Cancellation token.
/// No such marker on the route.
Task ReplayAsync(string routeId, string markerName, IExchange snapshot, CancellationToken ct = default);
///
/// Lists every compiled replay marker as (routeId, markerName). Lets the platform show
/// which routes are replayable and into which save-points, before any traffic has flowed.
///
IReadOnlyCollection<(string RouteId, string MarkerName)> GetReplayMarkers();
///
/// Registers a global exception handler for a specific exception type: on every route, for a failure of this type
/// no OnException took. The processor runs with the failure on the exchange; unless it marks the exchange
/// handled, the failure goes on to the caller.
///
/// Exception type to handle.
/// Processor to invoke when this exception type is caught.
void AddGlobalExceptionHandler(IProcessor processor) where TException : Exception;
///
/// Registers a global exception handler for the specified exception type (non-generic overload).
///
/// Exception type to handle.
/// Processor to invoke when the exception is caught.
void AddGlobalExceptionHandler(Type exceptionType, IProcessor processor);
///
/// Registers a local exception handler for a specific route and exception type: checked before the global
/// handlers, for a failure of that route no OnException took.
///
/// Route identifier.
/// Exception type to handle.
/// Processor to invoke when this exception type is caught in this route.
void AddLocalExceptionHandler(string routeId, Type exceptionType, IProcessor processor);
// ── Routes ──
/// Returns a snapshot of all compiled routes managed by this context.
IReadOnlyList Routes { get; }
// ── Lifecycle ──
/// Registers a lifecycle listener for route and context state change notifications.
/// Listener to add.
/// This context for chaining.
IRouteContext AddLifecycleListener(IRouteLifecycleListener listener);
///
/// Registers a factory that can create instances for routes
/// during compilation. When multiple factories are registered, the first non-null result wins.
///
/// Policy factory to add.
/// This context for chaining.
IRouteContext AddRoutePolicyFactory(IRoutePolicyFactory factory);
///
/// Returns the effective cluster-policy resolution for a compiled route.
///
/// Route identifier.
///
/// A describing whether .Cluster(true) was requested
/// and which policy (if any) was actually attached. Returns a descriptor with
/// = "AllNodes" and
/// = "Route not found" when the route is unknown.
///
RoutePolicyDescriptor GetRoutePolicy(string routeId);
///
/// Starts all registered endpoints and consumers. Uses degraded-mode startup:
/// logs individual endpoint failures but continues starting remaining endpoints.
///
/// Cancellation token.
Task Start(CancellationToken ct = default);
/// Stops all routes managed by this context.
/// Cancellation token.
Task Stop(CancellationToken ct = default);
/// Starts an individual route by its route ID.
/// The route identifier.
/// Cancellation token.
Task StartRoute(string routeId, CancellationToken ct = default);
/// Stops an individual route by its route ID.
/// The route identifier.
/// Cancellation token.
Task StopRoute(string routeId, CancellationToken ct = default);
/// Stops an individual route with an optional timeout override.
/// The route identifier.
/// Timeout for graceful drain. If null, uses default shutdown timeout.
/// Cancellation token.
Task StopRoute(string routeId, TimeSpan? timeout, CancellationToken ct = default);
}