Path: redb-route/redb.Route.SignalR/Fluent/SignalR.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/Fluent/SignalR.cs ---- using System.Text; using redb.Route.Abstractions; namespace redb.Route.SignalR; /// /// Fluent entry point for SignalR endpoints. /// /// // Consumer — listen for SignalR connections (hub server): /// .From(SignalR.Hub("0.0.0.0:5000/chatHub").InOut().MessagePack()) /// /// // Producer (client) — connect to a remote SignalR hub: /// .To(SignalR.Connect("api.example.com:5000/chatHub").Method("Send").Ssl()) /// /// // Producer (server) — broadcast to connected clients via local hub: /// .To(SignalR.Broadcast("0.0.0.0:5000/chatHub").Method("Notify").Group("room1")) /// /// public static class SignalR { /// Listen for SignalR connections — start a hub server (consumer). /// Bind address, e.g. 0.0.0.0:5000/chatHub. public static SignalRBuilder Hub(string hostPortPath) => new(hostPortPath); /// Connect to a remote SignalR hub (producer, client mode). /// Target address, e.g. api.example.com:5000/chatHub. public static SignalRBuilder Connect(string hostPortPath) => new(hostPortPath, SignalRMode.Client); /// Broadcast to clients of a local hub (producer, server mode). /// Hub address matching the consumer, e.g. 0.0.0.0:5000/chatHub. public static SignalRBuilder Broadcast(string hostPortPath) => new(hostPortPath, SignalRMode.Server); } /// Fluent builder for SignalR endpoint URIs. Scheme: signalr. public sealed class SignalRBuilder { private readonly string _hostPortPath; private SignalRMode? _mode; // Common private string? _method; private bool? _inOut; // Transport private SignalRTransport? _transport; private bool? _messagePack; private int? _maxConnections; private int? _maxParallelInvocationsPerClient; // Groups / targeting private string? _defaultGroup; private string? _targetType; private string? _targetGroup; // TLS private bool _ssl; private string? _sslCertPath; private string? _sslCertPassword; private bool _trustAllCertificates; // Client: reconnect private bool _reconnect; private int? _reconnectInterval; private int? _maxReconnectAttempts; // Client: auth private string? _accessToken; // Client: bridge mode (default true = route through RedbBridgeHub) private bool _bridge = true; internal SignalRBuilder(string hostPortPath, SignalRMode? mode = null) { ArgumentException.ThrowIfNullOrWhiteSpace(hostPortPath); _hostPortPath = hostPortPath; _mode = mode; } // ── Common ────────────────────────────────────────────────────── /// Hub method name to invoke/listen on. public SignalRBuilder Method(string method) { _method = method; return this; } /// Hub method name from an expression. public SignalRBuilder Method(IExpression expr) { _method = expr.ToTemplateString(); return this; } /// Enable request-response (InOut) exchange pattern. public SignalRBuilder InOut(bool value = true) { _inOut = value; return this; } // ── Transport ─────────────────────────────────────────────────── /// Force specific transport type. public SignalRBuilder Transport(SignalRTransport transport) { _transport = transport; return this; } /// Enable MessagePack protocol. public SignalRBuilder MessagePack(bool value = true) { _messagePack = value; return this; } /// Maximum hub connections (server mode); a connection over the limit is aborted at OnConnected. 0 = unlimited. public SignalRBuilder MaxConnections(int max) { _maxConnections = max; return this; } /// SignalR MaximumParallelInvocationsPerClient; 0 = SignalR default (per-client serial). public SignalRBuilder MaxParallelInvocationsPerClient(int max) { _maxParallelInvocationsPerClient = max; return this; } // ── Groups / targeting ────────────────────────────────────────── /// Default group for auto-join on connect (consumer). public SignalRBuilder DefaultGroup(string group) { _defaultGroup = group; return this; } /// Target type for server-mode broadcast: all, group, user, connection. public SignalRBuilder Target(string targetType) { _targetType = targetType; return this; } /// Target type from an expression. public SignalRBuilder Target(IExpression expr) { _targetType = expr.ToTemplateString(); return this; } /// Target group name for server-mode broadcast. public SignalRBuilder Group(string group) { _targetType = "group"; _targetGroup = group; return this; } /// Target group name from an expression. public SignalRBuilder Group(IExpression expr) { _targetType = "group"; _targetGroup = expr.ToTemplateString(); return this; } // ── TLS ───────────────────────────────────────────────────────── /// Enable SSL/TLS. public SignalRBuilder Ssl() { _ssl = true; return this; } /// Path to SSL certificate. public SignalRBuilder SslCertPath(string path) { _sslCertPath = path; return this; } /// SSL certificate password. public SignalRBuilder SslCertPassword(string password) { _sslCertPassword = password; return this; } /// /// Producer: accept any server certificate, including a self-signed one. Explicit on purpose -- /// this used to switch itself on whenever ssl was NOT set. /// public SignalRBuilder TrustAllCertificates() { _trustAllCertificates = true; return this; } // ── Client reconnect ──────────────────────────────────────────── /// Enable auto-reconnect (client mode). public SignalRBuilder Reconnect(int intervalMs = 5000, int maxAttempts = 0) { _reconnect = true; _reconnectInterval = intervalMs; _maxReconnectAttempts = maxAttempts; return this; } /// Access token for authentication (client mode). public SignalRBuilder AccessToken(string token) { _accessToken = token; return this; } /// /// Direct mode — call external hub methods by name instead of routing through the bridge. /// Equivalent to bridge=false. Use when connecting to third-party SignalR hubs. /// public SignalRBuilder Direct() { _bridge = false; return this; } // ── Build ─────────────────────────────────────────────────────── /// Builds the endpoint URI string. public string Build() { var sb = new StringBuilder(); sb.Append("signalr:"); sb.Append(_hostPortPath); var sep = '?'; void Append(string key, string v) { sb.Append(sep); sb.Append(key); sb.Append('='); sb.Append(Uri.EscapeDataString(v)); sep = '&'; } void AppendStr(string key, string? v) { if (v is not null) Append(key, v); } void AppendInt(string key, int? v) { if (v.HasValue) Append(key, v.Value.ToString()); } void AppendBool(string key, bool v) { if (v) Append(key, "true"); } void AppendBoolN(string key, bool? v) { if (v.HasValue) Append(key, v.Value.ToString().ToLowerInvariant()); } if (_mode is not null) Append("mode", _mode.Value.ToString().ToLowerInvariant()); AppendStr("method", _method); AppendBoolN("inOut", _inOut); if (_transport is not null) Append("transport", _transport.Value.ToString()); AppendBoolN("messagePack", _messagePack); AppendStr("defaultGroup", _defaultGroup); AppendStr("targetType", _targetType); AppendStr("targetGroup", _targetGroup); AppendBool("ssl", _ssl); AppendStr("sslCertPath", _sslCertPath); AppendStr("sslCertPassword", _sslCertPassword); AppendBool("trustAllCertificates", _trustAllCertificates); AppendBool("reconnect", _reconnect); AppendInt("reconnectInterval", _reconnectInterval); AppendInt("maxReconnectAttempts", _maxReconnectAttempts); AppendStr("accessToken", _accessToken); AppendInt("maxConnections", _maxConnections); AppendInt("maxParallelInvocationsPerClient", _maxParallelInvocationsPerClient); if (!_bridge) Append("bridge", "false"); return sb.ToString(); } /// Implicit conversion to string URI. public static implicit operator string(SignalRBuilder b) => b.Build(); /// public override string ToString() => Build(); }