Cannot evaluate expression because the code of the current method is optimized

May 4, 2011 at 12:45 AM

I keep getting this error when trying to step through Unity or Enterprise Library code in the context of a Windows Service. If I run the same code through a Console application and step through in Debug mode, I am able to see all in-context variables and drill down to see all object values, etc. I have confirmed that the compile options in both cases are identical. For example under Debug -> Options and Settings -> General, both the Windows Service and the Console application solutions have Just My Code unchecked as well as .Net framework source stepping unchecked. I did try checking .Net framework source stepping, but did not get a different result.

Any thoughts on how to fix this?

May 9, 2011 at 5:58 AM


I was able to step inside the Unity source code while debugging a Windows Service project. Not sure what could probably be missing in your end. Could you be able to send us a repro project for this for us to further investigate.

Btw, you can also check this thread ( and see if this may help. HTH.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc. 

May 11, 2011 at 10:08 PM

Gino - your pointers did help me fix the problem. I had changed my compile output directory to a non-default path. All pdbs were being copied correctly to that path. However, I was missing the step of adding that path under Debug Options -> Symbols. As soon as that path was added, I could see all the debug information. Thank you for your support and guidance.

May 12, 2011 at 5:52 AM

Well almost. I still don't see anything inside ValidationCallHandler (Microsoft.Practices.EnterpriseLibrary.Validation.PolicyInjection). There seems to be no independent dll or pdb for that namespace. Also, the debugger cannot find it in any of the existing pdbs (even when I copy over all pdbs from Unity and Entlib bin directories into my solution's bin directory--which shouldn't be required)

May 12, 2011 at 5:58 PM

How deep have you been able to step inside the source code? Note to continuosly step inside the Invoke method until further down in the ValidationCallHandler. If VS prompts you to locate the ValidationCallHandler class or any class make sure to properly set it up to the correct path on where the entlib or unity source code has been deployed in your machine.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.

May 12, 2011 at 6:29 PM

I am able to step through ValidationCallHandler code; the problem is when I hover on some variable, I can't see any values inside it. Also, typing that variable name in the Immediate Window in debug mode does not get me the value. Even simple properties like .Count don't show up.

May 13, 2011 at 2:17 AM

That's odd then, I was able to hover on some variable and see its values.

ValidationCallHandler Screenshot

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.

May 13, 2011 at 4:31 AM

I do see similar values in some cases, but in this particular case (ValidationCallHandler) as well as some others, I think for some reason it is unable to load debug symbols.

Jul 8, 2011 at 8:35 PM

So finally I figured out why I was getting this problem. Turns out if you have Visual Studio hosting enabled in the project, the values will be optimized away and cannot be watched in debug mode. Crediting the original sources that helped me fix the problem: