BadImageFormatException 无法加载文件或程序集或其依赖项之一.试图加载格式不正确的程序 [英] BadImageFormatException Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format

查看:29
本文介绍了BadImageFormatException 无法加载文件或程序集或其依赖项之一.试图加载格式不正确的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制台应用程序 (VS2012) 引用dcasdk.dll"时出现以下运行时错误.控制台应用程序的 .Net Framework 为 4.5,平台目标为Any CPU".

I am getting following runtime error, with my console application(VS2012) which refers to "dcasdk.dll". .Net Framework of the console app is 4.5, platform target is "Any 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 的目标平台.详情如下..

I used CorFlags application to check the target platform of the dll. Below are the details..

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

根据上面的信息,我认为 dll 也是使用Any CPU"目标构建的.所以它应该与具有相同目标平台的控制台应用程序一起使用.我不知道为什么我在运行时会收到这个错误.

As per the information above, i think the dll is also built using "Any CPU" target. So it should work with the console app having the same target platform. I am not sure why i get this error when i run.

我还尝试将目标平台更改为 x86,这给出了以下FileNotFoundException".我在反射器中检查了 dll 的引用.它只显示 System.Xml 和 System.Data.已在控制台应用中添加为参考.

I also tried changing the target platform to x86 which gives the following "FileNotFoundException". I checked for the dll's references in reflector. It only shows System.Xml and System.Data. Which are already added as a reference in the console app.

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

对此的任何帮助将不胜感激.

Any help regarding this will be greatly appreciated.

提前致谢.

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

Below is the Assembly Binding Log from Fusion.. as per the last three lines of error information, this seems to be an issue with assembly platform. But as this is a third party dll, i cannot recompile this to any specific platform. Please share your thoughts on what can be done here..

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

Please note that i am running this console app on Windows7, 64bit OS.

*** 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:WindowsMicrosoft.NETFramework64v4.0.30319clr.dll
Running under executable  C:ConsoleApplication3ConsoleApplication3inDebugConsoleApplication3.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:ConsoleApplication3ConsoleApplication3inDebugConsoleApplication3.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFramework64v4.0.30319configmachine.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:ConsoleApplication3ConsoleApplication3inDebugdcasdk.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.

如果我将Prefer 32-bit"设置为ON,那么上面的错误就会消失.但是我仍然收到FileNotFoundException"、无法加载文件或程序集‘dcasdk.dll’或其依赖项之一.找不到指定的模块.".但是这次 Fusion 日志没有错误!

If i set "Prefer 32-bit" ON, then the error above disappears. However i still get "FileNotFoundException", "Could not load file or assembly 'dcasdk.dll' or one of its dependencies. The specified module could not be found.". But this time no error in Fusion logs!

*** 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:WindowsMicrosoft.NETFrameworkv4.0.30319clr.dll
Running under executable  C:ConsoleApplication3ConsoleApplication3inDebugConsoleApplication3.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:ConsoleApplication3ConsoleApplication3inDebugConsoleApplication3.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.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:ConsoleApplication3ConsoleApplication3inDebugdcasdk.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:ConsoleApplication3ConsoleApplication3inDebugdcasdk.dll.
LOG: Assembly is loaded in default load context.

FileNotFoundException"堆栈跟踪..

"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()

<小时>

问题和修复:


The ISSUE and fix:

原来是找不到 dcasdk dll 的依赖项的问题.我确实联系了第三方提供商,他们确认 dcasdk dll 依赖于另外两个 dll(由他们稍后提供),添加后解决了问题!

It turned out to be an issue of dcasdk dll's dependencies not being found. I did contact the third party provider and they confirmed that the dcasdk dll had dependency with two more dlls(provided by them later) which after adding resolved the issue!

感谢大家帮助我理解问题.

Thanks everyone for helping me understand the issue.

当我尝试将它部署为 Windows 服务器 R2 上运行的机器中的 Windows 服务时,我再次收到 FileNotFoundException.

I am getting FileNotFoundException again when i try to deploy this as a windows service in a machine runs on Windows server R2.

我尝试使用Prefer 32"进行编译,结果导致BadImageFormatException".

I tried compiling with "Prefer 32" On which resulted in the "BadImageFormatException".

如果我使用 x86 平台目标进行编译,我会收到 FileNotFoundException,尽管服务文件夹中存在所有所需的 dll.

If i compile using x86 platform target i am getting FileNotFoundException, though all the dlls required are present in the service folder.

谁能帮我了解部署出了什么问题.它在任何 CPU,首选 32 位开启"的情况下按预期在开发系统中工作.

Can someone help me understand whats going wrong with the deployment. It works in the development system as expected with "Any CPU, Prefer 32bit ON".

推荐答案

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

As mentioned already it is due to wrong architecture either a) Using x64 assembly with Windows x86 b) Using x86 assembly with x64 process or viceversa

为获得最佳结果,请确保所有 .NET 程序集均使用任何 CPU"和相同的 .NET 配置文件构建(即全部使用 .NET Core、客户端配置文件或完整的 .NET).

For best results, ensure all .NET assemblies are built with "Any CPU", and same .NET profile (ie all using .NET Core, or Client Profile or Full .NET).

...或者根本没有找到一个依赖项,启用 Fusion Log 以告知在哪里收集程序集:

...or one dependency not being found at all, Enable Fusion Log to tell where is looking to gather the assemblies:

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

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

UPDATE: Given the error code 0x8007000b I am pretty sure is an architecture mismatch: a) The dll may be Full .NET code, but not compiled with AnyCPU b) The dll may be native code, and then you need a matching architecture (plus some calling its code using PInvoke) c) The dll may be C++ CLI (mix of native/.NET code, again with wrong architecture). d) The dll may be corrupt.

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

You may need to contact the 3rd party provider for support. Also, this link mentions it could be a mismatch of .NET version.

总之,问题缩小了.

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

Also if the DLL is partly native, it may need the MSVC runtime (as this question's answers mentions, Using 32-bit dll on 64-bit system shows 0x8007000B Error)

在这种情况下,问题将是找不到 dcasdk 的依赖项.可以查看是哪个 Dependency Walker,参见 http://www.dependencywalker.com/(它还有一个配置文件模式,您可以在其中配置一个 exe,并查看打开在运行时失败的 dll 的调用).

In that case the problem would be a dependency of dcasdk not being found. You can check which Dependency Walker, see http://www.dependencywalker.com/ (it also has a profile mode where you profile an exe, and see the call to open a dll which failed at runtime).

这篇关于BadImageFormatException 无法加载文件或程序集或其依赖项之一.试图加载格式不正确的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆