Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between / use cases for using Response.End(false) vs ApplicationInstance.CompleteRequest()

Tags:

asp.net

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?

like image 649
Shai Cohen Avatar asked Nov 20 '25 19:11

Shai Cohen


1 Answers

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."

like image 200
Win Avatar answered Nov 23 '25 08:11

Win



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!