Path: redb-route/redb.Route/Extensions/RouteContextExtensions.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/Extensions/RouteContextExtensions.cs ---- using System.Reflection; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Components; namespace redb.Route.Extensions; /// /// Extension methods for — component auto-discovery and convenience helpers. /// public static class RouteContextExtensions { /// /// Resolves a service the context was given, looking in both places it can live: the services /// set on the context itself (AddService, or a constructor argument), then the DI /// container behind . Returns null when /// neither holds one — the caller decides whether that is a default, its own instance, or a /// refusal. /// /// This is the shape a component needs when the host builds it by scanning: nothing can be /// injected through its constructor, so the one shared instance is looked up instead. Having it /// in one place keeps the two sources in one order everywhere — the metrics subscriber, the /// template options and the shared HTTP host all resolve through it. /// /// /// Service type. /// Route context; a null context resolves to null. public static T? Resolve(this IRouteContext? context) where T : class => context?.GetService() ?? context?.GetServiceProvider()?.GetService(typeof(T)) as T; /// /// The stream cache options in force for : a registered /// service, else the engine's /// , else the defaults. The .StreamCaching() step /// resolves them the same way; a connector that spools through takes them from here. /// /// Route context; null gives the defaults. public static Configuration.StreamCacheOptions GetStreamCacheOptions(this IRouteContext? context) => context?.GetService() ?? (context as Core.RouteContext)?.StreamCacheOptions ?? new Configuration.StreamCacheOptions(); /// /// The attachment options in force for : the engine's /// , the defaults without a context. There is no second /// source (no separately registered service), so RedbRoute:Attachments:MaxReplayableBytes means the same for /// the checkpoint and for the Tsak DLQ. /// /// Route context; null gives the defaults. public static Configuration.AttachmentOptions GetAttachmentOptions(this IRouteContext? context) => (context as Core.RouteContext)?.AttachmentOptions ?? new Configuration.AttachmentOptions(); /// /// Resolves a named object from the context registry and FAILS LOUD when nothing is /// registered under the name. A typo in connectionFactory=… must never silently /// fall back to URI parameters or defaults — a working configuration and a /// misconfiguration must be distinguishable. A null context (no /// registry at all) fails the same way. /// /// Expected registered type. /// Route context (may be null — still a loud failure). /// Registry name the endpoint referenced. public static T GetRequiredFromRegistry(this IRouteContext? context, string name) where T : class { ArgumentException.ThrowIfNullOrWhiteSpace(name); return context?.GetFromRegistry(name) ?? throw new InvalidOperationException( $"'{name}' is not registered in the context registry (expected {typeof(T).Name}). " + "Register it with AddToRegistry(name, ...) before Start()."); } /// /// Scans the specified assemblies for concrete implementations /// and registers any that are not already present in the context. /// A component is created with a public constructor callable without arguments: none at all, or only optional ones, /// called with their defaults (the rule the XML catalog follows too). A component without one, or whose constructor /// throws, is left out with a warning naming it; register it with AddComponent. /// /// The route context to register components in. /// One or more assemblies to scan. /// The context (for fluent chaining). public static IRouteContext AddComponents(this IRouteContext context, params Assembly[] assemblies) { ArgumentNullException.ThrowIfNull(context); var logger = context.GetService()?.CreateLogger("redb.Route.AddComponents"); foreach (var assembly in assemblies) { Type[] types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { // Some types may fail to load (missing deps); use whatever loaded successfully types = ex.Types.Where(t => t is not null).ToArray()!; } var components = ComponentConstruction.Create(types, (type, reason) => logger?.LogWarning( "AddComponents: {Type} is not registered: {Reason}; register it with AddComponent", type.FullName, reason)); foreach (var component in components) { if (context.HasComponent(component.Scheme)) continue; context.AddComponent(component); } } return context; } /// /// Registers a ${prefix.rest} resolver for the expressions of this context only ( /// ends with a dot). It lives with the context: no unregistering when the context goes, and an unloaded module's /// delegate stays nowhere. It is consulted before the process-wide resolvers of /// . /// public static IRouteContext RegisterPrefixResolver(this IRouteContext context, string prefix, Func resolver) { Expressions.ExpressionPrefixResolvers.For(context).Register(prefix, resolver); return context; } }