【问题标题】:How to debug a TypeLoadException during ngen如何在 ngen 期间调试 TypeLoadException
【发布时间】:2013-11-05 14:16:54
【问题描述】:

情况

我在安装过程中使用 ngen 创建我的程序集的本机映像。直到最近这才正常工作,我不知道是什么破坏了它。遗憾的是,ngen 输出并不是很有帮助。它只是说它无法找到或加载一个类型,但没有说明是哪种类型。

非常奇怪的是,只有当我的程序集是使用 msbuild 构建时(由 TFS Team Build 或我自己在命令行中运行),ngen 才会失败,但如果我使用 Visual Studio 2010 构建它,ngen 工作正常!我已经从 .NET 4.0(我的本地机器)、.NET 4.5(TFS 2012 Update 3)和 .NET 4.5.1(TFS 2013)尝试了 msbuild,都在 ngen 中导致了相同的错误。它只是 Visual Studio 2010 GUI 中的构建有效。但是错误只发生在 ngen 中,运行时行为是相同的 - 它回退到 IL 程序集,但工作正常。

我什至尝试调试 ngen.exe 和 mscorsvw.exe,但我只能看到引发了一些本机异常,其中之一是 EETypeLoadException,但我无法获得有关异常的其他信息。但是在调试原生应用程序方面我一点经验都没有。

问题

有谁知道我如何获得有关 ngen 错误的更多信息?导致错误的类型的名称会很好。

我已经在某处读到 Visual Studio 不只是调用 msbuild 进行构建,但为什么会有这么大的差异?

其他信息

TFS 在自动构建期间运行 msbuild。没有配置额外的 msbuild 参数,并且构建过程模板是 TFS 2012 Update 3 中的默认模板。

当我自己运行 msbuild 时,我在 Visual Studio 中也使用的同一文件和文件夹上使用以下命令。配置和平台也是 Visual Studio 中的活动并在 TFS 构建中选择。

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

ngen 错误发生在我测试的所有机器上,所以它不是我的安装所特有的。

这是完整的 ngen 输出,即使使用了详细开关:

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)

【问题讨论】:

  • 如果有人遇到同样的问题:在客户端机器(运行 ngen 的机器)上安装 .NET Framework 4.5.1 为我解决了问题。

标签: c# .net visual-studio-2010 msbuild ngen


【解决方案1】:

您可以使用 Fuslogvw.exe(程序集绑定日志查看器)查看 ngen 尝试加载的程序集。这可能会提供一个线索。您可能希望为您提到的两种情况(msbuild 和 VS)捕获绑定日志,然后进行比较。我猜 mbuild 正在引入一些 NGEN 无法使用的依赖项。

当您说运行时行为相同时,您的意思是应用程序正在按预期运行?然后它必须与 IL 而不是本机图像一起运行。同样,您可以使用 Fulogvw 进行检查。

【讨论】:

  • 是的,当我启动在 Ngen 中失败的应用程序时,它会退回到 IL 程序集。这就是我首先发现问题的方式,因为我检查了所有程序集在运行时是否使用本机图像。有时间我会尝试找出 Ngen 加载行为的差异,感谢您的建议!
猜你喜欢
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多