C# Response.Flush() 抛出 System.Web.HttpException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1556073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:48:46  来源:igfitidea点击:

Response.Flush() throws System.Web.HttpException

c#asp.netimagehttphandler

提问by Anthony Shaw

I have a HttpHandler that I'm using to handle certain images on a client's website. When I'm outputting the image stream to the response object and call Flush occasionally an error is thrown. Here is a codeblock

我有一个 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();

From what I've read, this exception is caused by a client disconnecting before the process has completed and there is nothing to flush.

从我读过的内容来看,此异常是由客户端在进程完成之前断开连接引起的,并且没有任何东西可以刷新。

Here is an output of my error page

这是我的错误页面的输出


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\workb3c57a060ff42d\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 falls at line 75.

context.Response.Flush 落在第 75 行。

Is there a way to check this before performing the flush without wrapping it in a try/catch block.?

有没有办法在执行刷新之前检查这一点,而无需将其包装在 try/catch 块中。?

采纳答案by Zhaph - Ben Duguid

While I agree with Mitchel- there's little need to call flush as you're about to call End, if you're using this elsewhere, you could try calling Response.IsClientConnnectedfirst.

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

Gets a value indicating whether the client is still connected to the server.

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

回答by Mitchel Sellers

Personally in your implementation since the next line is Response.End(), just remove the call to Response.Flush() as Response.End() takes care of everything for you.

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

回答by David Clarke

I realise this is an old post but it came up when I was looking for an answer to a similar issue. The following is largely verbatim from this SO answer. More background info is available at Is Response.End() considered harmful?.

我意识到这是一篇旧帖子,但在我寻找类似问题的答案时出现了。以下内容主要来自此 SO 答案。更多背景信息可在Response.End() 被认为有害吗?.

Replace this: HttpContext.Current.Response.End();

替换这个: HttpContext.Current.Response.End();

With this:

有了这个:

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.

回答by Garfield

For future readers..

对于未来的读者..

I ran into this case where Response.End() throws an error because the client is disconnected.

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

An error occurred while communicating with the remote host. The error code is 0x80070057

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

Oddly a CRLF in the StatusDescription was causing the connection to close.

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

Response.StatusDescription = ex.Message;

Cannot insert the value NULL into column '', table ''; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated

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

Removing it fixed my problem.

删除它解决了我的问题。

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