C# Response.Flush()引发System.Web.HttpException

C# Response.Flush()引发System.Web.HttpException,c#,asp.net,image,httphandler,C#,Asp.net,Image,Httphandler,我有一个HttpHandler,用来处理客户网站上的某些图像。当我将图像流输出到响应对象并调用Flush时,偶尔会抛出一个错误。这是一个代码块 var image = Image.FromStream(memStream); if (size > -1) image = ImageResize.ResizeImage(image, size, size, false); if (height > -1) image = ImageResize.Crop(image, size, height,

我有一个HttpHandler,用来处理客户网站上的某些图像。当我将图像流输出到响应对象并调用Flush时,偶尔会抛出一个错误。这是一个代码块


var image = Image.FromStream(memStream);
if (size > -1) image = ImageResize.ResizeImage(image, size, size, false);
if (height > -1) image = ImageResize.Crop(image, size, height, ImageResize.AnchorPosition.Center);

context.Response.Clear();
context.Response.ContentType = contentType;
context.Response.BufferOutput = true;

image.Save(context.Response.OutputStream, ImageFormat.Jpeg);

context.Response.Flush();
context.Response.End();

从我所读到的内容来看,这个异常是由一个客户端在进程完成之前断开连接造成的,并且没有任何东西需要刷新

这是我的错误页面的输出


System.Web.HttpException: An error occurred while communicating with the remote host. The error code is 0x80070057.
Generated: Mon, 12 Oct 2009 03:18:24 GMT

System.Web.HttpException: An error occurred while communicating with the remote host. The error code is 0x80070057.
   at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)
   at System.Web.HttpResponse.Flush(Boolean finalFlush)
   at System.Web.HttpResponse.Flush()
   at PineBluff.Core.ImageHandler.ProcessRequest(HttpContext context) in c:\TeamCity\buildAgent\work\79b3c57a060ff42d\src\PineBluff.Core\ImageHandler.cs:line 75
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
context.Response.Flush位于第75行


在执行刷新之前,是否有方法检查这一点而不将其包装在try/catch块中?

在您的实现中,因为下一行是Response.End(),只需将对Response.flush()的调用作为Response.End()删除即可为您解决所有问题。

虽然我同意您的看法,但在您即将调用End时,几乎不需要调用flush,如果您在其他地方使用此功能,您可以先尝试调用

获取一个值,该值指示客户端是否仍连接到服务器


我意识到这是一篇老文章,但它是在我寻找类似问题的答案时出现的。以下内容大体上是作者的逐字记录。有关更多背景信息,请访问

替换此:
HttpContext.Current.Response.End()

为此:

HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
给未来的读者

我遇到过这样的情况,Response.End()抛出一个错误,因为客户端已断开连接

与远程主机通信时出错。错误 代码是0x80070057

奇怪的是,StatusDescription中的CRLF导致连接关闭

Response.StatusDescription = ex.Message;
无法将值NULL插入列“”,表“”; 列不允许空值。INSERT失败。\r\n语句已被删除 终止

移除它修复了我的问题

Response.StatusDescription = ex.Message.Replace("\r\n", " ");

那么,我可以将.Flush()和.End()封装在一个.IsClientConnected中,并让它修复我的问题吗?客户的客户没有在他们的屏幕上收到错误,我每次都收到ELMAH的电子邮件。只是一个小小的烦恼比什么都重要。你当然可以在支票上写上.Flush-不太确定不打电话的后果。结束。。。我假设其中有一个长时间运行的进程导致人们在生成完映像之前断开连接?我可以确认.IsClientConnected中的换行刷新会引发相同的异常;现在发生在我身上。坚持。结束()