question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SignalR connection started from Blazor component is cannibalized by main page

See original GitHub issue

Describe the bug

Let’s say I have server with single SignalR hub and two channels/stream which simply broadcast some number every second. And I have a Blazor (webasm) client with plain page and two (!) hub connections, each connects to given channel. This works and what is most important here I can see at server side each hub connection is connected with its own connection id. So from server side I see two connections.

When I change the code on client side and move one connection as component the server side will see only one connection. So one connection id was cannibalized. As effect only one connection will receive messages from the hub.

To Reproduce

Create Blazor webasm demo, add SignalR. Refer to this gist: https://gist.github.com/astrowalker/d26fc0a7a62eda94bb91d32bb79b20ca

add component to web client and put there client part of the gist. Copy this code again and paste it to the page that uses added component – this time change “channelA” to “channelB”.

Add SignalR hub to the server (server part of the gist). Run the solution (it is better to run it in project mode, not IIS). Navigate to the page you placed signalR connection and observe the server – instead of two connections it will add one new, and reuse the existing one then.

If you want to compare it to working behaviour – move code away from component and place it directly within the page at webclient (thus page will have still 2 signalr connections, but would have them directly in the body of the page). Rerun solution, observe server registers two new connections (and as effect all messages are received by the client).

Exceptions (if any)

None.

Further technical details

  • ASP.NET Core version

Both Core 3.1 and Net 5.0

  • Include the output of dotnet --info

PM> dotnet --info .NET SDK (reflecting any global.json): Version: 5.0.103 Commit: 72dec52dbd

Runtime Environment: OS Name: Windows OS Version: 10.0.19041 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.103\

Host (useful for support): Version: 5.0.3 Commit: c636bbdc8a

.NET SDKs installed: 2.1.104 [C:\Program Files\dotnet\sdk] 2.1.201 [C:\Program Files\dotnet\sdk] 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.402 [C:\Program Files\dotnet\sdk] 2.1.403 [C:\Program Files\dotnet\sdk] 2.1.504 [C:\Program Files\dotnet\sdk] 2.1.509 [C:\Program Files\dotnet\sdk] 2.1.513 [C:\Program Files\dotnet\sdk] 2.1.602 [C:\Program Files\dotnet\sdk] 2.2.104 [C:\Program Files\dotnet\sdk] 5.0.101 [C:\Program Files\dotnet\sdk] 5.0.103 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version

VS 2019 Version 16.8.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
BrennanConroycommented, Mar 1, 2021

Don’t add your hub to DI, especially not as a singleton https://github.com/astrowalker/blazor-signalr/blob/92c4a0ed74e77509702baaf129cd2e87a9fa4f37/MultiSignalBlazorApp50/Server/Startup.cs#L30

If you want to store state in the hub class, it needs to be in static properties (and make sure you think about thread safety)

Don’t access properties of the hub class from inside a background task (your timer). If you want to send to clients like that, you need to use the IHubContext<THub> https://docs.microsoft.com/aspnet/core/signalr/hubcontext?view=aspnetcore-5.0

0reactions
msftbot[bot]commented, Mar 2, 2021

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

net Core 6 Blazor SignalR connection problem
So I have written an auction site and I'm using SignalR to live update the components when bids are made, so the user...
Read more >
Blazor is NOT loosing SignalR connection. Browser is!
Those who work with Blazor are fully aware of the SignalR connection failed issue and the adorable "Reload" message. In a recent project, ......
Read more >
Blazor Client-Side: Unable to create a SignalR connection ...
I have set up a hosted Blazor Client-Side Application and have added a SignalR-Connection to my Server (Calling endpoints.MapHub("/somePath").
Read more >
ASP.NET Core Blazor SignalR guidance
This article explains how to configure and manage SignalR connections in Blazor apps. Throughout this article, the terms client/client-side ...
Read more >
Blazor - Page Loads But Code Not Running Until SignalR ...
When my Blazor page starts up, you can see the page and type in the inputs. However, none of the event handlers get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found