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
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?.
Replace this: 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.
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.
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.IsClientConnnected first.
Gets a value indicating whether the client is still connected to the server.
For future readers..
I ran into this case where Response.End() throws an error because the client is disconnected.
An error occurred while communicating with the remote host. The error code is 0x80070057
Oddly a CRLF in the StatusDescription was causing the connection to close.
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
Removing it fixed my problem.
Response.StatusDescription = ex.Message.Replace("\r\n", " ");