Friday, April 24, 2015

Debugging optimized managed code in Sitecore (any other website) / Disable JIT Optimization

Latest Sitecore installation contains assemblies build with checked optimization flag. It causes troubles when you attach to w3wp(IIS) process and try to debug them. There is known way how to disable JIT Optimization:

Disabling Runtime Optimization:

  1. Close any application that may be using the optimized binaries.
  2. Navigate to the folder containing the binaries.
  3. Create a new file named BINARYNAME_WITHOUT_THE_EXTENSION.ini
    E.g. If I do not want MyApp.exe to be optimized, and then create an INI file named MyApp.ini.
    E.g. If I do not want MyAppHelper.dll to be optimized, and then create an INI file named MyAppHelper.ini.
  4. Open the INI file.
    Add the following text:
    [.NET Framework Debugging Control]
    GenerateTrackingInfo=1
    AllowOptimize=0
  5. Save the file.
  6. Now you are ready to debug the application with optimization disabled.
  7. Note that you will have to create the INI file for each dll/exe that contains the code you want to debug.
  8. So the best way to go about this is to create a small windows app that will create an INI file for all files (exe and dll) in a particular folder.

But it does not work when you attach to w3wp process… It happens because IIS uses assemblies from ASP.Net Temporary folder, e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files. So, you should open your solution from folder on IIS and start debug from VS.

There are a lot of DLL files in Sitecore BIN folder and I decided to write powershell script that will create ini files for all assemblies.

Get-ChildItem (Get-Item -Path ".\" -Verbose).FullName -Filter *.dll | 
Foreach-Object{
$newName = $_.DirectoryName +"
\"+ [System.IO.Path]::GetFileNameWithoutExtension($_) + ".ini"
"
[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0" | Out-File $newName
Write-host $newName
}

Use and enjoy :-)