Reputation: 3064
I created a basic Blazor Server-Side chat, but OnDisconnectedAsync is never called, the connection stays alive even when the browser is closed.
The OnDisconnectAsync method is inside the ChatHub.cs:
public override async Task OnDisconnectedAsync(Exception exception)
{
Debug.WriteLine("Disconnected");
}
The same code works if Blazor WebAssembly is used instead. I can't understand if this is a bug or there is something I'm missing.
Upvotes: 0
Views: 1374
Reputation: 3064
I figured out why OnDisconnectAsync was not called, the solution was given by Steve Sanderson from the issue I opened on GitHub:
https://github.com/dotnet/aspnetcore/issues/20932#event-3245802796
The Blazor Server disposes the connection but not the HubConnection that must be manually disposed. Since Blazor doesn’t support IAsyncDisposable, the call to DisposeAsync isn't awaited and the DisposeAsync must not be awaited as well:
@implements IDisposable
... rest of component code ...
@code {
public void Dispose()
{
_hubConnection.DisposeAsync();
}
}
This isn't just for Blazor Server. Hub connections on Blazor WebAssembly must be disposed as well if it lives within a single component.
Upvotes: 1