Path: redb-route/redb.Route.SignalR/SignalRComponent.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.SignalR/SignalRComponent.cs ---- using System.Collections.Concurrent; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.SignalR; using System.Security.Claims; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Extensions; using redb.Route.Core; using redb.Route.Http; namespace redb.Route.SignalR; /// /// SignalR component. Scheme: "signalr". /// Consumer: Kestrel-based SignalR Hub server — accepts connections and receives method invocations. /// Producer (client): HubConnection — connects to a remote SignalR hub and invokes methods. /// Producer (server): IHubContext — broadcasts to clients of the local hub. /// URI format: signalr:host:port/hubPath?method=Send&inOut=true&messagePack=true /// public class SignalRComponent : ComponentBase { private readonly ConcurrentDictionary _consumers = new(StringComparer.OrdinalIgnoreCase); private SharedHttpServerManager? _ownedServerManager; /// public override string Scheme => "signalr"; /// /// Shared Kestrel host. Set by AddRedbRouteSignalR() so a hub multiplexes onto the same /// listener as HTTP, gRPC, SOAP and AS2 — that is what lets /hub/ and the REST API live /// on one port behind one reverse proxy. A hand-built component owns a private manager instead /// of failing, so new SignalRComponent() stays usable on its own. /// public SharedHttpServerManager? ServerManager { get; set; } /// /// The manager actually used. Resolution order: explicitly assigned → the context's DI singleton /// → a private instance. The DI step matters for module hosts that add the component by scanning /// rather than through the extension method, so nothing ever assigns /// while the one shared manager sits in the container (gRPC and AS2 resolve the same way). /// internal SharedHttpServerManager EffectiveServerManager => ServerManager ?? Context.Resolve() ?? (_ownedServerManager ??= new SharedHttpServerManager()); /// /// Extra service registrations for the container of each hub, supplied by the host: a SignalR /// backplane for scale-out, extra protocols, anything else the hub needs. Runs once per hub, with /// the hub it is for. Kept as a delegate on purpose: the connector stays free of those /// dependencies and the host picks its own. /// public Action? HubServicesConfigurator { get; set; } /// /// Authenticates hub connections. Without it the hub accepts anonymous connections and /// UserIdentifier stays empty, which makes Clients.User(...) useless. Supplied by /// the host (see AddRedbRouteSignalR(o => o.Authenticate = ...)) rather than built in, /// because token validation belongs to whoever issues the tokens. The principal is put on every /// exchange the hub produces (ExchangePrincipal); build its identity with an authentication /// type, because code that reads it treats an identity that is not authenticated as anonymous. /// public Func>? Authenticate { get; set; } /// public override async ValueTask DisposeAsync() { if (_ownedServerManager is not null) { await _ownedServerManager.DisposeAsync().ConfigureAwait(false); _ownedServerManager = null; } await base.DisposeAsync().ConfigureAwait(false); } /// public override IEndpoint CreateEndpoint(EndpointUri uri) { ArgumentNullException.ThrowIfNull(uri); var options = new SignalREndpointOptions(); ParseHostPort(uri.Path, options); options.BindFromUri(uri.RawParameters); // Named ConnectionFactory keeps the access token / cert password out of the route URI. if (!string.IsNullOrEmpty(options.ConnectionFactory)) { // A set-but-unknown name fails loud -- never a silent fallback to URI params. var factory = Context.GetRequiredFromRegistry(options.ConnectionFactory); factory.ApplyTo(options, uri); } options.Validate(); return new SignalREndpoint(uri, this, options); } /// /// Registers a running consumer for the bridge hub and for server-mode producer lookup. /// /// The lookup key is the listener address plus the hub path, NOT the endpoint's normalized /// key: that one carries the sorted query parameters, and a server-mode producer always /// carries at least mode=server, so it never matched the consumer it was meant to push /// through. /// /// internal void RegisterConsumer(SignalRConsumer consumer) => _consumers[ConsumerKey(consumer.EndpointOptions.Host, consumer.EndpointOptions.Port, consumer.HubPath)] = consumer; /// Unregisters a consumer. internal void UnregisterConsumer(SignalRConsumer consumer) => _consumers.TryRemove(ConsumerKey(consumer.EndpointOptions.Host, consumer.EndpointOptions.Port, consumer.HubPath), out _); internal static string ConsumerKey(string host, int port, string hubPath) => $"{host}:{port}{hubPath}"; /// Gets the running consumer serving a hub, by listener address and hub path. internal SignalRConsumer? GetConsumer(string host, int port, string hubPath) => _consumers.TryGetValue(ConsumerKey(host, port, hubPath), out var c) ? c : null; /// /// Parses "host:port/hubPath" from the URI path segment. /// Sets Host and Port on options. HubPath is extracted separately via . /// internal static void ParseHostPort(string path, SignalREndpointOptions options) { var clean = path.TrimStart('/'); var slashIdx = clean.IndexOf('/'); var hostPort = slashIdx >= 0 ? clean[..slashIdx] : clean; var colonIdx = hostPort.LastIndexOf(':'); if (colonIdx > 0 && int.TryParse(hostPort[(colonIdx + 1)..], out var port)) { options.Host = hostPort[..colonIdx]; options.Port = port; } else { options.Host = hostPort; } } /// /// Extracts the hub path after host:port. /// "0.0.0.0:5000/chatHub" → "/chatHub" /// "0.0.0.0:5000" → "/" /// internal static string ExtractHubPath(string uriPath) { var clean = uriPath.TrimStart('/'); var slashIdx = clean.IndexOf('/'); return slashIdx >= 0 ? clean[slashIdx..] : "/"; } } /// /// SignalR endpoint. Creates either a producer or consumer for the SignalR hub. /// public class SignalREndpoint : EndpointBase { /// Creates a SignalR endpoint. public SignalREndpoint(EndpointUri uri, SignalRComponent component, SignalREndpointOptions options) : base(uri, component, options) { } /// The hub path (after host:port in the URI). public string HubPath => SignalRComponent.ExtractHubPath(Uri.Path); /// The shared host this endpoint serves on. internal SharedHttpServerManager ServerManager => ((SignalRComponent)Component).EffectiveServerManager; /// /// Registers the hub with the shared listener, when this endpoint serves one: a consumer was created on it (the /// route context creates every consumer while it compiles the routes, before it starts any endpoint). A producer /// endpoint (mode=server broadcast, mode=client connection) maps nothing: the server-mode producer /// sends through the hub its consumer registered, and a second registration of one hub path would map it twice. /// public override async Task Start(CancellationToken ct = default) { if (Volatile.Read(ref _servesHub)) await EnsureHubRegisteredAsync(ct).ConfigureAwait(false); } // Set by CreateConsumer: this endpoint is a hub, not a producer that talks to one. private bool _servesHub; // Serializes putting the hub on the listener and taking it off; _hub itself is read without it. private readonly SemaphoreSlim _hubGate = new(1, 1); private SignalRHubHost? _hub; /// /// The hub's own container while the hub is on the listener: the server-mode producer resolves /// IHubContext from here. /// internal IServiceProvider? HubServices { get { return Volatile.Read(ref _hub)?.Services; } } /// The consumer this hub dispatches to, while it runs. internal SignalRConsumer? ServingConsumer => ((SignalRComponent)Component).GetConsumer(Options.Host, Options.Port, HubPath); /// /// Puts the hub on the listener exactly once per run, with a container of its own (see /// ), whether the listener is already serving or not. Called from /// when the route context owns the lifecycle, and again from the consumer so a /// hand-built consumer (tests, embedded use) works without anyone having started the endpoint first. /// internal async Task EnsureHubRegisteredAsync(CancellationToken ct) { await _hubGate.WaitAsync(ct).ConfigureAwait(false); try { if (_hub is null) Volatile.Write(ref _hub, await SignalRHubHost.StartAsync(this, ct).ConfigureAwait(false)); } finally { _hubGate.Release(); } } /// /// Takes the hub off the listener when the consumer stops: its endpoints go, its connections are /// closed and its container is disposed, while the other hubs and routes on the listener keep /// serving. The listener can then shut down once nothing occupies it, and a later Start puts the /// hub back instead of finding a listener that no longer exists. /// internal async Task ReleaseHubRegistrationAsync() { await _hubGate.WaitAsync().ConfigureAwait(false); try { var hub = Interlocked.Exchange(ref _hub, null); if (hub is not null) await hub.DisposeAsync().ConfigureAwait(false); } finally { _hubGate.Release(); } } /// The endpoint options for external access. internal SignalREndpointOptions EndpointOptions => Options; /// /// Builds the full URL for producer client connections. /// "signalr:api.example.com:5000/chatHub" → "http://api.example.com:5000/chatHub" /// public string BuildClientUrl() { var scheme = Options.Ssl ? "https" : "http"; var host = Options.Host; var hubPath = HubPath; return $"{scheme}://{host}:{Options.Port}{hubPath}"; } /// public override IProducer CreateProducer() { return new SignalRProducer(this, Options); } /// public override IConsumer CreateConsumer(IProcessor processor) { ArgumentNullException.ThrowIfNull(processor); Volatile.Write(ref _servesHub, true); return new SignalRConsumer(this, processor, Options); } }