Path: redb-route/redb.Route.SignalR/Extensions/ServiceCollectionExtensions.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/Extensions/ServiceCollectionExtensions.cs ---- using Microsoft.Extensions.DependencyInjection; using redb.Route.Extensions; using redb.Route.Http; namespace redb.Route.SignalR; /// /// Extension methods for registering the SignalR component with dependency injection. /// public static class ServiceCollectionExtensions { /// /// Registers the SignalR component with the route context. The hub is served by the shared /// Kestrel host, so it can share a port with HTTP, gRPC, SOAP and AS2 routes. /// public static IServiceCollection AddRedbRouteSignalR(this IServiceCollection services) => AddRedbRouteSignalR(services, null); /// /// Registers the SignalR component and lets the host configure its hubs. /// /// This is the seam for everything the connector deliberately does not depend on: a backplane /// for scale-out, and authentication. A hub keeps its connections and groups in the memory of /// one process, so with more than one replica a backplane is required: without it a /// broadcast only reaches the clients attached to the replica that sent it. /// /// /// services.AddRedbRouteSignalR(o => /// { /// o.UseRedisBackplane("redis:6379"); // redb.Route.SignalR.StackExchangeRedis /// o.Authenticate = async ctx => await myJwtValidator.ValidateAsync( /// ctx.Request.Query["access_token"]); /// }); /// /// public static IServiceCollection AddRedbRouteSignalR( this IServiceCollection services, Action? configure) { var registration = new SignalRRegistrationOptions(); configure?.Invoke(registration); // The hub multiplexes onto the same Kestrel as HTTP/gRPC/SOAP/AS2 (idempotent call). services.AddRedbRouteHttpHosting(); services.AddSingleton(); // IRouteContextConfigurator is applied by RouteHostedService at startup -- // the correct registration hook (a lazy marker singleton never fires). services.AddRouteContextConfigurator((sp, context) => { var component = sp.GetRequiredService(); component.ServerManager = sp.GetRequiredService(); component.HubServicesConfigurator = registration.HubServices; component.Authenticate = registration.Authenticate; context.AddComponent(component); }); return services; } } /// /// Host-supplied configuration for the SignalR hubs. Everything here is a delegate on /// purpose: the connector stays free of Redis, Azure and authentication dependencies, and the /// host, which knows its own infrastructure, supplies them. /// public sealed class SignalRRegistrationOptions { internal Action? HubServices { get; private set; } /// /// Authenticates a hub connection. Returns the principal to attach to the request (its /// NameIdentifier claim becomes SignalR's UserIdentifier, which is what makes /// Clients.User(...) and the redbSignalR.UserId header work), or null to reject /// the handshake with 401 before the connection is upgraded. /// /// Browsers cannot set headers on a WebSocket handshake, so a token normally arrives in the /// access_token query parameter: read it from there and from the Authorization header. /// /// /// The principal is also put on every exchange the hub produces (ExchangePrincipal). Build its /// identity with an authentication type (new ClaimsIdentity(claims, "Bearer")): code that reads /// the principal treats an identity that is not authenticated as anonymous. /// /// public Func>? Authenticate { get; set; } /// /// Adds services to the container of each hub: a SignalR backplane, extra protocols, an /// IUserIdProvider, hosted services, and so on. Every consumer endpoint has a container of /// its own, so runs once per hub, with the hub it is for. The container /// does NOT see the services of the application hosting the route context: anything the hub needs /// must be registered here. /// /// For a Redis backplane prefer UseRedisBackplane from the /// redb.Route.SignalR.StackExchangeRedis package: it keys the channels on the hub path. A /// backplane added here by hand shares its channels between every hub of the same hub class, so /// the hubs of one listener would see each other's broadcasts; keying its channels on /// is then the host's job. /// /// public SignalRRegistrationOptions ConfigureHubServices(Action configure) { ArgumentNullException.ThrowIfNull(configure); var previous = HubServices; HubServices = previous is null ? configure : (s, hub) => { previous(s, hub); configure(s, hub); }; return this; } }