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

27,144

I add this as a community wiki to help other people to solve this problem.

If you have this problem, please check this SO answer: https://stackoverflow.com/a/2926982/68571. Maybe, it could help you to solve it.

Share:
27,144
VansFannel
Author by

VansFannel

I'm software architect, entrepreneur and self-taught passionate with new technologies. At this moment I am studying a master's degree in advanced artificial intelligence and (in my free time ) I'm developing an immersive VR application with Unreal Engine. I have also interested in home automation applying what I'm learning with Udacity's nanodegree course in object and voice recognition.

Updated on July 09, 2022

Comments

  • VansFannel
    VansFannel almost 2 years

    I have an ASP.NET MVC 4 Web Api application with .NET Framework 4.0 and C# deployed on a Windows 7 Professional 64 bits and IIS 7.0.

    When I run the app, http://myHost/WebApi, I get this:

    Can not load file or assembly 'AutomationMiddleware.Common' or one of its dependencies. An attempt was made to load a program with an incorrect format. Description: An unhandled exception when running the current web request. Review the stack trace for more information about the error and where it originated in the code. Exception Details: System.BadImageFormatException: Could not load file or assembly 'AutomationMiddleware.Common' or one of its dependencies. An attempt was made to load a program with an incorrect format.

    Source code error:

    Has generated an uncontrolled during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    Trail to load the assembly: The following information can be helpful to determine why no assembly 'AutomationMiddleware.Common' was loaded.

    The administrator assemblies are loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll Running under executable file c:\windows\system32\inetsrv\w3wp.exe --- Register of detailed errors.

    === Status information prior link ===
    LOG: DisplayName = AutomationMiddleware.Common
    (Partial)
    AVS: partial link information for an assembly is provided:
    AVS: Assembly Name:. AutomationMiddleware.Common | Domain Id: 2
    AVS: A partial bond occurs when only provides part of the display name of the assembly.
    AVS: This may lead to an incorrect load linker assembly.
    AVS: It is recommended to provide a textual identity completely specified for assembly,
    AVS: consisting of simple name, version, culture, and public key token.
    AVS: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this problem.
    LOG: AppBase = file: /// C: / inetpub / wwwroot / WebAPI /
    LOG: Initial privatePath = C: \ inetpub \ wwwroot \ WebAPI \ bin Assembly Call: (Unknown). ===
    LOG: This link begins in the context of default load.
    LOG: Using the configuration file of the application: C: \ inetpub \ wwwroot \ WebAPI \ web.config
    LOG: Using host configuration file: C: \ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ aspnet.config
    LOG: Using machine configuration file in C: \ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ config \ machine.config.
    LOG: The policy is not being applied to reference at this time (link private assembly, custom, partial, or location-based).
    LOG: Attempting download of new URL file: /// C: /Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files / WebAPI / d5634960 / b33de11c / AutomationMiddleware.Common.DLL.
    LOG: Attempting download of new URL file: /// C: /Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files / WebAPI / d5634960 / b33de11c / AutomationMiddleware.Common / AutomationMiddleware.Common. DLL.
    LOG: Attempting download of new URL file: /// C: /inetpub/wwwroot/WebApi/bin/AutomationMiddleware.Common.DLL.
    ERR: Failed to complete the installation of assembly (hr = 0x8007000B). He ended the search.

    Stack trace:

    [BadImageFormatException: Could not load file or assembly 'AutomationMiddleware.Common' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
    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.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +210
    System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +242
    System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +17 System.Reflection.Assembly.Load(String assemblyString) +35
    System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +122

    [ConfigurationErrorsException: Could not load file or assembly 'AutomationMiddleware.Common' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
    System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +12495956
    System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +499 System.Web.Configuration.AssemblyInfo.get_AssemblyInternal() +131 System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +331
    System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +148
    System.Web.Compilation.BuildManager.ExecutePreAppStart() +172
    System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +1151

    [HttpException (0x80004005): Could not load file or assembly 'AutomationMiddleware.Common' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
    System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12617668 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12457285

    Microsoft .NET Framework:4.0.30319; Versión ASP.NET:4.0.30319.34237

    I have compiled every dll with x86 architecture, and the same error. With anyCPU and the same error. And with x64 and the same error.

    AutomationMiddleware.Common is an assembly that I have developed. All the assemblies are mine except the nuget ones (Entity Framework, Json.net, etc.).

    Any idea?