RSS

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

27 Jan

Problem description:
This is one of the common runtime error reported by a lot of ASP.NET users. A successfully compiled code at runtime throws the following error during an operation:

“Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.”

Typically, this happens when the Response.EndResponse.Redirect, or Server.Transfer method is used.

Assessment:
This happens because current page execution is stopped and execution is sent to the Application_EndRequest event in the application’s event pipeline.

The duty of Response.End method is to stop the page execution and raise the EndRequest event. Thus, the line of code that follows Response.End is not executed. Error occurs with Response.Redirect and Server.Transfer methods too because both the methods call Response.End internally.

When a call is made to the Abort method to destroy a thread, the CLR throws a ThreadAbortException. It is a special exception that can be caught, but will automatically be raised again at the end of the catch block. When this exception is re-raised, the CLR executes all the finally blocks before ending the thread.

Using Visual Studio debugger, we can see the internal message for the exception as “Thread was being aborted.”

Resolution:

  • Use a try-catch statement to catch the exception if needed
    try {
    }
    catch (System.Threading.ThreadAbortException ex){
    }
  • For Response.End : Invoke  HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event
  • For Response.Redirect : Use an overload, Response.Redirect(String url, bool endResponse) that passes false for endResponse parameter to suppress the internal call to Response.End
    // code that follows Response.Redirect is executed
    Response.Redirect ("mynextpage.aspx", false);
  • For Server.Transfer : Use Server.Execute method to bypass abort. When Server.Execute is used, execution of code happens on the new page, post which the control returns to the initial page, just after where it was called.

Refer:
Microsoft Support Article – 312629
MSDN: HttpResponse.Redirect Method
MSDN: HttpResponse.End Method
MSDN: ThreadAbortException Class
MSDN: HttpServerUtility.Execute Method

Conclusion:
This is as designed. This exception has bad effect on Web application performance, which is why handling the scenario correctly is important.

Advertisements
 
1 Comment

Posted by on January 27, 2013 in ASP.NET

 

Tags: , , , ,

One response to “Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

  1. Viresh Rajput

    January 28, 2014 at 3:17 pm

    Thanks for posting such type of post.
    Nice work please keep it up

     

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: