【问题标题】:Intermittent BadImageFormatException building "plugin" DLL on PC or via TFS build在 PC 上或通过 TFS 构建间歇性 BadImageFormatException 构建“插件”DLL
【发布时间】:2015-05-21 15:52:03
【问题描述】:

我有一个应用程序,其中几个程序集与主应用程序分开构建。在运行时,它们位于主 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,但这并没有任何区别。

我不知道还能看什么……有什么想法吗?

【问题讨论】:

    标签: c# msbuild tfsbuild msbuildcommunitytasks badimageformatexception


    【解决方案1】:

    我自己想通了。它与构建本身或目标处理器架构无关。

    程序集在安装过程中被损坏。我使用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

    【讨论】:

      猜你喜欢
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 2023-01-30
      • 2012-03-09
      • 2011-06-01
      • 1970-01-01
      • 2014-10-13
      相关资源
      最近更新 更多