2009-10-12 81 views
10

我有一个HttpHandler,我用它来处理客户端网站上的某些图像。当我将图像流输出到响应对象并偶尔调用Flush时,会引发错误。这里是一个代码块Response.Flush()抛出System.Web.HttpException


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

有没有办法进行刷新之前,要检查这个没有尝试它包裹/ catch块。?

回答

6

虽然我同意Mitchel - 因为您打算调用End,所以几乎没有必要调用flush,如果您在其他地方使用此方法,则可以先尝试调用Response.IsClientConnnected

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

+0

所以,我可以换我的.Flush()和.END()在.IsClientConnected并有修复我的问题,你觉得呢?客户的客户没有收到他们屏幕上的错误,我每次只收到ELMAH的电子邮件。只是一个小小的烦恼比什么都重要。 – 2009-10-12 18:42:43

+0

你肯定可以将.Flush包裹在检查中 - 不太确定是否存在任何未调用的影响。结束......我认为在那里有一些长时间运行的进程会导致人们在完成图像生成之前断开连接。 – 2009-10-12 20:58:33

+0

我可以确认在.IsClientConnected内包装flush可以抛出相同的异常;现在发生在我身上。坚持.End() – Contra 2011-06-28 09:02:40

11

个人在你的实现中,因为下一行是Response.End(),只需将Response.Flush()的调用移除为Response.End()负责处理所有事情。

0

我意识到这是一个旧的帖子,但它出现时,当我正在寻找一个类似的问题的答案。以下内容主要来自this SO answer。更多的背景信息可在Is Response.End() considered harmful?

替换此: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. 
相关问题