Path: redb-route/redb.Route.SignalR.StackExchangeRedis/RedisBackplaneExtensions.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.StackExchangeRedis/RedisBackplaneExtensions.cs ---- using System.Net; using Microsoft.AspNetCore.SignalR.StackExchangeRedis; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using StackExchange.Redis; namespace redb.Route.SignalR; /// /// Redis backplane for the hubs of the SignalR transport. /// public static class RedisBackplaneExtensions { /// /// Scales every hub of the SignalR transport out through Redis: a broadcast (Clients.All, Group, /// User) sent on one replica reaches the clients of that hub on every replica. /// /// The Redis channels of a hub are keyed on its path. SignalR names them after the hub class, and every hub of the /// transport is the same class, so without that key two hubs on one Redis would see each other's broadcasts. A /// ChannelPrefix set in (the name of the application or the stand) comes first /// and the hub path follows it: myapp and /chat give myapp/chat. The hub path is always part /// of the prefix, so replicas of a hub meet on its channels whatever port each of them listens on. /// /// /// A hub refuses to start when another hub of this process already holds its channels on the same Redis: the same /// path on two ports with the same prefix. Give the hubs different paths or different prefixes. /// /// /// The SignalR registration options. /// Redis connection string (redis:6379,password=...). /// Further Redis options: a ChannelPrefix of the application, a connection factory. public static SignalRRegistrationOptions UseRedisBackplane(this SignalRRegistrationOptions options, string configuration, Action? configure = null) { ArgumentNullException.ThrowIfNull(options); ArgumentException.ThrowIfNullOrWhiteSpace(configuration); return options.ConfigureHubServices(Backplane(configuration, configure)); } /// /// Scales every hub of the SignalR transport out through Redis, with the connection described by /// alone (Configuration or ConnectionFactory). The channels are keyed on /// the hub path as in . /// /// The SignalR registration options. /// The Redis options. public static SignalRRegistrationOptions UseRedisBackplane(this SignalRRegistrationOptions options, Action configure) { ArgumentNullException.ThrowIfNull(options); ArgumentNullException.ThrowIfNull(configure); return options.ConfigureHubServices(Backplane(null, configure)); } /// /// The same backplane for a component built by hand (new SignalRComponent { ... }, as a Tsak module does): /// it is added to the component's , after what is there. /// See . /// /// The SignalR component. /// Redis connection string (redis:6379,password=...). /// Further Redis options: a ChannelPrefix of the application, a connection factory. public static SignalRComponent UseRedisBackplane(this SignalRComponent component, string configuration, Action? configure = null) { ArgumentNullException.ThrowIfNull(component); ArgumentException.ThrowIfNullOrWhiteSpace(configuration); return AddTo(component, Backplane(configuration, configure)); } /// /// The backplane of for a component /// built by hand. /// /// The SignalR component. /// The Redis options. public static SignalRComponent UseRedisBackplane(this SignalRComponent component, Action configure) { ArgumentNullException.ThrowIfNull(component); ArgumentNullException.ThrowIfNull(configure); return AddTo(component, Backplane(null, configure)); } private static SignalRComponent AddTo(SignalRComponent component, Action backplane) { var previous = component.HubServicesConfigurator; component.HubServicesConfigurator = previous is null ? backplane : (services, hub) => { previous(services, hub); backplane(services, hub); }; return component; } private static Action Backplane(string? configuration, Action? configure) => (services, hub) => { var signalR = services.AddSignalR(); if (configuration is not null) signalR.AddStackExchangeRedis(configuration, redis => configure?.Invoke(redis)); else signalR.AddStackExchangeRedis(configure!); // After every Configure, so the prefix of the application is known and the hub path is added to it. services.PostConfigure(redis => redis.Configuration.ChannelPrefix = RedisBackplaneChannels.Prefix(redis.Configuration.ChannelPrefix, hub.Path)); services.AddSingleton(sp => new RedisBackplaneClaim(hub, sp.GetRequiredService>().Value, sp.GetService() ?? RedisBackplaneClaims.Process)); }; } /// The channel prefix of a hub on the Redis backplane. internal static class RedisBackplaneChannels { /// The prefix of the application, if any, followed by the hub path. public static RedisChannel Prefix(RedisChannel application, string hubPath) => RedisChannel.Literal(application.IsNullOrEmpty ? hubPath : application.ToString() + hubPath); /// The Redis a backplane publishes to: its endpoints, in a stable order. public static string Server(RedisOptions redis) => string.Join(",", redis.Configuration.EndPoints.Select(Format).Order(StringComparer.OrdinalIgnoreCase)); private static string Format(EndPoint endpoint) => endpoint switch { DnsEndPoint dns => $"{dns.Host}:{dns.Port}", _ => endpoint.ToString() ?? "", }; } /// /// The channels the hubs of one process hold on their Redis servers. A process has one; a test that plays several /// replicas in one process gives each hub container its own. /// internal sealed class RedisBackplaneClaims { /// The claims of this process. public static RedisBackplaneClaims Process { get; } = new(); private readonly Dictionary _holders = new(StringComparer.Ordinal); /// Claims for ; the hub that already holds it, if any. public SignalRHubDescriptor? TryClaim(string key, SignalRHubDescriptor hub) { lock (_holders) { if (_holders.TryGetValue(key, out var holder)) return holder; _holders[key] = hub; return null; } } /// Releases when holds it. public void Release(string key, SignalRHubDescriptor hub) { lock (_holders) { if (_holders.TryGetValue(key, out var holder) && ReferenceEquals(holder, hub)) _holders.Remove(key); } } } /// /// Holds the channels of one hub on one Redis for as long as the hub runs, and refuses a second hub of this process /// that would publish on the same ones: the two would see each other's broadcasts. Started and stopped with the hub /// as a hosted service of its container. /// internal sealed class RedisBackplaneClaim(SignalRHubDescriptor hub, RedisOptions redis, RedisBackplaneClaims claims) : IHostedService { private readonly string _key = RedisBackplaneChannels.Server(redis) + "|" + redis.Configuration.ChannelPrefix; public Task StartAsync(CancellationToken cancellationToken) { if (claims.TryClaim(_key, hub) is { } holder) throw new InvalidOperationException( $"SignalR hub {hub.Path} on {hub.Host}:{hub.Port} cannot use the Redis backplane channels " + $"'{redis.Configuration.ChannelPrefix}' on {Describe(redis)}: the hub {holder.Path} on " + $"{holder.Host}:{holder.Port} of this process already publishes on them, and the two hubs would " + "see each other's broadcasts. Give the hubs different paths, or different ChannelPrefix values " + "in UseRedisBackplane."); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { claims.Release(_key, hub); return Task.CompletedTask; } private static string Describe(RedisOptions redis) => RedisBackplaneChannels.Server(redis) is { Length: > 0 } server ? server : "the Redis of the connection factory"; }