Take notes regarding to the Microsoft programming technologies that I used and learnt when working such as ASP.NET, SharePoint, WCF, WWF and etc. before I forgot. ^_^!

About Me

Tuesday 27 April 2010

Could not load file or assembly ... An attempt was made to load a program with an incorrect format.

1. ERROR

I got the error below a few day ago after adding a very very normal class into an existing assembly.

"Could not load file or assembly 'R...tools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=21ef81843f5d77b6' or one of its dependencies. An attempt was made to load a program with an incorrect format."

or

"Could not load file or assembly 'R...tools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=21ef81843f5d77b6' or one of its dependencies."

The class I added:

public static class Generic
{
static Generic ()
{
......
}

public static string ClearValue (string value)
{
......
return value;
}
}

Before this I’ve tested other functions and they worked perfect, but I got the same error even if I deleted the new above class. I had no idea what's going on at all and it seemed suddenly came out.

After a lot of search and reading several articles online, it seemed the 64bit issue again. Again? Yes, again, I've already met a lot of 64bit problems when working on SharePoint on Win2K8 Server.

2. REASON


Actually before adding the above new class, I created another class for reading excel file using @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullFilePathAndName + @";Extended Properties=""Excel 12.0;HDR=YES;""" and it works fine only if the application that invoked the excel reader class is complied with x86 platform target. Otherwise you would get the error :

System.InvalidOperationException: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

So I changed both the assembly and the main application's Platform Target from 'Any CPU' to 'x86' in the Build tab of the project properties to make it work and it did, which was wrong: only the main application's PlatForm Target should be changed to ‘x86’ not the assembly.

The second mistake is more unforgivable: I didn’t retest all other functions that might only work in x64 environment.

3. WORKAROUND


I just changed the Platform Target of the assembly back to 'Any CPU' (Sometimes I have to change it to 'x64' and back to 'Any CPU' and then it takes effect.). They work perfect as before as long as the main application doesn't invoke the x64-only function.

So what if another main application that need to invoke the all functions in the assembly including the x86-only one as well as the x64-only one? Maybe I have to access excel in another way.

4. OTHER SOLUTIONS


There are several other ways I found online but I didn't try them. They might help one day if the similar error comes out again...


  1. Use ProcMon.exe from Microsoft (free tool) to find out which and where the assemblies are being loaded from.

  2. Set IIS in compatibility mode for 32 bits.


(1)Open a command prompt and navigate to the “%systemdrive%\Inetpub\AdminScripts” directory:


cd %systemdrive%\Inetpub\AdminScripts


(2)And then type the following command:


cscript.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 1



(3)Press ENTER.


Reference: http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/13f991a5-45eb-496c-8618-2179c3753bb0.mspx?mfr=true

No comments:

Post a Comment

Followers