public async Task HandleAsync(CancellationToken cancellationToken) { using (var cts = CancellationTokenSource .CreateLinkedTokenSource(cancellationToken)) { if (await _connection.TryOpenAsync().ConfigureAwait(false)) { try { _keepAlive.Begin(cts.Token); _messageProcessor.Begin(cts.Token); await _messageReciver.ReceiveAsync(cts.Token) .ConfigureAwait(false); } finally { cts.Cancel(); await _connection.CloseAsync( "Session ended.", SocketCloseStatus.NormalClosure, CancellationToken.None) .ConfigureAwait(false); } } } }
public async Task HandleAsync(CancellationToken cancellationToken) { using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); if (await _connection.TryOpenAsync()) { try { _keepAlive.Begin(cts.Token); _messageProcessor.Begin(cts.Token); await _messageReceiver.ReceiveAsync(cts.Token); } catch (OperationCanceledException) { // OperationCanceledException are catched and will not // bubble further. We will just close the current subscription // context. } finally { cts.Cancel(); await _connection.CloseAsync( "Session ended.", SocketCloseStatus.NormalClosure, CancellationToken.None); } } }