I have ASP .Net Web Application in C# (no MVC or WebForms) compiled in Release mode for AnyCPU, pdb files are enabled and deployed with application.
When enable 32-bit applications
on AppPool has the default value of False
the stack trace of the exception has correct line numbers.
When the flag is set to True
the stack trace has incorrect line numbers.
Just to make it clear the only thing I change is the value for enable 32-bit applications
flag in AppPool configuration of my web application.
I have tried this on two machines:
In my particular case it is OK to just reconfigure AppPool (we have already migrated from x86 to AnyCPU and this obsolete configuration is just a mistake), but I am still interested why does this happen? (may be there is some bug in IIS, I was not able to find this behavior mentioned anywhere).
Update: it seems I have figured it out, but this is a temporary reprieve:
So the answer seems to be:
There are basically two steps of optimization in C#: a compilter (csc.exe, when C# code is translated into IL) and a jitter (when IL is translated in machine code). Jitter does not most of the optimizations (article). Also there is a great post by Eric Lippert about which optimization you might expect.
x86 and x64 jitters do different optimizations (CLR via C# Fourth Edition by Jeffrey Richter, Part V Threading, section Volatile Constructs, page 764)
Thus you can get correct line numbers in x64 (because jitter does not optimize code that aggressively) and x86 (that is more mature).
Summary: I have not found a way to work around it.