0

I published a small web application with WCF services locally by using a File System in Visual Studio Community 2017. I hosted it locally to test it and I noticed that a single WCF service was not called and raised a System.BadImageFormatException. The reason was a single id variable from a method of Int32 type. The solution platform in the Configuration Manager, is a "Mixed Platforms" with Any CPU. So the problem was not there. I followed the solution by bluwater2001, here by enabling 32 bit applications in the Application pool. However my question remains: Why a 32-bit variable caused such an error whereas my web app was built for any cpu and mixed platforms? And why was it necessary to enable 32-bit applications in the application pool in IIS?

SoftDev30_15
  • 463
  • 7
  • 20
  • 3
    _"The reason was a single id variable from a method of Int32 type"_ - no. Your application was running as 64 bit and was loading an assembly that was 32 bit. – CodeCaster Dec 05 '18 at 09:24
  • 1
    The BadImageFormatException must be also telling you which dll its not able to load up, use fusion log viewer to see from where the assembly is trying to get loaded. – peeyush singh Dec 05 '18 at 09:40
  • It really depends on the context that gave you the exception, as `BadImageFormatException` can occur when root causes vary. "So the problem was not there" is not true either. You only checked your own projects, but a typical project can have multiple dependencies, all of which can trigger such exceptions if the root cause is bitness mismatch (bitness mismatch is only one of the possible root causes, and probably the simplest to understand and troubleshoot). So do more investigation on your side, and edit the question to include the full exception call stack and other important information. – Lex Li Dec 05 '18 at 18:44

1 Answers1

0

This has nothing to do with the type of your variables/parameters.

If you choose AnyCPU your code can run on any platform. It will compile into 32bit code on a 32bit machine and into 64bit code on 64bit machine.

If your assembly is running as a 32bit process it cannot load a 64bit assembly and vice versa. If it tries to load a assembly which doesn't match the process you get a BadImageFormatException.

Mardukar
  • 415
  • 2
  • 11
  • Bitness mismatch is only one of the common causes of `BadImageFormatException`. So it is too early to draw a conclusion like yours. – Lex Li Dec 05 '18 at 18:46