Tuesday, 8 April 2014

Know about Response.End, HttpApplication.CompleteRequest and Response.Close

Definition and usage of Response.End, HttpApplication.CompleteRequest and Response.Close.

Respnse.end()

The end method causes the Web server to stop processing the script and return the current result. The remaining contents of the file are not processed

The End method is only to be compatible with classic ASP when 1.0 was released. 

Classic ASP has a Response.End method that terminates processing of the ASP script.  To mimic this behavior, ASP.NET’s End method tries to raise a ThreadAbortException.  If this is successful, the calling thread will be aborted (very expensive, not good for performance) and the pipeline will jump ahead to the EndRequest event. 

The ThreadAbortException, if successful, of course means that the thread unwinds before it can call any more code, so calling End means you won’t be calling any code after that.

If the End method is not able to raise a ThreadAbortException, it will instead flush the response bytes to the client, but it does this synchronously which is really bad for performance, and when the user code after End is done executing, the pipeline jumps ahead to the EndRequest notification. 

Writing bytes to the client is a very expensive operation, especially if the client is halfway around the world and using a 56k modem, so it is best to send the bytes asynchronously, which is what we do when the request ends the normal way. 

Flushing synchronously is really bad.  So to summarize, you shouldn’t use End, but using CompleteRequest is perfectly fine. 

The documentation for End should state that CompleteRequest is a better way to skip ahead to the EndRequest notification and complete the request.

HttpApplication.CompleteRequest()

CompleteRequest() simply bypasses the Response.End() method, which is what generates the ThreadAbortException you mentioned, but crucially CompleteRequest() flushes the response buffer. This means the HTTP 302 redirect response is sent to the browser at the line where you call CompleteRequest(), which gives you a chance to do operations that don't affect the response after it's been sent to the user.

HttpResponse.End flushes the output buffer to the client and terminates the current request-handling thread , whereas HttpApplication.CompleteRequest tells ASP.NET to immediately skip all future stages in the ASP.NET pipeline and jump directly to the EndRequest step (which also raises the HttpApplication.EndRequest event. The request thread then proceeds with normal end-of-life cleanup.

So, Response.End is like an ejector seat: it quickly ends things, but means you lose control and might be unnecessarily harsh. Whereas CompleteRequest is like making an emergency landing at the nearest airport.

Response.Close()

Closes the socket connection to a client.

This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. The method sends a reset packet to the client, which can cause response data that is buffered on the server, the client, or somewhere in between to be dropped.

You might use this method in response to an attack by a malicious HTTP client. However, typically you should call CompleteRequest instead if you want to jump ahead to the EndRequest event and send a response to the client.

Response.Close() should really only be used under error conditions to abort the response/connection."

Response.Close calls HttpWorkerRequest.CloseConnection, which is described on MSDN as “Terminates the connection with the client.”


About Author:
Nirmal Doshi is budding technology geek, who helps Systems Plus Pvt. Ltd. with his creativity and research on technology. He works in systems Plus and actively contributes to technology. He can be contacted at: nirmal.d@spluspl.com

1 comment: