public async Task CloseAsync()

in src/Microsoft.Azure.Relay/HybridHttpConnection.cs [497:551]


            public async Task CloseAsync()
            {
                if (this.closed)
                {
                    return;
                }

                try
                {
                    RelayEventSource.Log.ObjectClosing(this);
                    using (var cancelSource = new CancellationTokenSource(this.WriteTimeout))
                    using (await this.asyncLock.LockAsync(cancelSource.Token).ConfigureAwait(false))
                    {
                        if (!this.responseCommandSent)
                        {
                            var responseCommand = CreateResponseCommand(this.context);
                            if (this.writeBufferStream != null)
                            {
                                responseCommand.Body = true;
                                this.writeBufferStream.Position = 0;
                            }

                            // Don't force any rendezvous now
                            await this.connection.SendResponseAsync(responseCommand, this.writeBufferStream, cancelSource.Token).ConfigureAwait(false);
                            this.responseCommandSent = true;
                            this.CancelWriteBufferFlushTimer();
                        }
                        else
                        {
                            var buffer = new ArraySegment<byte>(new byte[0], 0, 0);
                            await this.connection.SendBytesOverRendezvousAsync(buffer, WebSocketMessageType.Binary, true, cancelSource.Token).ConfigureAwait(false);
                        }
                    }

                    RelayEventSource.Log.ObjectClosed(this);
                }
                catch (Exception e) when (!Fx.IsFatal(e))
                {
                    RelayEventSource.Log.ThrowingException(e, this);
                    throw;
                }
                finally
                {
                    try
                    {
                        await this.connection.CloseAsync().ConfigureAwait(false);
                    }
                    catch (Exception closeException) when (!Fx.IsFatal(closeException))
                    {
                        RelayEventSource.Log.HandledExceptionAsWarning(this, closeException);
                    }

                    this.closed = true;
                }
            }