title: HttpClient.SendAsync HttpException: The Client Disconnected
body: <p>I have an ASP.NET web API application hosted in a Windows Azure Web Role.
  The purpose of this application is to proxy Http Requests to other web enabled endpoints - such as a Service Bus Relay and return their response.</p>
  
  <p>On occasion our application throws an exception when sending a request with a significant (>5MB) payload. This might happen 1 in 20 requests with a large payload.</p>
  
  <blockquote>
    <p>Exception Details: System.AggregateException: One or more errors
    occurred. ---> System.Web.HttpException: The client disconnected.<br>
    at System.Web.Hosting.IIS7WorkerRequest.EndRead(IAsyncResult
    asyncResult)    at
    System.Web.HttpBufferlessInputStream.EndRead(IAsyncResult asyncResult)
    at System.Net.Http.StreamToStreamCopy.BufferReadCallback(IAsyncResult
    ar)    --- End of inner exception stack trace ---
    ---> (Inner Exception #0) System.Web.HttpException (0x800703E3): The client disconnected.    at
    System.Web.Hosting.IIS7WorkerRequest.EndRead(IAsyncResult asyncResult)
    at System.Web.HttpBufferlessInputStream.EndRead(IAsyncResult
    asyncResult)    at
    System.Net.Http.StreamToStreamCopy.BufferReadCallback(IAsyncResult
    ar)&lt;--- ; TraceSource 'w3wp.exe' event</p>
  </blockquote>
  
  <p>We send these Http Requests using the System.Net.HttpClient in .NET 4.5.</p>
  
  <pre><code>public class ProxyController : ApiController
  {
      private static readonly HttpClient HttpClient = new HttpClient();
      private static readonly Uri BaseUri = new Uri("http://webendpoint.com");
  
      public HttpResponseMessage Post()
      {
          var newUri = new Uri(BaseUri, Request.RequestUri.PathAndQuery);
          var request = new HttpRequestMessage(HttpMethod.Post, newUri)
                  {
                      Content = this.Request.Content
                  };
  
          var task = HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
          task.Wait();
          var response = task.Result;
          return new HttpResponseMessage(response.StatusCode)
          {
              Content = new PushStreamContent((stream, content, ctx) =&gt;
              {
                  var tempStream = response.Content.ReadAsStreamAsync().Result;
                  tempStream.CopyToAsync(stream).Wait();
                  stream.Flush();
                  stream.Close();
              })
          };
      }
  }
  </code></pre>
  
  <p>Any thoughts on what could be causing this problem?</p>