diff --git a/CoreRemoting/RemotingSession.cs b/CoreRemoting/RemotingSession.cs index 43e181f..dacbc2a 100644 --- a/CoreRemoting/RemotingSession.cs +++ b/CoreRemoting/RemotingSession.cs @@ -42,7 +42,7 @@ public sealed class RemotingSession : IDisposable /// Event: Fired before the session is disposed to do some clean up. /// public event Action BeforeDispose; - + #endregion #region Construction @@ -70,7 +70,7 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se _delegateProxyCache = new ConcurrentDictionary(); _rawMessageTransport = rawMessageTransport ?? throw new ArgumentNullException(nameof(rawMessageTransport)); _clientPublicKeyBlob = clientPublicKey; - + _rawMessageTransport.ReceiveMessage += OnReceiveMessage; _rawMessageTransport.ErrorOccured += OnErrorOccured; @@ -122,6 +122,7 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se _remoteDelegateInvocationEventAggregator.RemoteDelegateInvocationNeeded += (_, uniqueCallKey, handlerKey, arguments) => { + // handle graceful client disconnection if (_isDisposing) return null; @@ -147,9 +148,19 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se keyPair: _keyPair, messageType: "invoke"); - // Invoke remote delegate on client - _rawMessageTransport?.SendMessage( - _server.Serializer.Serialize(remoteDelegateInvocationWebsocketMessage)); + try + { + // Invoke remote delegate on client + _rawMessageTransport?.SendMessage( + _server.Serializer.Serialize(remoteDelegateInvocationWebsocketMessage)); + } + catch (Exception ex) + { + // handle unexpected client disconnection + OnErrorOccured("Failed to dispatch the remote event. " + + $"Session: {SessionId}, Unique call key: {uniqueCallKey}, " + + $"Handler key: {handlerKey}", ex); + } return null; }; @@ -165,7 +176,7 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se private void OnErrorOccured(string errorMessage, Exception ex) { var exception = new RemotingException(errorMessage, innerEx: ex); - + ((RemotingServer)_server).OnError(exception); } @@ -708,7 +719,7 @@ private bool MapLinqExpressionArgument(Type argumentType, object argument, out o return true; } - + #endregion #region Close session