pet*_*ter 20 .net c# asp.net iis-7 httpexception
我写了一个自定义的http处理程序.我通过编写实现IHttphandler的类来完成此操作.
在那个班级里面,我有这样的代码,
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + attachmentFileName);
context.Response.AddHeader("Content-Length", new FileInfo(downloadFile).Length.ToString());
context.Response.ContentType = GetMimeType(attachmentFileName);
context.Response.TransmitFile(downloadFile);
context.Response.Flush();
context.Response.Close();
Run Code Online (Sandbox Code Playgroud)
偶尔我会收到这样的错误,
Exception HttpException The remote host closed the connection The error code is 0x800703E3
Run Code Online (Sandbox Code Playgroud)
或这个,
Exception HttpException The remote host closed the connection The error code is 0x80070040
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,堆栈跟踪都是这样的,
at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
at System.Web.HttpResponse.Flush(Boolean finalFlush)
at System.Web.HttpResponse.Flush()
Run Code Online (Sandbox Code Playgroud)
这种情况发生在生产中,如果我回顾过去几天错误发生了23次,总共上述代码被称为497次.
我怀疑这个失败与用户点击链接不止一次启动上述代码(这将给他们多个下载对话框)有关,然后他们取消了其中一些.话虽如此,如果它是这样的,我会期望连接在两端优雅地关闭.
如何证明此错误的确切原因?我试图启用这样的.NET跟踪为什么不跟踪侦听器记录自定义处理程序流量?但无法让它发挥作用.
我发现虽然我已启用IIS跟踪来记录失败的请求.失败再次发生,并且该日志中没有任何内容.
我可以启用任何其他跟踪吗?
接下来我尝试的是这个,
if (context.Response.IsClientConnected)
{
context.Response.Flush();
context.Response.Close();
}
else
{
LogMessage("Client has disconnected before flush was called", Severity.Information);
}
Run Code Online (Sandbox Code Playgroud)
但这没有任何区别.我猜的原因是客户端在下载过程中断开连接,而不是在调用flush之前.
Sam*_*eff 14
取出Flush()并Close()打电话.你真的不需要它们.处理程序完成后,它将退出,ASP.NET将处理关闭请求.
此外,Flush()应该在将内容流式传输到客户端时使用(在块中将部分添加到响应流中).你不需要使用它TransmitFile().
我遇到了类似的问题,得到了这篇文章解释说应该避免使用Response.End(),而是建议使用CompleteRequest()方法.MSDN文档也已使用此信息进行了更新.我希望这可以帮助别人.
| 归档时间: |
|
| 查看次数: |
32929 次 |
| 最近记录: |