17 December 2010

Could not load file or assembly 'System.Data' or one of its dependencies

The full exception is:
Could not load file or assembly 'System.Data' or one of its dependencies. An attempt was made to load a program with an incorrect format.

This problem seems to occur rather a lot and it seems there are different causes for it to occur. I have had this problem today and the information I found on the internet did not help at all. I have tried setting System.Data to not copy local, but then it just started with other libraries. I have also changed the .net version in my web.config, but that did not solve my problem either.
The only real difference that I could find between the old and new server was the fact that the new server was 64-bits. So looked into that and found an option in the advanced settings of an app pool in IIS7, named "Enable 32-Bit Applications".


After setting it to true, everything worked as before.



Note: If you want to use this in IIS6, please be aware that IIS6 does not support this on a "per app pool" basis. So if you set IIS6 to 32 bit, all its app pools will run in 32 bit mode. If you want to set this on a "per app pool" basis, you will have to upgrade to IIS7.
To set IIS6 to 32 bit, do the following:
1. Open a command prompt and navigate to the %systemdrive%\Inetpub\AdminScripts directory.
2. Enter the following command:
cscript.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 "true"
It seems that in some cases, the "true" from the above command needs to be replaced with a 1. Sometimes it is also necessary to reset IIS.

14 comments :

Anonymous said...

What about IIS6 and WinServer2003? I'm having the same problem with my own dll when it is loaded inside of web service hosted on iis6.

Michael Jepson said...

What I have been able to find for doing this in IIS 6 is the following:

1. Open a command prompt and navigate to the %systemdrive%\Inetpub\AdminScripts directory.
2. Type the following command:
cscript.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 “true”
3. Press ENTER.

Anonymous said...

Yes, I also found that.

But that will affect all the applications on the iis. I can't do that because some of them are required to run od iis 64 bit framework.

Thanks for your reply

Michael Jepson said...

Hi Anonymous,

I have done a bit of research on the subject and it seems that IIS 6 does not support setting this on a "per pool" basis.
I also found that you have to make sure your IIS will use the 32 bit ISAPI filters, rather than the 64 bit ones. This indicates to me that your whole IIS will be running in 32 bits, so all programs will too.
I'm afraid your only option is to upgrade to IIS 7 if you want to mix 32 and 64 bit apps.

Regards,

Michael

brownie said...

thanks a lot. you saved me a lot of time!

FabioSSena said...

Thanks friend

Robotovich said...

I ran into the same problem recently. In my case the system dll was copied to the application bin. I simple removed it so that it would be fetched from the local GAC.

Michael Jepson said...

Hi Unknown,

If the System.dll is copied locally, try setting the value for Copy Local to false. You can find this setting by selecting the reference in your project and then opening its properties. Please note that it might be any of the referenced projects in your solution that has the Copy Local set to True. If any of these projects has this setting, it will result in it being copied locally. You'll have to check each one if it still happens.
Deleting it is an option of course, but you might run into it again when you deploy a new version.

Anonymous said...

Thanks! It solved my problem. Cheers to you! :D

Anonymous said...

Thanks a lot......It solved my problem---Prashant Bhopale

Peter Venable said...

Thanks! That was helpful.

swathi said...

Thanks! it solved my problem immediately. Instead of making those changes to DefaultAppPool, i did to my application alone.

Pete Haas said...

Thank you! Had a Win2012 box that had this problem. StackOverflow usually is my 1st stop, but no luck.

Anonymous said...


Thanks. This really helped :-)