Thursday, November 12, 2009

Thread abort exception

While working with an application, I came across a ThreadAbort Exception at Response.End statement.

Cause of this issue is:

The Response.End method ends the page execution and shifts the execution to the Application_End Request event in the application's event pipeline. The line of code that follows Response.End is not executed.

Same issue occurs in Response.Redirect & Server.Transfer (Since Response.End is called internally for these methods).

Remedies provided by microsoft are as follows.

1) Use HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_End Request event.

2) For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End.

Response.Redirect ("Default.aspx", false);

3) Use Server.Execute method instead of Server.Transfer

Lessons Learned

1)Always use the overloaded method of Response.Redirect and use false for end response
2)Use HttpContext.Current.ApplicationInstance.CompleteRequest instead of Respons.End
3)Use Server.Execute method instead of Server.Transfer

No comments:

Post a Comment


Obstacles are those frightful things you see when you take your eyes off your goal.------> by Henry Ford