BadImageFormatException Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format
Asked Answered
S

3

18

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

Stephenson answered 25/4, 2017 at 12:22 Comment(6)
One line answer - x64 and x86 version problem.Skewback
Where is the DLL coming from, though? If you get a FileNotFoundException just from changing the bitness, this seems to indicate the file isn't copied from/to where you think it is. The fact is that either the DLL is corrupt, or its bitness does not match the bitness of the process, regardless of what corflags is telling you. If necessary, turn on assembly binding logging to double check where "dacsdk" is loaded from.Jut
Thanks @JeroenMostert for giving it a right direction, can you please take a look into my updated question above? Any idea what does the error "Invalid assembly platform or ContentType in file" indicate? Why is this not working even if i change "target platform" to x86\x64.. ? Please do share your thoughts. Thanks again for the time.Stephenson
try this in your web.config in <runtime> <dependentAssembly> <assemblyIdentity name="assembly name with reference" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" /> </dependentAssembly> </runtime> replace your assembly version and replace assembly name like(System.Web.Helpers)Lissy
@Vishal, this dint work... getting same "FileNotFoundException" error again..Stephenson
Please read How to Ask, especially the part about writing good titles. The text of an exception is rarely a good title.Provo
J
22

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

Jacksmelt answered 25/4, 2017 at 15:27 Comment(4)
Thank you so much for the information about "Fusion". I have updated my question above with "Assembly Binding Logs". Can you please take a look? As this is a third party dll, i cannot recompile it. Is there any other way to look at this issue?Stephenson
Hi, provided more details.Jacksmelt
You were right!... I did contact the third party provider and they confirmed that the dcasdk dll had dependency with two more dlls which after adding resolved the issue. I will try using dependency walker, as it seems to be of really some use in cases such as this. Thanks a lot for the time and help!Stephenson
I can't believe I didn't look at the type of architecture. Dumb mistake, but this answer set me straight.Cachinnate
D
14

In VS, go to tools, then click on 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.

**enter image description here**

Dulaney answered 17/12, 2019 at 15:18 Comment(1)
This was the exact issue I had, when using IISExpress to debug locally in VisualStudio it kept throwing this error. I searched and none of my projects referenced x86 but it looks like IISExpress was. Switched it to use the 64bit version and everything worked, thank you!Leonerd
D
4

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.

For me, I was trying to load x64 dll with AnyCpu setting. I changed the AnyCpu Setting to x64 and it start working for me.

enter image description here

Dulaney answered 8/6, 2020 at 13:42 Comment(1)
This helped me solve my issue. My main project was Any CPU, but the DLL being referenced was only x86. Changing my main project to x86 removed the error.Simas

© 2022 - 2024 — McMap. All rights reserved.