3

I came across an SO question that discussed the use of ApplicationInstance.CompleteRequest() to avoid a ThreadAbortException being thrown when Response.End() is called.

In the past, to avoid the exception error I mentioned above, I have used this overload: Response.End(false).

I am trying to understand what are the differences between the two methods. Why would I choose to use ApplicationInstance.CompleteRequest() in place of Response.End(false)?

EDIT:

I understand that ApplicationInstance.CompleteRequest() is the more correct way of doing things, but the one feature that calling Response.End(true) that is missing is not processing the rest of the code that follows. Sometimes, we do not want to continue with the processing, but want to end the execution right there and then.

Maybe answering my own question here, but maybe the correct way of doing things is to write the code so that when ending the response (and not aborting the thread), there are no more lines of code to process.

An example:

[DO SOME WORK]
if ([SOME CONDITION])
{
     ApplicationInstance.CompleteRequest();
}
else
{
    //continue processing request
}

This may work, but it seems like we are coding here to deal the limitations of the method. And can lead to some messy code.

Am I missing something here?

Shai Cohen
  • 6,074
  • 4
  • 31
  • 54
  • http://stackoverflow.com/questions/10603553/response-end-vs-httpcontext-current-applicationinstance-completerequest – Win Jan 29 '13 at 20:08
  • @Win although the _title_ of that post is similar to mine, it does not discuss the differences between the two. Or am I missing something? – Shai Cohen Jan 29 '13 at 20:18
  • You are right. It did not discuss about ApplicationInstance.CompleteRequest(). +1 – Win Jan 29 '13 at 21:34

1 Answers1

3

http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx

From article: "instead of using the HttpResponse.End and HttpResponse.Close methods, the best would be to use the HttpApplication.CompleteRequest method, so that the response data that is buffered on the server, the client or in between won’t get dropped."

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello Hajan");
    Response.End();            
    Response.Write("<br />Goodbye Hajan");
}
Output: Hello Hajan

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello Hajan");
    this.Context.ApplicationInstance.CompleteRequest();
    Response.Write("<br />Goodbye Hajan");
}

Output: Hello Hajan 
Goodbye Hajan

http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx

From article: "HttpApplication.CompleteRequest is a better way to end a request."

Win
  • 61,100
  • 13
  • 102
  • 181