HttpClient.SendAsync HttpException:客户端已断开连接

时间:2013-05-23 08:00:39

标签: c# asp.net asp.net-web-api dotnet-httpclient

我有一个在Windows Azure Web角色中托管的ASP.NET Web API应用程序。 此应用程序的目的是将Http请求代理到其他启用Web的端点 - 例如服务总线中继并返回其响应。

有时,我们的应用程序在发送具有重要(> 5MB)有效负载的请求时会抛出异常。这可能发生在具有大负载的20个请求中的1个。

  

异常详细信息:System.AggregateException:一个或多个错误   发生了。 ---> System.Web.HttpException:客户端断开连接。
  在System.Web.Hosting.IIS7WorkerRequest.EndRead(IAsyncResult   asyncResult)at   System.Web.HttpBufferlessInputStream.EndRead(IAsyncResult asyncResult)   在System.Net.Http.StreamToStreamCopy.BufferReadCallback(IAsyncResult)   ar)---内部异常堆栈跟踪结束---   ---> (内部异常#0)System.Web.HttpException(0x800703E3):客户端已断开连接。在   System.Web.Hosting.IIS7WorkerRequest.EndRead(IAsyncResult asyncResult)   在System.Web.HttpBufferlessInputStream.EndRead(IAsyncResult   asyncResult)at   System.Net.Http.StreamToStreamCopy.BufferReadCallback(IAsyncResult的   ar)< ---; TraceSource'w3wp.exe'事件

我们使用.NET 4.5中的System.Net.HttpClient发送这些Http请求。

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();
            })
        };
    }
}

关于可能导致此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

尝试返回任务版本,然后用异步替换同步代码。没有更多"。结果"东西,而是使用" await"关键字并放置" asyc"您方法上的关键字。像这样:

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();
            })
        };
    }
}