protected override async Task HandleAsync(
        ISocketConnection connection,
        TerminateConnectionMessage message,
        CancellationToken cancellationToken)
    {
        await connection.CloseAsync(
            "Connection terminated by user.",
            SocketCloseStatus.NormalClosure,
            cancellationToken);

        await _socketSessionInterceptor.OnCloseAsync(connection, cancellationToken);
    }
Exemplo n.º 2
0
    private async Task SendResultsAsync()
    {
        await using IResponseStream responseStream = _responseStream;
        CancellationToken cancellationToken = _sessionToken;

        try
        {
            await foreach (IQueryResult result in
                           responseStream.ReadResultsAsync().WithCancellation(cancellationToken))
            {
                using (result)
                {
                    if (!cancellationToken.IsCancellationRequested && !_connection.Closed)
                    {
                        await _connection.SendAsync(
                            new DataResultMessage(Id, result),
                            cancellationToken);
                    }
                }
            }

            if (!cancellationToken.IsCancellationRequested && !_connection.Closed)
            {
                await _connection.SendAsync(new DataCompleteMessage(Id), cancellationToken);
            }
        }
        catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested)
        {
        }
        catch (ObjectDisposedException) { }
        catch (Exception ex) when(!cancellationToken.IsCancellationRequested)
        {
            if (!_connection.Closed)
            {
                try
                {
                    try
                    {
                        await _connection.SendAsync(
                            new DataResultMessage(Id, UnknownSubscriptionError(ex)),
                            cancellationToken);
                    }
                    finally
                    {
                        await _connection.SendAsync(
                            new DataCompleteMessage(Id),
                            cancellationToken);

                        await _sessionInterceptor.OnCloseAsync(_connection, cancellationToken);
                    }
                }
                catch
                {
                    // suppress all errors, so original exception can be rethrown
                }
            }

            _diagnosticEvents.SubscriptionTransportError(Subscription, ex);
        }
        finally
        {
            // completed should be always invoked to be ensure that disposed subscription is
            // removed from subscription manager
            Completed?.Invoke(this, EventArgs.Empty);
            Dispose();
        }
    }