IIS

If you deploy web application or service and you get "Could not load file or assembly 'SomeAssembly' or one of its dependencies. An attempt was made to load a program with an incorrect format.", chances are that you are running of 64-bit Windows.

Underlying problem is BadImageFormatException (you can check it in stack trace) but no matter how far down stack trace you take a look, you will not be able to link it to your code. Reason for that is that your application hasn't started running yet.

IIS

By default IIS' processes are 64 bits (on 64-bit system). Your code is in 32-bit world. Mix those two and you won't get far. To fix it simple recompile of application in 64-bits or as "Any CPU" will do.

However, sometimes recompile is out of question. Maybe you have some 32-bit dependencies or you don't have access to source code to recompile it. Whatever reason is, only solution is to force IIS into 32-bit mode.

Solution

That is simply done:

C:\inetpub\AdminScripts\adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 True

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Enable32BitAppOnWin64 : (BOOLEAN) True

This will cause all IIS processes to work in 32-bit mode and that solves our problem. Not even restart is not required.

P.S. Notice that you do need to install "IIS Management Script and Tools" for that script to be available.

Leave a Reply

Your email address will not be published. Required fields are marked *