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

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

问题描述

我的控制台应用程序(VS2012)引用了 dcasdk.dll,因此出现运行时错误。
控制台应用程序的.Net Framework为4.5,平台目标为任何CPU。

 无法加载文件或程序集 dcasdk,版本= 1.0.1.0,文化=中性,PublicKeyToken =空或其依赖项之一。试图加载格式错误的程序。 

我使用CorFlags应用程序检查dll的目标平台。

 版本:v4.0.30319 
CLR标头:2.5
PE:PE32
CorFlags:16
ILONLY:0
32BIT:0
Signed:0

根据上面的信息,我认为该dll也使用任何CPU目标构建。因此,它应与具有相同目标平台的控制台应用程序一起使用。我不确定为什么我在运行时会收到此错误。



我还尝试将目标平台更改为x86,从而得到以下 FileNotFoundException。我在反射器中检查了dll的引用。它仅显示System.Xml和System.Data。

 无法加载文件或程序集 dcasdk.dll或以下项之一: b它的依赖关系。指定的模块无法找到。 

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



预先感谢。



下面是来自Fusion。的Assembly Binding Log。根据最后三行错误信息,这似乎是Assembly的问题平台。但是由于这是第三方dll,因此我无法将其重新编译到任何特定平台。请在这里分享您的想法。



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

  ***程序集活页夹日志条目(4/26/2017 @ 8:31:08 AM)*** 

操作失败了
绑定结果:hr = 0x8007000b。试图加载格式错误的程序。

程序集管理器从以下位置加载:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
在可执行文件C:\ConsoleApplication3\下运行ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe
---详细的错误日志如下。

===绑定前状态信息===
日志:DisplayName = dcasdk,Version = 1.0.1.0,Culture = neutral,PublicKeyToken = null
(完全指定)
日志:Appbase = file:/// C:/ ConsoleApplication3 / ConsoleApplication3 / bin / Debug /
LOG:初始PrivatePath = NULL
LOG:动态库= NULL
LOG :缓存基= NULL
日志:AppName = ConsoleApplication3.vshost.exe
调用程序集:ConsoleApplication3,版本= 1.0.0.0,文化=中性,PublicKeyToken =空。
===
日志:此绑定在默认加载上下文中启动。
日志:使用应用程序配置文件:C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe.Config
日志:使用主机配置文件:
LOG :使用来自C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config的机器配置文件。
LOG:目前未将策略应用于引用(私有,自定义,部分或基于位置的程序集绑定)。
日志:尝试下载新的URL文件:/// C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/dcasdk.DLL。
日志:程序集下载成功。尝试设置文件:C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll
日志:进入源运行设置阶段。
日志:程序集名称为:dcasdk,版本= 1.0.1.0,区域性=中性,PublicKeyToken =空
ERR:文件中的汇编程序平台或ContentType无效(hr = 0x8007000b)。
ERR:从源运行设置阶段失败,hr = 0x8007000b。
ERR:无法完成程序集的设置(hr = 0x8007000b)。探测终止。

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

  *** Assembly Binder日志条目(4/26/2017 @ 9:57 :53 AM)*** 

操作成功。
绑定结果:hr = 0x0。操作成功完成。

程序集管理器从以下位置加载:C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
在可执行文件C:\ConsoleApplication3下运行ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe
---详细的错误日志如下。

===绑定前状态信息===
日志:DisplayName = dcasdk,Version = 1.0.1.0,Culture = neutral,PublicKeyToken = null
(完全指定)
日志:Appbase = file:/// C:/ ConsoleApplication3 / ConsoleApplication3 / bin / Debug /
LOG:初始PrivatePath = NULL
LOG:动态库= NULL
LOG :缓存基= NULL
日志:AppName = ConsoleApplication3.vshost.exe
调用程序集:ConsoleApplication3,版本= 1.0.0.0,文化=中性,PublicKeyToken =空。
===
日志:此绑定在默认加载上下文中启动。
日志:使用应用程序配置文件:C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe.Config
日志:使用主机配置文件:
LOG :使用C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config中的机器配置文件。
LOG:目前未将策略应用于引用(私有,自定义,部分或基于位置的程序集绑定)。
日志:尝试下载新的URL文件:/// C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/dcasdk.DLL。
日志:程序集下载成功。尝试设置文件:C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll
日志:进入源运行设置阶段。
日志:程序集名称是:dcasdk,版本= 1.0.1.0,区域性=中性,PublicKeyToken =空
日志:绑定成功。从C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll返回程序集。
日志:程序集在默认加载上下文中加载。

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(对象状态)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading。 ExecutionContextexecutionContext,System.Threading.ContextCallback回调,对象状态,布尔值saveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executeContext,System.Threading.ContextCallback回调,对象状态,布尔值prepareSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executeContext,System.Threading.ContextCallback回调,对象状态)
mscorlib.dll! System.Threading.ThreadHelper.ThreadStart()






问题并修复:



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



谢谢大家为帮助我理解问题。






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



我尝试使用 Prefer 32进行编译,从而导致 BadImageFormatException。



如果我使用x86平台目标进行编译,虽然服务文件夹中存在所有必需的dll,但我正在获取FileNotFoundException。



有人可以帮我了解最新情况吗部署有误。

解决方案

如上所述,它可以在开发系统中按预期方式在任何CPU,首选32bit ON的情况下工作。错误的体系结构
a)在Windows x86上使用x64程序集
b)在x64进程上使用x86程序集,反之亦然



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



...或根本找不到一个依赖项,启用Fusion Log可以告诉正在寻找集合的地方:



请参阅如何在.NET中启用程序集绑定失败日志记录(融合)

http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoade rErrors.aspx



更新:鉴于错误代码0x8007000b,我可以肯定是体系结构不匹配:
a)dll可能是Full .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的调用。)


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.

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

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.

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.

Thanks in advance.

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..

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:\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.

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:\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()


The ISSUE and fix:

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.


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

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

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

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".

解决方案

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

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).

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

See How to enable assembly bind failure logging (Fusion) in .NET and http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx

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.

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

Anyway, looks like the problem is narrowed.

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)

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天全站免登陆