+1  A: 

Does your web site uses the DefaultAppPool? If so, try setting the application pool of your web site to ASP .Net v4.0, Or if you are using a customized app pool, verify that it is running .net framework 4.0

sagie
I am using a new application pool. I set it to use .Net 4.0 Framework already and using integrated mode. Any ideas?
George2
Could this issue be caused by my Windows Server 2008 not activated (I did not entered a license number yet)?
George2
With which platform you are building your solution? Any CPU? x86?I don't think it is an activation issue.Are you using any third parties?
sagie
I am not using 3rd parties. If I refer to another DLL I developed by myself, is it 3rd party?
George2
And with which platform this Dll was built with?
sagie
+1  A: 

The BadImageFormatException is raised when the assembly file can be found, but is not a proper assembly, or is corrupted. For instance, if you FTP'ed the files to the server and the transfer was interrupted, the DLL file may have been transferred only partially, causing this error to happen.

On 64 bit vs 32 bit: when you use P/Invoke or COM Interop, some blogger reports that switching to a specific target compilation may help your situation. This means: if you interface with a 32 bit dll, make sure that you compile for x86, forcing it to run under WoW32, otherwise you'll receive this exception. This fix is confirmed here and here.

Alternatively, you can set your whole system to default 32-bit by running the command:

Ldr64.exe setwow

from the Framework64 directory.

A common solution is to rebuild the file, or to at least re-publish it.

Abel
You mean test.foo.dll itself is corrupted or some other system DLLs?
George2
@George2: I mean test.foo.dll is corrupted, but see my new notes on the 32 bit vs 64 bit, which may seem counter-intuitive.
Abel
Thanks for your update @Abel, do you mean I should rebuild the solution to 64-bit platform (since my Windows Server 2008 is 64-bit)?
George2
Could this issue be caused by my Windows Server 2008 not activated (I did not entered a license number yet)?
George2
@George2: that depends. If you do not use COM Interop or P/Invoke to 32 bit COM or native DLLs, you can stick to Any and the issue is somewhere else. If you do use P/Invoke or COM Interop: *change to 32 bit* and test if that works. Note: when running on 64 bit, the .NET boots trapper will select 64 bit and compiles as such, but if you *need* 32 bit, you must compile specifically to that platform (even if you *run* on 64 bit).
Abel
I did not use COM Interop or P/Invoke in my code, my application is just simple web application. I think I should use build for Any Platform, correct?
George2
@George2: correct. (I missed your q. about non-activation: don't worry, Windows is fully functional, it will not block your self-made code and definitely not with a BadImageFormatException...).
Abel
Thanks, question answered!
George2