如何解决SignalR HubConnection StopAsync 抛出 ObjectDisposedException
我们有一个 WPF 客户端,它通过 SignalR 连接到我们的一台服务器。关闭 WPF 客户端时,我们也会关闭 SignalR HubConnection:
public HubConnection Connection => _connection ??= GetConnection();
private HubConnection GetConnection()
{
var host = ConfigurationManager.AppSettings["SignalRHost"];
var connection = new HubConnectionBuilder()
.WithUrl(host)
.Build();
connection.Closed += ConnectionOnClosed;
return connection;
}
private async Task ConnectionOnClosed(Exception exception)
{
// The connection is disconnected
if (_disconnecting)
{
// The disconnect was requested by the client
Connection.Closed -= ConnectionOnClosed;
Logger.information("The connection to SignalR hub was closed.");
}
else
{
await StartAsync();
}
}
public async Task disconnect()
{
if (_disconnecting) return;
try
{
_disconnecting = true;
await _connection.StopAsync();
}
catch (Exception e)
{
Logger.Error(e,"Error in closing connection");
}
finally
{
await _connection.disposeAsync();
_connection = null;
}
}
调用 CloseAsync 会导致两个 ObjectdisposedException。我只有部分堆栈跟踪(这似乎是我的 VS 中的另一个问题):
System.dll!System.Net.sockets.NetworkStream.InternalSocket.get() Line 136
System.dll!System.Net.PooledStream.CancelPendingIoAndCloseIfSafe(bool closeWithTimeout,int timeout) Line 596
System.dll!System.Net.PooledStream.Close(int timeout) Line 682
System.dll!System.Net.Connection.AbortOrdisassociate(System.Net.HttpWebRequest request,System.Net.WebException webException) Line 2859
System.dll!System.Net.HttpWebRequest.Abort(System.Exception exception,int abortState) Line 3173
System.dll!System.Net.HttpWebRequest.Abort() Line 3133
System.dll!System.Net.WebSockets.ClientWebSocket.AbortRequest(object obj) Line 366
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecutionContextCallback(object obj) Line 1031
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx) Line 980
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,bool preserveSyncCtx) Line 928
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,object state) Line 917
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecuteCallback() Line 1021
mscorlib.dll!System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(System.Threading.CancellationCallbackCoreWorkArguments args) Line 863
mscorlib.dll!System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(bool throwOnFirstException) Line 808
mscorlib.dll!System.Threading.CancellationTokenSource.NotifyCancellation(bool throwOnFirstException) Line 743
System.dll!System.Net.WebSockets.ClientWebSocket.dispose() Line 377
System.dll!System.Net.WebSockets.ClientWebSocket.Abort() Line 360
Microsoft.AspNetCore.Http.Connections.Client.dll!Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport.ProcessSocketAsync(System.Net.WebSockets.WebSocket socket) Line 220
接下来:
> System.dll!System.Net.sockets.NetworkStream.InternalSocket.get() Line 136 C#
System.dll!System.Net.PooledStream.CancelPendingIoAndCloseIfSafe(bool closeWithTimeout,int timeout) Line 596 C#
System.dll!System.Net.PooledStream.Close(int timeout) Line 682 C#
System.dll!System.Net.Connection.AbortSocket(bool isAbortState) Line 2901 C#
System.dll!System.Net.ConnectStream.CloseInternal(bool internalCall,bool aborting) Line 2707 C#
System.dll!System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(System.Net.CloseExState closeState) Line 2426 C#
System.dll!System.Net.HttpWebResponse.Abort() Line 479 C#
System.dll!System.Net.HttpWebRequest.SetResponse(System.Exception E) Line 3968 C#
System.dll!System.Net.HttpWebRequest.SetAndOrProcessResponse(object responSEOrException) Line 3661 C#
System.dll!System.Net.ConnectionReturnResult.SetResponses(System.Net.ConnectionReturnResult returnResult) Line 205 C#
System.dll!System.Net.Connection.AbortOrdisassociate(System.Net.HttpWebRequest request,System.Net.WebException webException) Line 2873 C#
System.dll!System.Net.HttpWebRequest.Abort(System.Exception exception,int abortState) Line 3173 C#
System.dll!System.Net.HttpWebRequest.Abort() Line 3133 C#
System.Net.Http.dll!System.Net.Http.httpclienthandler.OnCancel(object state) Line 1210 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecutionContextCallback(object obj) Line 1031 C#
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,bool preserveSyncCtx) Line 980 C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,bool preserveSyncCtx) Line 928 C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,object state) Line 917 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecuteCallback() Line 1021 C#
mscorlib.dll!System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(System.Threading.CancellationCallbackCoreWorkArguments args) Line 863 C#
mscorlib.dll!System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(bool throwOnFirstException) Line 808 C#
mscorlib.dll!System.Threading.CancellationTokenSource.NotifyCancellation(bool throwOnFirstException) Line 743 C#
mscorlib.dll!System.Threading.CancellationTokenSource.LinkedTokenCancelDelegate(object source) Line 91 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecutionContextCallback(object obj) Line 1031 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecuteCallback() Line 1021 C#
mscorlib.dll!System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(System.Threading.CancellationCallbackCoreWorkArguments args) Line 863 C#
mscorlib.dll!System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(bool throwOnFirstException) Line 808 C#
mscorlib.dll!System.Threading.CancellationTokenSource.NotifyCancellation(bool throwOnFirstException) Line 743 C#
mscorlib.dll!System.Threading.CancellationTokenSource.TimerCallbackLogic(object obj) Line 534 C#
mscorlib.dll!System.Threading.TimerQueueTimer.CallCallbackInContext(object state) Line 730 C#
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,bool preserveSyncCtx) Line 928 C#
mscorlib.dll!System.Threading.TimerQueueTimer.CallCallback() Line 713 C#
mscorlib.dll!System.Threading.TimerQueueTimer.Fire() Line 670 C#
mscorlib.dll!System.Threading.TimerQueue.FireQueuedTimerCompletion(object state) Line 444 C#
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() Line 1252 C#
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.dispatch() Line 820 C#
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Line 1161 C#
服务器在 Visual Studio 中运行,地址为 http://localhost:5008/tubahub。在同一台机器上的 IIS 中运行服务器时,由于超时,我们在调用 StopAsync 时也会收到 SocketException。也许这是相关的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。