Skip to content

feat(SignalR): add Dispatch/Connection services for wasm mode - #1252

Merged
ArgoZhang merged 0 commit into
masterfrom
dev-middleware
Oct 27, 2026
Merged

ArgoZhang merged 0 commit into
masterfrom
dev-middleware

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Link issues

fixes #1254

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Enable WebAssembly applications to use shared SignalR-based dispatch and connection services while standardizing client asset loading and lifecycle management.

New Features:

  • Add WebAssembly SignalR client services for message dispatch, client heartbeats, and connection snapshot access over a shared Hub connection.

Bug Fixes:

  • Improve asset loading for multiple JavaScript components by resolving their stylesheets directly in JavaScript, including bundled MouseFollower styles.

Enhancements:

  • Strengthen SignalR connection lifecycle handling with automatic reconnect, subscription restoration, transient-failure retries, and coordinated resource cleanup.
  • Refine SVG editor and SignaturePad client-side behavior and lifecycle handling.

Build:

  • Add the BootstrapBlazor.SignalR.Client extension project and update project packaging and solution configuration.

@sourcery-ai

sourcery-ai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Adds end-to-end SignalR support for BootstrapBlazor, including a reconnecting shared client connection, generic dispatch and connection-monitoring services, server Hub handlers with opt-in authorization/configuration, and service/endpoint registration; the existing IP middleware is extracted into a dedicated handler.

Sequence diagram for SignalR dispatch and subscription

sequenceDiagram
    participant App as Blazor app
    participant Client as DefaultDispatchService
    participant Connection as SignalRConnection
    participant Hub as BootstrapBlazorHub
    participant Server as DefaultDispatchService

    App->>Client: SubscribeAsync(callback)
    Client->>Connection: SubscribeAsync(entryType, ReceiveAsync)
    Connection->>Hub: Subscribe(entryType)
    Hub-->>Connection: true
    Connection-->>Client: subscription ready
    App->>Client: DispatchAsync(entry)
    Client->>Connection: DispatchAsync(entryType, entry)
    Connection->>Hub: Dispatch(entryType, entry)
    Hub->>Server: DispatchAsync(entry)
    Hub-->>Connection: dispatch accepted
    Connection-->>Client: dispatch completed
    Client-->>App: invoke local callbacks
Loading

Sequence diagram for connection heartbeat and snapshot polling

sequenceDiagram
    participant Client as DefaultConnectionService
    participant Connection as SignalRConnection
    participant Hub as BootstrapBlazorHub
    participant Handler as ConnectionHandler

    loop every 3 seconds
        Client->>Connection: InvokeAsync(GetConnectionSnapshot)
        Connection->>Hub: GetConnectionSnapshot()
        Hub->>Handler: GetSnapshotAsync()
        Handler-->>Hub: ConnectionSnapshot or null
        Hub-->>Connection: snapshot result
        Connection-->>Client: update local snapshot
    end
    Client->>Connection: InvokeAsync(Heartbeat, client)
    Connection->>Hub: Heartbeat(client)
    Hub->>Handler: ReportHeartbeatAsync(client)
    Handler-->>Hub: true or false
    Hub-->>Connection: heartbeat result
Loading

Flow diagram for SignalR reconnect subscription restoration

flowchart TD
    Disconnected[SignalR connection disconnected] --> Reconnect[Automatic reconnect]
    Reconnect --> Restored[RestoreSubscriptionsAsync]
    Restored --> Subscribe[SubscribeCoreAsync]
    Subscribe --> Connected[Subscriptions restored]
    Subscribe --> Retry[Transient failure]
    Retry --> Delay[Wait 3 seconds]
    Delay --> Subscribe
Loading

File-Level Changes

Change Details Files
Adds a shared SignalR client package and registration extension for dispatch, heartbeat, and connection snapshot functionality.
  • Introduces a reference-counted Hub connection with automatic reconnect, transient-failure retry, subscription restoration, and serialized invocations.
  • Implements generic client dispatch subscriptions with callback lifecycle management, server publish support, cancellation, and cleanup.
  • Adds periodic heartbeat reporting and polling of authorized connection snapshots.
  • Registers the client services through AddBootstrapBlazorHub with a configurable Hub path.
src/extensions/BootstrapBlazor.SignalR.Client/BootstrapBlazor.SignalR.Client.csproj
src/extensions/BootstrapBlazor.SignalR.Client/SignalRConnection.cs
src/extensions/BootstrapBlazor.SignalR.Client/DefaultDispatchService.cs
src/extensions/BootstrapBlazor.SignalR.Client/DefaultConnectionService.cs
src/extensions/BootstrapBlazor.SignalR.Client/ServiceCollectionExtensions.cs
Adds the server-side BootstrapBlazor SignalR Hub, configuration, handlers, and endpoint registration for dispatch and connection tracking.
  • Introduces BootstrapBlazorHub methods for subscribe, unsubscribe, dispatch, heartbeat, and snapshot queries.
  • Adds opt-in message-type publication/subscription configuration and optional snapshot authorization.
  • Implements server-side group routing, local callback dispatch, client-originated publishing, heartbeat validation, and connection snapshot retrieval.
  • Adds AddBootstrapBlazorHub service registration and MapBootstrapBlazorHub endpoint mapping.
src/middleware/BootstrapBlazor.Middleware/BootstrapBlazorHub.cs
src/middleware/BootstrapBlazor.Middleware/BootstrapBlazorHubOptions.cs
src/middleware/BootstrapBlazor.Middleware/DispatchHandler.cs
src/middleware/BootstrapBlazor.Middleware/ConnectionHandler.cs
src/middleware/BootstrapBlazor.Middleware/DefaultDispatchService.cs
src/middleware/BootstrapBlazor.Middleware/ServiceCollectionExtensions.cs
Refactors the existing IP middleware handler without changing its public behavior.
  • Extracts /ip.axd request processing into a dedicated handler.
  • Updates XML documentation and keeps IPv4/proxy-header handling intact.
src/middleware/BootstrapBlazor.Middleware/BootstrapBlazorApplicationBuilderExtensions.cs
src/middleware/BootstrapBlazor.Middleware/IpRequestHandler.cs
src/middleware/BootstrapBlazor.Middleware/InternalIPAddressExtensions.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#1251 Add an AddBootstrapBlazorHub extension method to register the BootstrapBlazor SignalR Hub services. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot added the enhancement New feature or request label Oct 27, 2026
@bb-auto bb-auto Bot added this to the v10.0.0 milestone Oct 27, 2026

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 9 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/middleware/BootstrapBlazor.Middleware/ServiceCollectionExtensions.cs" line_range="47" />
<code_context>
+    /// </summary>
+    /// <param name="endpoints"></param>
+    /// <param name="pattern"></param>
+    public static HubEndpointConventionBuilder MapBootstrapBlazorHub(this IEndpointRouteBuilder endpoints, string pattern = "/bb.hub")
+        => endpoints.MapHub<BootstrapBlazorHub>(pattern);
+}
</code_context>
<issue_to_address>
**Cross-site pages can hijack Hub sessions**

When the host uses cookie authentication for this Hub and a victim visits an attacker-controlled site while the Hub exposes sensitive operations, `MapBootstrapBlazorHub` adds no WebSocket origin validation. ASP.NET Core endpoint authorization alone does not prevent a browser from opening a cross-site WebSocket with the victim’s cookie, allowing attacker-controlled JavaScript to invoke Hub methods and read their responses as that victim.

Validate the WebSocket handshake Origin against an explicit allowlist for this Hub; do not rely on CORS or endpoint authorization alone to prevent cross-site WebSocket requests.
</issue_to_address>

### Comment 2
<location path="src/middleware/BootstrapBlazor.Middleware/ConnectionHandler.cs" line_range="57-72" />
<code_context>
+            return null;
+        }
+
+        if (!string.IsNullOrEmpty(options.ConnectionSnapshotAuthorizationPolicy))
+        {
+            // 如果设置了授权策略则进行授权检查
+            var authorization = services.GetRequiredService<IAuthorizationService>();
+            var result = await authorization.AuthorizeAsync(context.User ?? new ClaimsPrincipal(), null, options.ConnectionSnapshotAuthorizationPolicy);
+            if (!result.Succeeded)
+            {
+                return null;
+            }
+        }
+
+        var service = services.GetRequiredService<IConnectionService>();
+        return new ConnectionSnapshot
+        {
+            Count = service.Count,
+            Connections = service.Connections
+        };
+    }
</code_context>
<issue_to_address>
**Online client details are exposed**

When the host enables connection snapshots, leaves the snapshot authorization policy unset, and maps the Hub without `RequireAuthorization`, `GetSnapshotAsync` skips authorization when no snapshot policy is configured and returns the full connection collection. Since `MapBootstrapBlazorHub` maps the Hub without requiring authorization, anonymous callers can retrieve online client details, including IP addresses and usernames.

Require an authorization policy for snapshot queries, or require Hub endpoint authorization whenever snapshots are enabled.

Also at `src/middleware/BootstrapBlazor.Middleware/ServiceCollectionExtensions.cs:47`.
</issue_to_address>

### Comment 3
<location path="src/middleware/BootstrapBlazor.Middleware/ConnectionHandler.cs" line_range="27-28" />
<code_context>
+            return false;
+        }
+
+        if (client == null || string.IsNullOrWhiteSpace(client.Id) || client.Id.Length > 128 ||
+            !Enum.IsDefined(typeof(WebClientDeviceType), client.Device))
+        {
+            throw new HubException("Invalid connection heartbeat.");
</code_context>
<issue_to_address>
**Client IDs overwrite other records**

When two clients submit the same `ClientInfo.Id`, `ReportHeartbeatAsync` accepts any nonblank client-supplied `Id` and passes that `ClientInfo` to `AddOrUpdateAsync`; clients that reuse or deliberately choose another ID overwrite that connection record, so stored connection data is attributed to the wrong client.

Derive the stored identity from a server-controlled identifier, or reject IDs already associated with another connection or owner.
</issue_to_address>

### Comment 4
<location path="src/extensions/BootstrapBlazor.SignalR.Client/DefaultDispatchService.cs" line_range="74-93" />
<code_context>
+        try
+        {
+            var initialize = true;
+            while (true)
+            {
+                await _gate.WaitAsync(token);
+                try
+                {
+                    if (initialize)
+                    {
+                        _pendingSubscriptions.UnionWith(_subscriptions.Keys);
+                        initialize = false;
+                    }
+                    if (_connection.State != HubConnectionState.Connected)
+                    {
</code_context>
<issue_to_address>
**Subscriptions never return**

When the Hub is unavailable or returns a persistent Hub error during subscription, `SubscribeCoreAsync` wraps every `HubException` as `IOException`, `IsTransientFailure` retries it, and the subscription loop has no retry limit, so `SubscribeAsync` waits indefinitely.

Preserve permanent Hub errors as non-transient and bound retries for transient subscription failures.

Also at `src/extensions/BootstrapBlazor.SignalR.Client/SignalRConnection.cs:49`, `src/extensions/BootstrapBlazor.SignalR.Client/SignalRConnection.cs:340-342`.
</issue_to_address>

### Comment 5
<location path="src/middleware/BootstrapBlazor.Middleware/DefaultDispatchService.cs" line_range="36" />
<code_context>
+            var clients = excludedConnectionId == null
+                ? hub.Clients.Group(_entryType)
+                : hub.Clients.GroupExcept(_entryType, [excludedConnectionId]);
+            await clients.SendAsync("Receive", _entryType, JsonSerializer.SerializeToElement(entry, _serializerOptions), cancellationToken);
+        }
+
</code_context>
<issue_to_address>
**Local subscribers miss dispatches**

When the SignalR send or its backing transport fails, `DispatchAsync` awaits `clients.SendAsync` before invoking local callbacks, so a send exception skips the callback loop and local subscribers miss the entry.

Invoke local callbacks even when the SignalR send fails, then report the send failure.
</issue_to_address>

### Comment 6
<location path="src/middleware/BootstrapBlazor.Middleware/DefaultDispatchService.cs" line_range="36" />
<code_context>
+            var clients = excludedConnectionId == null
+                ? hub.Clients.Group(_entryType)
+                : hub.Clients.GroupExcept(_entryType, [excludedConnectionId]);
+            await clients.SendAsync("Receive", _entryType, JsonSerializer.SerializeToElement(entry, _serializerOptions), cancellationToken);
+        }
+
</code_context>
<issue_to_address>
**Retried dispatch is delivered twice**

When a client retries a dispatch after an ambiguous transport failure, `Dispatch` runs the publish callback again for each invocation, and this broadcast path has no idempotency check. When a client retries after the server handled the first invocation but its response was lost, remote subscribers and server-side callbacks process the entry twice.

Deduplicate dispatches by a stable entry ID before broadcasting or invoking callbacks, or document that callers must not retry ambiguous sends.
</issue_to_address>

### Comment 7
<location path="src/middleware/BootstrapBlazor.Middleware/ConnectionHandler.cs" line_range="71-72" />
<code_context>
+        var service = services.GetRequiredService<IConnectionService>();
+        return new ConnectionSnapshot
+        {
+            Count = service.Count,
+            Connections = service.Connections
+        };
+    }
</code_context>
<issue_to_address>
**Snapshot totals become inconsistent**

When a heartbeat changes the connection service between the separate property reads, `GetSnapshotAsync` reads `Count` and `Connections` separately, so the returned count can disagree with the returned collection and callers see inconsistent online-connection data.

Read the count and connections together from one atomic connection-service snapshot.
</issue_to_address>

### Comment 8
<location path="src/middleware/BootstrapBlazor.Middleware/BootstrapBlazorHubOptions.cs" line_range="66-67" />
<code_context>
+            throw new HubException("Invalid dispatch payload.");
+        }
+
+        var service = (DefaultDispatchService<TEntry>)services.GetRequiredService<IDispatchService<TEntry>>();
+        await service.DispatchAsync(entry, connectionId, cancellationToken);
+    }
+}
</code_context>
<issue_to_address>
**Custom dispatch services fail**

When an application replaces `IDispatchService<TEntry>` and enables client publishing for that entry type, `PublishAsync<TEntry>` casts the resolved `IDispatchService<TEntry>` to `DefaultDispatchService<TEntry>`, so a custom implementation causes `InvalidCastException` and the Hub publish fails.

Dispatch through the resolved `IDispatchService<TEntry>` without casting it to the default implementation.
</issue_to_address>

### Comment 9
<location path="src/extensions/BootstrapBlazor.SignalR.Client/SignalRConnection.cs" line_range="64" />
<code_context>
+    {
+        _logger = logger;
+        _connection = new HubConnectionBuilder()
+            .WithUrl(navigation.ToAbsoluteUri(hubPath))
+            .WithAutomaticReconnect(new ConnectionRetryPolicy())
+            .Build();
</code_context>
<issue_to_address>
**Bearer-authenticated hubs cannot connect**

When the mapped Hub requires a bearer token rather than ambient cookie authentication, `WithUrl` receives no `HttpConnectionOptions` configuration, so callers cannot provide an `AccessTokenProvider` through this extension; a Hub protected by bearer-token authorization rejects the connection and its dispatch, heartbeat, and snapshot features remain unavailable.

Add an overload or configuration callback that lets callers configure the SignalR `HttpConnectionOptions`, including `AccessTokenProvider`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +27 to +28
if (client == null || string.IsNullOrWhiteSpace(client.Id) || client.Id.Length > 128 ||
!Enum.IsDefined(typeof(WebClientDeviceType), client.Device))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Client IDs overwrite other records

When two clients submit the same ClientInfo.Id, ReportHeartbeatAsync accepts any nonblank client-supplied Id and passes that ClientInfo to AddOrUpdateAsync; clients that reuse or deliberately choose another ID overwrite that connection record, so stored connection data is attributed to the wrong client.

Derive the stored identity from a server-controlled identifier, or reject IDs already associated with another connection or owner.

Prompt for AI agents
In `src/middleware/BootstrapBlazor.Middleware/ConnectionHandler.cs` at lines 27-28:

**Client IDs overwrite other records**

When two clients submit the same `ClientInfo.Id`, `ReportHeartbeatAsync` accepts any nonblank client-supplied `Id` and passes that `ClientInfo` to `AddOrUpdateAsync`; clients that reuse or deliberately choose another ID overwrite that connection record, so stored connection data is attributed to the wrong client.

Derive the stored identity from a server-controlled identifier, or reject IDs already associated with another connection or owner.

bb-auto[bot]
bb-auto Bot previously approved these changes Oct 27, 2026

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@ArgoZhang ArgoZhang changed the title feat(Middleware): add AddBootstraBlazorHub extension method feat(SignalR): add Dispatch/Connection services for wasm mode Oct 27, 2026
bb-auto[bot]
bb-auto Bot previously approved these changes Oct 27, 2026

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@ArgoZhang ArgoZhang closed this Oct 27, 2026
@ArgoZhang
ArgoZhang merged commit 0e4e228 into master Oct 27, 2026
1 check passed
@ArgoZhang
ArgoZhang deleted the dev-middleware branch October 27, 2026 01:22

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(SignalR): add Dispatch/Connection services for wasm mode

1 participant