Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asp.Net Core SignalR on Blazor Server-Side never calls OnDisconnectedAsync #20932

Closed
francescocristallo opened this issue Apr 17, 2020 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components area-signalr Includes: SignalR clients and servers Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@francescocristallo
Copy link

Describe the bug

OnDisconnectedAsync is never called when using Asp.net Core SignalR with Blazor Server-Side

To Reproduce

https://github.com/francescocristallo/BlazorAppServerChat

Launch the app, open two tabs, the chat works.
When closing one of the browsers/tab, OnDisconnectedAsync is not called.

The same code calls OnDisconnectedAsync if used with Blazor Webassembly (Client Side)

Further technical details

ASP.NET Core 3.1 on Visual Studio Professional 2019

.NET Core SDK (reflecting any global.json):
Version: 3.1.200-preview-014883
Commit: 4e2a0ee959

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64

Host (useful for support):
Version: 3.1.1
Commit: a1388f194c

@francescocristallo francescocristallo changed the title Asp.Net Core SignalR on Blazor Server-Side never call OnDisconnectedAsync Asp.Net Core SignalR on Blazor Server-Side never calls OnDisconnectedAsync Apr 17, 2020
@javiercn javiercn added area-signalr Includes: SignalR clients and servers area-blazor Includes: Blazor, Razor Components Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together labels Apr 17, 2020
@SteveSandersonMS
Copy link
Member

Hub connections are disposable, and you do need to dispose them when you're finished with them. Blazor components get disposed when the user navigates away or terminates their connection. So, to avoid problems you should add code to your Index.razor component similar to the following:

@implements IDisposable

... rest of component code ...

@code {
    public void Dispose()
    {
        _hubConnection.DisposeAsync();
    }
}

As you can see, one oddity with this is that currently Blazor doesn’t support IAsyncDisposable, so the call to DisposeAsync isn't awaited. Adding IAsyncDisposable is tracked in #9960

Note that this isn't just for Blazor Server. You also need to dispose your hub connection on Blazor WebAssembly if it lives within a single component like yours does. Otherwise if the user keeps navigating back and forth between Index.razor and other components, you'll still be accumulating undisposed hub connections.

@SteveSandersonMS SteveSandersonMS added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Apr 17, 2020
@ghost ghost added the Status: Resolved label Apr 17, 2020
@ghost ghost locked as resolved and limited conversation to collaborators May 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components area-signalr Includes: SignalR clients and servers Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants