微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

Exception Details: System.AggregateException: One or more errors
occurred. —> System.Web.HttpException: 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) — 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)<— ; TraceSource ‘w3wp.exe’ event

我们使用.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();
            })
        };
    }
}

有什么可能导致这个问题的想法?

解决方法

尝试返回任务版本,然后用异步替换同步代码.不再使用“.Results”内容,而是使用“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,newUri)
        {
            Content = Request.Content
        };

        var response = await HttpClient.SendAsync(request,HttpCompletionoption.ResponseHeadersRead);

        return new HttpResponseMessage(response.StatusCode)
        {
            Content = new PushStreamContent(async (stream,ctx) =>
            {
                var tempStream = await response.Content.ReadAsstreamAsync();
                await tempStream.copyToAsync(stream);
                stream.Flush();
                stream.Close();
            })
        };
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐