Monday, May 17, 2010

Force IIS7 to run applications in x86 (32 bit)

The other day I ran into a problem where my WCF C# .net application was being run in x64 bit mode. Normally this would not be an issue but my C# application was linking to some old unmanaged dlls that were compiled to only run in x86 mode.

The error message I was seeing is IIS7 was:
Could not load file or assembly 'MyDllFile' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Could not load file or assembly 'MyDllFile' or one of its dependencies. An attempt was made to load a program with an incorrect format.Description: An unhandled exception occurred during the execution of the current web request. Please 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 'MyDllFile' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Source Error:

An unhandled exception was generated 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.

Here is how I was able to force IIS7 to use x86 mode on a 64 bit version of Windows Server 2008:
  1. First I needed to change my C# applications to compile in x86 mode rather than "Mixed Platforms".
    I did this by right clicking my solution and opening the Properties and then selecting the Configuration Properties tree branch in Visual Studio 2008.

  2. Next I opened IIS7 Manager (Internet Information Services Manager):
    Control Panel -> Administrative Options -> Internet Information Services Manager

  3. In the IIS7 Manager I selected the Application Pools leaf for my server.





  4. I right clicked on the Classic .Net AppPool.

    (you may want to use the Default AppPool depending on if you application was originally designed for IIS7 )

    And selected Advanced Settings...





  5. Under Advanced Sttings there is an option called
    "Enable 32-Bit Applications"

    Switch that option to True





  6. Open your website and find your Application.
    Change your application to use the Classic .Net AppPool
    (or Default AppPool depending on your application )

  7. Restart IIS7

1 comment:

  1. Thanks :) Helped me resolve a 'Microsoft.Jet.OLEDB.4.0 Exception

    ReplyDelete