How to debug a TypeLoadException during ngen

0

Situation

I'm using ngen to create native images of my assemblies during the installation. This worked fine until recently and I've no idea what broke it. Sadly the ngen output isn't very helpful. It just says that it couldn't find or load a type, but doesn't say which type.

What's very weird is that ngen only fails if my assembly was build with msbuild (either run by the TFS Team Build or by myself in the command line), but if I build it with Visual Studio 2010 ngen works fine! I've tried msbuild from .NET 4.0 (my local machine), .NET 4.5 (TFS 2012 Update 3) and .NET 4.5.1 (TFS 2013), all result in the same error in ngen. It's just the build in the Visual Studio 2010 GUI that works. But the error only occurs in ngen, runtime behavior is the same - it falls back to the IL assembly, but works fine.

I even tried to debug ngen.exe and mscorsvw.exe, but all I could see was that some native exceptions were thrown, one of them was a EETypeLoadException, but I couldn't get additional information about the exceptions. But I'm not experienced at all when it comes to debug native applications.

Questions

Does anyone know how I could get more information about the ngen error? The name of the type which causes the error would be great.

I've already read somewhere that Visual Studio doesn't just invoke msbuild for building, but why are there that big differences?

Additional Information

The TFS runs msbuild during the automated build. No additional msbuild parameters are configured and the build process template is the default one from TFS 2012 Update 3.

When I run msbuild myself I'm using the following command on the same file and folder that is also used in Visual Studio. Configuration and Platform are also the ones active in Visual Studio and chosen in the TFS Build.

msbuild Solutionname.sln /t:Rebuild /p:Configuration=Release /p:"Platform=Mixed Platforms"

The ngen error happens on all machines that I tested, so it's not something specific to my installation.

Here is the full ngen output, even with the verbose switch:

C:\Program Files (x86)\MyProgram>ngen install MyProgram.exe /verbose
Microsoft (R) CLR Native Image Generator - Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.
Installing assembly C:\Program Files (x86)\MyProgram\MyProgram.exe
1>    Compiling assembly C:\Program Files (x86)\MyProgram\MyProgram.exe (CLR v4.0.30319) ...
1>Error compiling C:\Program Files (x86)\MyProgram\MyProgram.exe: Could not find or load a type. (Exception from HRESULT: 0x80131522)
1>    Finished compiling assembly C:\Program Files (x86)\MyProgram\MyProgram.exe ...
1>Ngen failed to generate native code for image C:\Program Files (x86)\MyProgram\MyProgram.exe because of the following error: Could not find or load a type. (Exception from HRESULT: 0x80131522)
1>Ngen will retry compilation of image C:\Program Files (x86)\MyProgram\MyProgram.exe
1>    Compiling assembly C:\Program Files (x86)\MyProgram\MyProgram.exe (CLR v4.0.30319) ...
1>Error compiling C:\Program Files (x86)\MyProgram\MyProgram.exe: Could not find or load a type. (Exception from HRESULT: 0x80131522)
1>    Finished compiling assembly C:\Program Files (x86)\MyProgram\MyProgram.exe ...
1>Uninstalling assembly C:\Program Files (x86)\MyProgram\MyProgram.exe because of an error during compilation: Could not find or load a type. (Exception from HRESULT: 0x80131522)
Could not find or load a type. (Exception from HRESULT: 0x80131522)
c#
.net
visual-studio-2010
msbuild
ngen
asked on Stack Overflow Nov 5, 2013 by cremor • edited Feb 7, 2014 by cremor

1 Answer

0

You can use Fuslogvw.exe (Assembly Binding Log Viewer) and take a look at the assemblies ngen is trying to load. That may provide a clue. You may want to capture binding logs for both cases you mentioned (msbuild and VS) and then compare them. I am guessing mbuild is introducing some dependencies not consumable by NGEN.

And when you say runtime behaviour is same, you mean the app is working as it's supposed to ? Then it must be running with IL not the Native Images. Again, you can use Fulogvw to check that.

answered on Stack Overflow Feb 7, 2014 by m_eric

User contributions licensed under CC BY-SA 3.0