The dilemma with Response.End, Response.Redirect, or Server.Transfer and threadAbortException

If you are a .net developer  then you can hardly avoid using one of the above   methods of Response  object.In my case I was using Response.Redirect and Response.End as under

Response.Redirect(“mysampleurl”)
Response.End()

Once the code  went live , we were getting loads of errors in the event viewer .After  spending some time doing google with bing(as Scott Hanselman says )  and  doing some reading this is what I  found .

“If you use the Response.End, Response.Redirect, or Server.Transfer method, a ThreadAbortException exception occurs.”

So here are the list of viable options you could use to either recover or avoid this from happening

Option1 : For Response.End ,call the HttpContext.Current.ApplicationInstance.CompleteRequest method

 

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

NB :If you use this workaround ,the code that follows Response,Redirect is executed

 

Option3:For Server.Transfer,use Server.Execute method instead

 

Option 4:Use  multiple try catch block as under

Try

Catch threadAbortExc As Threading.ThreadAbortException
            ‘You logic to handle thread abort exception here

Catch ex As Exception
           ‘Your logic to handle all other generic exceptions here 

Finally
            ‘You logic for finally block here

End Try

 

Hope this saves trouble for some of you guys out there .

Happy coding Smile

 

References  and further readings:

http://support.microsoft.com/kb/312629

http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s