【发布时间】:2017-09-22 11:03:38
【问题描述】:
我的控制台应用程序 (VS2012) 引用“dcasdk.dll”时出现以下运行时错误。 控制台应用的 .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.
我使用 CorFlags 应用程序来检查 dll 的目标平台。下面是详细内容。。
Version : v4.0.30319
CLR Header : 2.5
PE : PE32
CorFlags : 16
ILONLY : 0
32BIT : 0
Signed : 0
根据上面的信息,我认为 dll 也是使用“任何 CPU”目标构建的。因此它应该与具有相同目标平台的控制台应用程序一起使用。我不知道为什么我在运行时会收到此错误。
我还尝试将目标平台更改为 x86,这会给出以下“FileNotFoundException”。我在反射器中检查了 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 的组装绑定日志。根据最后三行错误信息,这似乎是组装平台的问题。但由于这是第三方 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”堆栈跟踪..
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()
问题和修复:
原来是找不到 dcasdk dll 的依赖项的问题。我确实联系了第三方提供商,他们确认 dcasdk dll 依赖于另外两个 dll(由他们稍后提供),添加后解决了问题!
感谢大家帮助我理解这个问题。
当我尝试将其部署为在 Windows 服务器 R2 上运行的机器中的 Windows 服务时,我再次收到 FileNotFoundException。
我尝试使用“Prefer 32”进行编译,结果出现“BadImageFormatException”。
如果我使用 x86 平台目标进行编译,我会收到 FileNotFoundException,尽管所有需要的 dll 都存在于服务文件夹中。
谁能帮助我了解部署出了什么问题。它可以在开发系统中按预期使用“任何 CPU,首选 32 位开启”。
【问题讨论】:
-
一行回答——x64和x86版本问题。
-
DLL 是从哪里来的呢?如果您仅通过更改位数获得
FileNotFoundException,这似乎表明该文件未从您认为的位置复制到您认为的位置。事实是 DLL 已损坏,或者它的位数与进程的位数不匹配,无论 corflags 告诉您什么。如有必要,打开程序集绑定日志记录以仔细检查“dacsdk”的加载位置。 -
感谢@JeroenMostert 给它一个正确的方向,你能看看我上面更新的问题吗?知道错误“无效的程序集平台或文件中的 ContentType”表示什么吗?为什么即使我将“目标平台”更改为 x86\x64.. 也不起作用?请分享你的想法。再次感谢您的时间。
-
在
替换您的程序集版本并替换程序集名称,如(System.Web.Helpers) -
@Vishal,这很有效...再次出现相同的“FileNotFoundException”错误..
标签: c# .net .net-assembly badimageformatexception target-platform