12

I am getting an error in my asp.net app that reads

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

protected void btnCustomerProfile_Click(object sender, EventArgs e)
{
    try
    {
        Server.Transfer("CustomerProfile.aspx");
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
    finally
    { }
}

After searching SO, I see most of the similar posts involve response.redirect. My code is using server.transfer and my application is also using Master Pages.

How can I resolve this issue?

Update: For some reason, this error occurs is I use Response.Redirect as well. Unfortunately I cannot use Server.Execute, because Server.Execute calls the calling page towards the end.

DNR
  • 3,706
  • 14
  • 56
  • 91
  • can you post the piece of code that throws the exception? – davioooh Jun 11 '12 at 15:35
  • uhmm... in my case the problem was caused by `Response.End`... But looking at your code is very difficult to say what's the cause of this issue... – davioooh Jun 11 '12 at 15:44

4 Answers4

13

You will get an error, but the code block below will trap it and you can get on with your life.

Try this:

using System.Threading.ThreadAbortException;

catch(ThreadAbortException ex)
{
    throw;
}
Simple Sandman
  • 900
  • 3
  • 11
  • 34
Troy
  • 1,659
  • 4
  • 19
  • 33
10

The issue you describe seems to be by design as shown here:

http://support.microsoft.com/kb/312629/EN-US/

Using Server.Execute should solve the problem

  • The information on your link indicates .net 1.0 & 1.1 framework. I am using 4.0 framework. I would think this issue would be resolved 8 years later. – DNR Jun 11 '12 at 15:37
  • 8
    @DotNetRookie Microsoft doesn't solve its issues, only mark them as "known issues"... :) – davioooh Jun 11 '12 at 15:38
  • The issue with server.execute is that it goes back to the calling page. Since I currently have code that does server.transfer I want to see if I can stick with that same coding strategy. http://techahead.wordpress.com/2007/10/14/aspnet-servertransfer-vs-serverexecute/ – DNR Jun 11 '12 at 17:11
  • Is the exception generated a ThreadAbortException? Also is there an inner exception message, might be worth writing out the exception to a log file to make sure that the debugger isn't formatting the message – AvengingBudda Jun 11 '12 at 18:14
2

Have you tried replacing the server.transfer with response.redirect()?

Server.Transfer VS Response.Redirect

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • The application currently has code that uses server.transfer (code was done by a developer who is no longer here), so I need to stay consistent with the current architecture. – DNR Jun 11 '12 at 15:41
2

I faced this message when I was testing working of multi-threading application using MS-Test.

I found the reason for this was because the testing main thread got ended and initialized cleaning of objects while other created threads which are meant to run infinitely were still working.

As teat clean up method kills objects these threads gets aborted showing above message.

jit4peace
  • 21
  • 5