Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,36 @@ public override async Task OnConnectedAsync(HubConnectionContext connection)

public override async Task OnDisconnectedAsync(HubConnectionContext connection, Exception? exception)
{
await using var scope = _serviceScopeFactory.CreateAsyncScope();
AsyncServiceScope scope;
try
{
scope = _serviceScopeFactory.CreateAsyncScope();
}
catch (ObjectDisposedException)
{
// The service provider was disposed during shutdown. We can't resolve the hub
// or run OnDisconnectedAsync, so just no-op.
Log.ServiceProviderDisposedWhileDisconnecting(_logger);
return;
}

await using var _ = scope;

IHubActivator<THub> hubActivator;
THub hub;
try
{
hubActivator = scope.ServiceProvider.GetRequiredService<IHubActivator<THub>>();
hub = hubActivator.Create();
}
catch (ObjectDisposedException)
{
// The service provider can be disposed between CreateAsyncScope() and service
// resolution during shutdown.
Log.ServiceProviderDisposedWhileDisconnecting(_logger);
return;
}

var hubActivator = scope.ServiceProvider.GetRequiredService<IHubActivator<THub>>();
var hub = hubActivator.Create();
Activity? activity = null;
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@ public static void ClosingStreamWithBindingError(ILogger logger, CompletionMessa

[LoggerMessage(28, LogLevel.Trace, "Received SequenceMessage with Sequence ID '{SequenceId}'.", EventName = "ReceivedSequenceMessage")]
public static partial void ReceivedSequenceMessage(ILogger logger, long sequenceId);

[LoggerMessage(29, LogLevel.Debug, "The service provider was disposed while dispatching 'OnDisconnectedAsync'. Unable to run Hub.OnDisconnectedAsync.", EventName = "ServiceProviderDisposedWhileDisconnecting")]
public static partial void ServiceProviderDisposedWhileDisconnecting(ILogger logger);
}
Loading