HttpClient.SendAsync HttpException: client disconnected

I have an ASP.NET web API application hosted in a Windows Azure web role. The purpose of this application is Http proxy requests for other network-enabled endpoints, such as relaying the service bus and returning their response.

Sometimes our application throws an exception when sending a request with a significant payload (> 5 MB). This can happen 1 out of 20 queries with a large payload.

Exception Details: System.AggregateException: One or more errors occurred. ---> System.Web.HttpException: the client is disconnected.
in System.Web.Hosting.IIS7WorkerRequest.EndRead (IAsyncResult asyncResult) in System.Web.HttpBufferlessInputStream.EndRead (IAsyncResult asyncResult) in System.Net.Http.StreamToStreamCopy.Bufferead - ---> (Internal exception # 0) System.Web.HttpException (0x800703E3): the client is disconnected. in System.Web.Hosting.IIS7WorkerRequest.EndRead (IAsyncResult asyncResult) in System.Web.HttpBufferlessInputStream.EndRead (IAsyncResult asyncResult) in System.Net.Http.StreamToStreamCopyBufferead (IA) TraceSource event 'w3wp.exe'

We send these Http requests using System.Net.HttpClient in .NET 4.5.

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) => { var tempStream = response.Content.ReadAsStreamAsync().Result; tempStream.CopyToAsync(stream).Wait(); stream.Flush(); stream.Close(); }) }; } } 

Any thoughts on what might cause this problem?

+4
source share
1 answer

Try to return the version of the task, and then replace the synchronization code with async. There is no more .Results, instead use the wait keyword and put the asyc keyword in your method. Something like that:

 public class ProxyController : ApiController { private static readonly HttpClient HttpClient = new HttpClient(); private static readonly Uri BaseUri = new Uri("http://webendpoint.com"); public async Task<HttpResponseMessage> Post() { var newUri = new Uri(BaseUri, Request.RequestUri.PathAndQuery); var request = new HttpRequestMessage(HttpMethod.Post, newUri) { Content = Request.Content }; var response = await HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); return new HttpResponseMessage(response.StatusCode) { Content = new PushStreamContent(async (stream, content, ctx) => { var tempStream = await response.Content.ReadAsStreamAsync(); await tempStream.CopyToAsync(stream); stream.Flush(); stream.Close(); }) }; } } 
+1
source

Source: https://habr.com/ru/post/1482333/


All Articles