The module was expected to contain an assembly manifest

41

I have a project in c# that uses c++ library and some dlls. When I run it it works great.

The library is Awesomium, contains both c++ library and c# wrappers

When I create reference to this project from my ASP.NET MVC 4 project I get the following error:

Exception Details: System.BadImageFormatException: Could not load file or assembly 'file:///D:\Projects\Development\ProGamers\GamingSite\bin\avcodec-52.dll' or one of its dependencies. The module was expected to contain an assembly manifest.

Things I've tried:

  1. Coping the dlls, but it still doesn't work (The file exists).

  2. Registering it in the GAC but I've got the same error there.

  3. Make sure using .NET Framework 4.0 (and not client profile)

  4. 2 files with the same dll/exe name causing conflict - I dont have it.

  5. Change the platform of the solution to AnyCPU/Mixed Platforms.

Stack Trace:

[BadImageFormatException: Could not load file or assembly 'file:///D:\Projects\Development\ProGamers\GamingSite\bin\avcodec-52.dll' or one of its dependencies. The module was expected to contain an assembly manifest.]
   System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
   System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +34
   System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
   System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark) +102
   System.Reflection.Assembly.LoadFrom(String assemblyFile) +34
   WebActivator.ActivationManager.Run() +190

[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load file or assembly 'file:///D:\Projects\Development\ProGamers\GamingSite\bin\avcodec-52.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +550
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath) +90
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +135
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load file or assembly 'file:///D:\Projects\Development\ProGamers\GamingSite\bin\avcodec-52.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9850940
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +456
c#
badimageformatexception
asked on Stack Overflow Sep 1, 2012 by Aviran Cohen • edited Sep 2, 2012 by Aviran Cohen

7 Answers

40

I found that, I am using a different InstallUtil from my target .NET Framework. I am building a .NET Framework 4.5, meanwhile the error occured if I am using the .NET Framework 2.0 release. Having use the right InstallUtil for my target .NET Framework, solved this problem!

answered on Stack Overflow Jan 22, 2013 by swdev • edited Jan 22, 2013 by swdev
12

BadImageFormatException, in my experience, is almost always to do with x86 versus x64 compiled assemblies. It sounds like your C++ assembly is compiled for x86 and you are running on an x64 process. Is that correct?

Instead of using AnyCPU/Mixed as the platform. Try to manually set it to x86 and see if it will run after that.

Hope this helps.

answered on Stack Overflow Sep 1, 2012 by Davin Tryon
1

I found another strange reason and i thought maybe another developer confused as me. I did run install.bat that created to install my service in developer Command Prompt of VS2010 but my service generated in VS2012. it was going to this error and drives me to crazy but i try VS2012 Developer Command Prompt tools and everything gone to be OK. I don't no why but my problem was solved. so you can test it and if anyone know reason of that please share with us. Thanks.

answered on Stack Overflow Mar 26, 2014 by QMaster
1

Check if the manifest is a valid xml file. I had the same problem by doing a DOS copy command at the end of the build, and it turns out that for some reason I can not understand "copy" was adding a strange character (->) at the end of the manifest files. The problem was solved by adding "/b" switch to force binary copy.

answered on Stack Overflow Aug 25, 2016 by Pandele Florin
1

First try to open the file with a decompiler such as ILSpy, your dll might be corrupt. I had this error on an online web site, when I downloaded the dll and tried to open it, it was corrupt, probably some error occurred while uploading it via ftp.

answered on Stack Overflow Jul 2, 2017 by BornToCode
1

I got the error in the following case:

  1. A .NET EXE file (#1) was referencing another .NET EXE file (#2)
  2. I was trying to obfuscate EXE #2 with Themida
  3. when running the EXE #1 it was crashing with the error "The module was expected to contain an assembly manifest"

I simply avoided the obfuscation and the error is gone. not a real solution, but at least I know what caused it...

answered on Stack Overflow Jun 16, 2019 by itsho
0

In my case, I was using InstallUtil.exe which was causing an error. To install the .Net Core service in window best way to use sc command.

More information here Exe installation throwing error The module was expected to contain an assembly manifest .Net Core

answered on Stack Overflow Jun 8, 2020 by R15

User contributions licensed under CC BY-SA 3.0