protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var stopWatch = ValueStopwatch.StartNew();

            var response = await base.SendAsync(request, cancellationToken);

            var responseStream = await response.Content.ReadAsStreamAsync();

            var wrapper = new EndOfStreamDetectingStream(responseStream, delegate
            {
                CreateChild(request, response).Observe(stopWatch.GetElapsedTime().TotalSeconds);
            });

            // Replace the Content with an implementation that observes when it has been fully read.
            var oldContent = response.Content;

            response.Content = new StreamContent(wrapper);

            // Copy headers.
            foreach (var header in oldContent.Headers)
            {
                response.Content.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }

            return(response);
        }
Esempio n. 2
0
                static async Task <int> ReadAsyncCore(EndOfStreamDetectingStream stream, Task <int> readTask)
                {
                    int read = await readTask;

                    if (read == 0)
                    {
                        stream.SignalCompletion();
                    }

                    return(read);
                }