【问题标题】:Response.Flush() throws System.Web.HttpExceptionResponse.Flush() 抛出 System.Web.HttpException
【发布时间】:2010-12-06 01:53:00
【问题描述】:

我有一个 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 块中。?

【问题讨论】:

    标签: c# asp.net image httphandler


    【解决方案1】:

    由于下一行是 Response.End(),个人在您的实施中,只需删除对 Response.Flush() 的调用,因为 Response.End() 会为您处理一切。

    【讨论】:

      【解决方案2】:

      虽然我同意 Mitchel 的观点 - 因为您即将调用 End,所以几乎不需要调用 flush,如果您在其他地方使用它,您可以尝试先调用 Response.IsClientConnnected

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

      【讨论】:

      • 那么,我可以将我的 .Flush() 和 .End() 包装在 .IsClientConnected 中,这样可以解决我的问题吗?客户的客户在他们的屏幕上没有收到错误,我每次都收到来自 ELMAH 的电子邮件。只是一个小烦恼比什么都重要。
      • 您当然可以将 .Flush 包含在支票中 - 不太确定不调用 .End 的任何影响...我认为那里有一些长期运行的过程会导致人们在您之前断开连接'已经完成生成图像了吗?
      • 我可以确认在 .IsClientConnected 中包装刷新会引发相同的异常;现在发生在我身上。坚持使用 .End()
      【解决方案3】:

      我意识到这是一篇旧帖子,但它是在我寻找类似问题的答案时出现的。以下内容大部分来自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.
      

      【讨论】:

        【解决方案4】:

        为未来的读者..

        我遇到了这种情况,其中 Response.End() 由于客户端断开连接而引发错误。

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

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

        Response.StatusDescription = ex.Message;
        

        无法将值 NULL 插入列 '',表 ''; 列不允许空值。 INSERT 失败。\r\n语句已被 终止

        删除它解决了我的问题。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-02
          • 2013-02-18
          • 1970-01-01
          • 1970-01-01
          • 2018-02-23
          相关资源
          最近更新 更多