间歇性BadImageFormatException在PC上或通过TFS构建

时间:2015-05-21 15:52:04

标签: c# msbuild tfsbuild msbuildcommunitytasks badimageformatexception

我有一个应用程序,其中几个程序集与主应用程序分开构建。在运行时,它们位于主BIN文件夹之外的文件夹中,并由主应用程序使用Assembly.LoadFrom加载。在这些“插件”程序集的VS项目中,引用了从主应用程序构建的物理程序集。

主应用程序和插件的构建都是由TFS构建服务器构建的。由“插件”程序集项目用作引用的程序集通常取自主应用程序的TFS构建,但偶尔会从我的PC上构建的程序中获取。

我每天都在处理这个应用程序,99%的插件构建工作正常。然后没有任何明显的解释,主应用程序将无法加载插件程序集,并出现以下错误:

System.BadImageFormatException: Could not load file or assembly 'file:///C:\Program Files\MyCompany\MainApp\Plugins\MyApp.Plugins.ABC.XYZ.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
File name: 'file:///C:\Program Files\MyCompany\MainApp\Plugins\MyApp.Plugins.ABC.XYZ.dll'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)

当发生这种情况时,我会尝试各种方法来解决它,但我没有可靠的解决方案。我尝试以下所有内容,最终问题消失了:

  • 再次运行构建
  • 使用构建服务器构建的新DLL更新“插件”程序集项目用作引用的程序集
  • 同上,但在我的电脑上构建了新的DLL
  • 重新启动构建服务器
  • 在构建服务器上安装Windows更新
  • 回家,第二天发现问题已经消失了!

有时,当构建服务器构建的插件构建显示此问题时,在我的PC上进行的构建将起作用。其他时候,两个版本都会显示问题。

到目前为止,主应用程序和插件解决方案的构建配置都针对“任何CPU”。今天(看似很多S / O帖子将此异常与目标处理器架构相关联之后)我尝试将两者都切换到目标x64,但这没有任何区别。

我不知道还有什么要看......有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我自己想通了。它与构建本身无关,或者与哪个处理器架构有针对性。

安装过程中程序集已损坏。我使用MSBUILD Community Tasks将插件程序集压缩为ZIP文件以进行分发。虽然任务成功完成,并且程序集可以解压缩,但手动检查包含无法加载的插件的ZIP文件会显示它们有CRC错误。

解决方案是在ZIP任务中使用ParallelCompression =“false”,例如

<Zip Files="@(PackageAssemblies);@(PackageAssemblyPDBs);@(PackageDocuments);@(PackageOtherFiles)" Flatten="True" WorkingDirectory="$(MSBuildProjectDirectory)" ZipFileName="$(PackageOutputFolder)\$(PackageName)_$(Major).$(Minor).$(Revision)_Install.zip" ParallelCompression="false" />

在此处找到线索:https://groups.google.com/forum/#!topic/msbuildtasks/txBNNAWNwYo