间歇性BadImageFormatException构建插件;DLL在PC上或通过TFS构建

本文关键字:构建 PC TFS DLL BadImageFormatException 插件 | 更新日期: 2023-09-27 17:50:27

我有一个应用程序,其中几个程序集是与主应用程序分开构建的。在运行时,它们位于主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更新"插件"程序集项目用作引用的程序集
  • 同样,但是在我的PC上构建了新的dll
  • 重启构建服务器
  • 在构建服务器上安装Windows更新
  • 回家第二天发现问题不见了!

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

到目前为止,主要应用程序和插件解决方案的构建配置都针对"任意CPU"。今天(在看了很多关于这个异常的处理器架构的S/O帖子之后),我尝试切换到目标x64,但这没有任何区别。

我不知道还能看什么…什么好主意吗?

间歇性BadImageFormatException构建插件;DLL在PC上或通过TFS构建

我自己想出来的。它与构建本身无关,也与目标处理器体系结构无关。

程序集在安装过程中被损坏。我使用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/#!主题/msbuildtasks txBNNAWNwYo