简体   繁体   English

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

I am getting following runtime error, with my console application(VS2012) which refers to "dcasdk.dll".我收到以下运行时错误,我的控制台应用程序(VS2012)引用了“dcasdk.dll”。 .Net Framework of the console app is 4.5, platform target is "Any CPU".控制台应用程序的 .Net Framework 是 4.5,平台目标是“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.我使用 CorFlags 应用程序来检查 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.根据上面的信息,我认为 dll 也是使用“任何 CPU”目标构建的。 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".我还尝试将目标平台更改为 x86,这给出了以下“FileNotFoundException”。 I checked for the dll's references in reflector.我在反射器中检查了 dll 的引用。 It only shows System.Xml and System.Data.它只显示 System.Xml 和 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.下面是 Fusion 的程序集绑定日志……根据最后三行错误信息,这似乎是程序集平台的问题。 But as this is a third party dll, i cannot recompile this to any specific platform.但由于这是第三方 dll,我无法将其重新编译到任何特定平台。 Please share your thoughts on what can be done here..请分享您对这里可以做什么的想法..

Please note that i am running this console app on Windows7, 64bit OS.请注意,我在 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.

If i set "Prefer 32-bit" ON, then the error above disappears.如果我将“Prefer 32-bit”设置为 ON,那么上面的错误就会消失。 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.".但是我仍然收到“FileNotFoundException”、“无法加载文件或程序集‘dcasdk.dll’或其依赖项之一。找不到指定的模块。”。 But this time no error in Fusion logs!但是这次 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.. “FileNotFoundException”堆栈跟踪..

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

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.当我尝试将它部署为 Windows 服务器 R2 上运行的机器中的 Windows 服务时,我再次收到 FileNotFoundException。

I tried compiling with "Prefer 32" On which resulted in the "BadImageFormatException".我尝试使用“Prefer 32”进行编译,结果导致“BadImageFormatException”。

If i compile using x86 platform target i am getting FileNotFoundException, though all the dlls required are present in the service folder.如果我使用 x86 平台目标进行编译,我会收到 FileNotFoundException,尽管所有需要的 dll 都存在于服务文件夹中。

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".它在“任何 CPU,首选 32 位开启”的情况下按预期在开发系统中工作。

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如前所述,这是由于错误的体系结构 a) 在 Windows x86 中使用 x64 程序集 b) 在 x64 进程中使用 x86 程序集,反之亦然

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).为获得最佳结果,请确保所有 .NET 程序集都使用“任何 CPU”和相同的 .NET 配置文件构建(即全部使用 .NET Core、客户端配置文件或完整的 .NET)。

...or one dependency not being found at all, Enable Fusion Log to tell where is looking to gather the assemblies: ...或者根本没有找到一个依赖项,启用 Fusion Log 来告诉在哪里寻找收集程序集:

See How to enable assembly bind failure logging (Fusion) in .NET and http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx请参阅如何在 .NEThttp://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx 中启用程序集绑定失败日志记录(Fusion)

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

You may need to contact the 3rd party provider for support.您可能需要联系第 3 方提供商以获得支持。 Also, this link mentions it could be a mismatch of .NET version.此外,此链接提到它可能与 .NET 版本不匹配。

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

In that case the problem would be a dependency of dcasdk not being found.在这种情况下,问题将是找不到 dcasdk 的依赖项。 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).您可以检查哪个 Dependency Walker,请参阅http://www.dependencywalker.com/ (它还有一个配置文件模式,您可以在其中配置一个 exe,并查看打开在运行时失败的 dll 的调用)。

In VS, go to tools , then click on options .在 VS 中,转到tools ,然后单击options

Search "iis" in search bar and check this option, and run the project, this is working for me as I was getting error in web project.在搜索栏中搜索“iis”并选中此选项,然后运行该项目,这对我有用,因为我在 Web 项目中遇到错误。

**在此处输入图片描述**

Right click on project settings, then click on build and set the platform target from Any to x64 or x86 , according to your dll settings.右键单击项目设置,然后单击构建并将平台目标从 Any 设置为 x64 或 x86 ,根据您的 dll 设置。

For me, I was trying to load x64 dll with AnyCpu setting.对我来说,我试图用 AnyCpu 设置加载 x64 dll。 I changed the AnyCpu Setting to x64 and it start working for me.我将 AnyCpu 设置更改为 x64,它开始为我工作。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集xxx或其依赖项之一。 试图加载格式错误的程序。 - Could not load file or assembly xxx or one of its dependencies. An attempt was made to load a program with an incorrect format. 无法加载文件或程序集“xxx”或其依赖项之一。 试图加载格式不正确的程序 - Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format 无法加载文件或程序集“ MWArray”或其依赖项之一。 试图加载格式错误的程序 - Could not load file or assembly 'MWArray' or one of its dependencies. An attempt was made to load a program with an incorrect format 无法加载文件或程序集“...”或其依赖项之一。 试图加载格式不正确的程序 - Could not load file or assembly '...' or one of its dependencies. An attempt was made to load a program with an incorrect format 无法加载文件或程序集“sapnco”或其依赖项之一。 试图加载格式不正确的程序 - Could not load file or assembly 'sapnco' or one of its dependencies. An attempt was made to load a program with an incorrect format 无法加载文件或程序集“SharpSvn”或其依赖项之一。 尝试加载格式不正确的程序 - Could not load file or assembly 'SharpSvn' or one of its dependencies. An attempt was made to load a program with an incorrect format 无法加载文件或程序集“ MyLibrary”或其依赖项之一。 试图加载格式错误的程序 - Could not load file or assembly 'MyLibrary' or one of its dependencies. An attempt was made to load a program with an incorrect format 无法加载文件或程序集“ Oracle.DataAccess”或其依赖项之一。 试图加载格式错误的程序。 缓存 - Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format. Ncache 无法加载文件或程序集“ myApp.Web”或其依赖项之一。 试图加载格式错误的程序 - Could not load file or assembly 'myApp.Web' or one of its dependencies. An attempt was made to load a program with an incorrect format 无法加载文件或程序集“JsonFx.Json”或其依赖项之一。 尝试加载格式不正确的程序 - Could not load file or assembly 'JsonFx.Json' or one of its dependencies. An attempt was made to load a program with an incorrect format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM