Enabling "32-bit Applications" in IIS starts the x86 version of the w3wp process.
AnyCPU is a handy feature, provided by .NET, do decide on application startup which processor architecture to choose. This is possible, because .NET applications do not compile to x86/x64 machine code, but to Intermediate Language. When starting the application it compiles behind the scenes to either x64 or x86 machine code.
However, non .NET applications usually compile directly to machine code of the target system. Also COM servers must be registered in different registry branches for x86 and x64.
Since you have only a 32-bit compatible server (DLL) this leads to the situations you described:
- When launching your console application with AnyCPU it compiles to x86 or x64, based on the architecture your system uses. After this, the references get loaded. If you are on x64, the process cannot find your COM factory for the server. Otherwise everything is alright. This is why you should never use AnyCPU with COM references.
- IIS can only run in two modes: explicit x86, or x64 (default). Enabling "32-bit Applications" leads to an x86 process, that loads your web application in x86 mode. Your COM references are well, when you are on "AnyCPU". However, if you disable "32-bit Applications" for some reason, you will run into trouble. Setting "x64" as target plattform will result in an
BadImageFormatException
.