BadImageFormatException无法加载文件或程序集或其依赖项之一。 尝试加载格式不正确的程序

我的控制台应用程序(VS2012)引用了“dcasdk.dll”,我遇到了运行时错误。 控制台应用程序的.Net Framework是4.5,平台目标是“任何CPU”。

Could not load file or assembly 'dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format. 

我使用CorFlags应用程序来检查dll的目标平台。 以下是详细信息..

 Version : v4.0.30319 CLR Header : 2.5 PE : PE32 CorFlags : 16 ILONLY : 0 32BIT : 0 Signed : 0 

根据上面的信息,我认为dll也是使用“Any CPU”目标构建的。 所以它应该与具有相同目标平台的控制台应用程序一起使用。 我不知道为什么我跑的时候会出现这个错误。

我也尝试将目标平台更改为x86,它提供了以下“FileNotFoundException”。 我在reflection器中检查了dll的参考。 它只显示System.Xml和System.Data。 这些已添加为控制台应用程序中的参考。

 Could not load file or assembly 'dcasdk.dll' or one of its dependencies. The specified module could not be found. 

任何有关这方面的帮助将不胜感激。

提前致谢。

下面是Fusion的assembly绑定日志..根据最后三行错误信息,这似乎是assembly平台的问题。 但由于这是第三方dll,我无法将其重新编译到任何特定平台。 请分享您对这里可以做些什么的想法..

请注意,我在Windows7,64位操作系统上运行此控制台应用程序。

 *** Assembly Binder Log Entry (4/26/2017 @ 8:31:08 AM) *** The operation failed. Bind result: hr = 0x8007000b. An attempt was made to load a program with an incorrect format. Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll Running under executable C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe --- A detailed error log follows. === Pre-bind state information === LOG: DisplayName = dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null (Fully-specified) LOG: Appbase = file:///C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/ LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = NULL LOG: AppName = ConsoleApplication3.vshost.exe Calling assembly : ConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Attempting download of new URL file:///C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/dcasdk.DLL. LOG: Assembly download was successful. Attempting setup of file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll LOG: Entering run-from-source setup phase. LOG: Assembly Name is: dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null ERR: Invalid assembly platform or ContentType in file (hr = 0x8007000b). ERR: Run-from-source setup phase failed with hr = 0x8007000b. ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated. 

如果我将“首选32位”设置为ON,则上述错误消失。 但是我仍然得到“FileNotFoundException”,“无法加载文件或程序集’dcasdk.dll’或其依赖项之一。无法找到指定的模块。” 但这次Fusion日志没有错误!

 *** Assembly Binder Log Entry (4/26/2017 @ 9:57:53 AM) *** The operation was successful. Bind result: hr = 0x0. The operation completed successfully. Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll Running under executable C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe --- A detailed error log follows. === Pre-bind state information === LOG: DisplayName = dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null (Fully-specified) LOG: Appbase = file:///C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/ LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = NULL LOG: AppName = ConsoleApplication3.vshost.exe Calling assembly : ConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Attempting download of new URL file:///C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/dcasdk.DLL. LOG: Assembly download was successful. Attempting setup of file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll LOG: Entering run-from-source setup phase. LOG: Assembly Name is: dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null LOG: Binding succeeds. Returns assembly from C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll. LOG: Assembly is loaded in default load context. 

“FileNotFoundException”stacktrace ..

 mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() 

问题和修复:

事实certificate这是dcasdk dll的依赖关系未找到的问题。 我确实联系了第三方提供商,他们确认dcasdk dll依赖于另外两个dll(后来由他们提供),这些dll在添加后解决了问题!

谢谢大家帮助我理解这个问题。


当我尝试将其部署为在Windows服务器R2上运行的计算机中的Windows服务时,我再次收到FileNotFoundException。

我尝试使用“Prefer 32”编译,导致“BadImageFormatException”。

如果我使用x86平台目标编译我得到FileNotFoundException,尽管所需的所有dll都存在于服务文件夹中。

有人可以帮我理解部署出了什么问题。 它可以在开发系统中使用“Any CPU,Prefer 32bit ON”。

如前所述,这是由于错误的架构a)使用x64程序集与Windows x86 b)使用x86程序集与x64进程或反之亦然

为了获得最佳结果,请确保所有.NET程序集都使用“Any CPU”和相同的.NET配置文件构建(即所有.NET程序集都使用.NET Core,或Client Profile或Full .NET)。

…或者根本找不到一个依赖项,启用Fusion Log以告知收集程序集的位置:

请参见如何在.NET中启用程序集绑定失败日志记录(Fusion)和http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx

更新:给出错误代码0x8007000b我很确定架构不匹配:a)dll可能是完整的.NET代码,但不能用AnyCPU编译b)dll可能是本机代码,然后你需要一个匹配的架构(加上一些人使用PInvoke调用它的代码)c)dll可能是C ++ CLI(本机/ .NET代码的混合,再次是错误的架构)。 d)dll可能已损坏。

您可能需要与第三方提供商联系以获取支持。 此外,此链接提到它可能是.NET版本的不匹配。

无论如何,看起来问题缩小了。

此外,如果DLL部分是本机的,它可能需要MSVC运行时(因为这个问题的答案提到, 在64位系统上使用32位DLL显示0x8007000B错误 )

在这种情况下,问题将是dcasdk未被发现的依赖性。 您可以查看哪个Dependency Walker,请参阅http://www.dependencywalker.com/ (它还有一个用于分析exe的配置文件模式,并查看打开在运行时失败的dll的调用)。