我有一个应用程序,其中几个程序集与主应用程序分开构建。在运行时,它们位于主 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,但这并没有任何区别。
我不知道还能看什么……有什么想法吗?