System.Net.Http.HttpClient.HandleFinishSendAsyncCleanup C# (CSharp) Method

HandleFinishSendAsyncCleanup() private method

private HandleFinishSendAsyncCleanup ( HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts ) : void
request HttpRequestMessage
cts System.Threading.CancellationTokenSource
disposeCts bool
return void
        private void HandleFinishSendAsyncCleanup(HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts)
        {
            try
            {
                // When a request completes, dispose the request content so the user doesn't have to. This also
                // helps ensure that a HttpContent object is only sent once using HttpClient (similar to HttpRequestMessages
                // that can also be sent only once).
                request.Content?.Dispose();
            }
            finally
            {
                if (disposeCts)
                {
                    cts.Dispose();
                }
            }
        }

Usage Example

        private async Task <HttpResponseMessage> FinishSendAsyncUnbuffered(
            Task <HttpResponseMessage> sendTask,
            HttpRequestMessage request,
            CancellationTokenSource cts,
            bool disposeCts)
        {
            HttpClient          httpClient = this;
            HttpResponseMessage httpResponseMessage;

            try
            {
                HttpResponseMessage response = await sendTask.ConfigureAwait(false);

                if (response == null)
                {
                    throw new InvalidOperationException(SR.net_http_handler_noresponse);
                }
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.ClientSendCompleted(httpClient, response, request);
                }
                httpResponseMessage = response;
            }
            catch (Exception ex)
            {
                httpClient.HandleFinishSendAsyncError(ex, cts);
                throw;
            }
            finally
            {
                httpClient.HandleFinishSendAsyncCleanup(cts, disposeCts);
            }
            return(httpResponseMessage);
        }