System.Net.WebSockets.ManagedWebSocket.CloseWithReceiveErrorAndThrowAsync C# (CSharp) Method

CloseWithReceiveErrorAndThrowAsync() private method

Send a close message to the server and throw an exception, in response to getting bad data from the server.
private CloseWithReceiveErrorAndThrowAsync ( WebSocketCloseStatus closeStatus, WebSocketError error, CancellationToken cancellationToken, Exception innerException = null ) : Task
closeStatus WebSocketCloseStatus The close status code to use.
error WebSocketError The error reason.
cancellationToken System.Threading.CancellationToken The CancellationToken used to cancel the websocket.
innerException Exception An optional inner exception to include in the thrown exception.
return Task
        private async Task CloseWithReceiveErrorAndThrowAsync(
            WebSocketCloseStatus closeStatus, WebSocketError error, CancellationToken cancellationToken, Exception innerException = null)
        {
            // Close the connection if it hasn't already been closed
            if (!_sentCloseFrame)
            {
                await CloseOutputAsync(closeStatus, string.Empty, cancellationToken).ConfigureAwait(false);
            }

            // Dump our receive buffer; we're in a bad state to do any further processing
            _receiveBufferCount = 0;

            // Let the caller know we've failed
            throw new WebSocketException(error, innerException);
        }