WPF客户端调用SignalR HubConnection StopAsync抛出ObjectDisposedException
WPF客户端SignalR关闭连接异常问题
场景说明
我们有一个WPF客户端通过SignalR连接到服务端,关闭WPF客户端时会同步关闭SignalR HubConnection,调用StopAsync时会抛出两个ObjectDisposedException,该问题疑似和Visual Studio异常捕获设置有关。当服务端部署在同一台机器的IIS上运行时,调用StopAsync还会因超时报SocketException,疑似和上述ObjectDisposedException问题相关。目前本地Visual Studio运行的服务端访问地址为http://localhost:5008/tubahub。
相关实现代码
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) { // 连接已断开 if (_disconnecting) { // 断开是客户端主动发起的 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; } }
异常堆栈信息
第一个ObjectDisposedException堆栈
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, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 928 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, 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
第二个ObjectDisposedException堆栈
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, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 980 C# mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 928 C# mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, 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, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 980 C# mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, 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#
内容的提问来源于stack exchange,提问作者Marco
相关产品推荐
相关产品推荐

