VS 2010 and VS 2012 Side by Side ILMerge

Gayan Perera, 02 November 2012

I recently received a bug with an old plugin, this plugin project had post build steps which ILMerged multiple assemblies. When I build the project on a machine that didn’t have Visual Studio 2012 it compiled, ran the post build steps correctly, but when I run it on a machine that has both Visual Studio 2010 and 2012 ILMerge.exe failed with the following error message:

Compile complete -- 0 errors, 0 warnings
  *.Plugins -> x:\Magnetism\Development\*.co.nz\*2011\*.Plugins\bin\Debug\*.Plugins.dll

  An exception occurred during merging:
  Unable to load DLL 'mscorsn.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
     at System.Compiler.Writer.MscorsnStrongNameSignatureGeneration(String wszFilePath, String wszKeyContainer, Byte[] pbKeyBlob, Int32 cbKeyBlob, IntPtr ppbSignatureBlob, IntPtr pcbSignatureBlob)
     at System.Compiler.Writer.WritePE(String location, Boolean writeDebugSymbols, Module module, Boolean delaySign, String keyFileName, String keyName)
     at System.Compiler.Writer.WritePE(CompilerParameters compilerParameters, Module module)
     at ILMerging.ILMerge.Merge()
     at ILMerging.ILMerge.Main(String[] args)

To fix this issue, simply copy mscorsn.dll from the v2.0 framework directory (C:\Windows\Microsoft.NET\Framework\v2.0.50727) into where your ILMerge.exe is then rebuild the project.

If you get another error similar to:
TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute'

Then change the post build ILMerge command to reference a different target directory. Your original command most likely will look like this

"$(SolutionDir)_libs\ILMerge.exe" /out:"$(SolutionDir)_deployment\$(TargetFileName)" "$(TargetDir)$(TargetFileName)" "$(TargetDir)xyz.dll" /target:dll /targetplatform:"v4, C:\Windows\Microsoft.NET\Framework64\v4.0.30319" /wildcards /keyfile:"$(ProjectDir)somekey.snk"

What you’ll need to do is change the /targetplatform to look at a different folder. Eg:
/targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"