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

  • Question

  • I'm stuck with another "Could not load file or assembly 'x' or one of its dependencies. An attempt was made to load a program with an incorrect format. " exception.  The last time was, I think, caused by a Reference to PresentationCore which was set to Copy Local: True.  In fact, no referenced assemblies have Copy Local: True; this time it's something else. 

    Anyone got any ideas what it might be.
    Tuesday, October 6, 2009 4:16 PM

Answers

  • The BadImageFormatException was caused by two things:

    1. the site is on an R2 server, so x64 and the Application Pool's default setting for Enable 32-Bit Applications was False.
    2. the assembly uses an assembly which uses COM Interop which is one of the things listed here: http://msdn.microsoft.com/en-us/library/system.badimageformatexception.aspx as a cause of this exception when run as 64-bit.

    Since I can't get rid of the Interop, I fixed it by switching the App Pool to 32-bit mode.

    • Marked as answer by SSG31415926 Tuesday, October 6, 2009 5:37 PM
    Tuesday, October 6, 2009 5:37 PM

All replies

  • The BadImageFormatException was caused by two things:

    1. the site is on an R2 server, so x64 and the Application Pool's default setting for Enable 32-Bit Applications was False.
    2. the assembly uses an assembly which uses COM Interop which is one of the things listed here: http://msdn.microsoft.com/en-us/library/system.badimageformatexception.aspx as a cause of this exception when run as 64-bit.

    Since I can't get rid of the Interop, I fixed it by switching the App Pool to 32-bit mode.

    • Marked as answer by SSG31415926 Tuesday, October 6, 2009 5:37 PM
    Tuesday, October 6, 2009 5:37 PM
  • Not exactly true!  I was getting the same error, and my web servers are running x64 for ALL application, "Enable 32-Bit Applications" is set to FALSE, and I am using a 32-bit COM third party object.

    Everything was working fine, until I upgraded to VS2010 an TFS2010, and then I got the error message.  The fix was this:

    1.  Changed the project setting "Target CPU" to "x86"  (It was originally ANYCPU) I checked the solution in.

    2.  I then changed th project setting "Target CPU" to "ANYCPU", and it WORKED.

    When you look at the project file to compare the differences between the original version and the one that works, there is only one difference:  The working project file has "<PlatformTarget>AnyCPU</PlatformTarget>", where the old one has NOTHING AT ALL.  (No entry for "PlatformTarget")

    It looks like TFS2010 needs that tag in order to compile it the way you want.  Anyways, one line of text in the project file fixed my problem.  ("<PlatformTarget>AnyCPU</PlatformTarget>")

    The only other noteworthy mention is that I did not upgrade the project to .NET 4.0; it is still 3.5.

    Forch

    • Proposed as answer by Forch Friday, April 16, 2010 5:13 PM
    Friday, April 16, 2010 5:13 PM