Could not load file or assembly ... An attempt was made to load a program with an incorrect format.
2010-04-27 19:34
661 查看
一、error
前段时间遇到这个错误:"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."
我只是在一个dll中添加了一个很普通类:
public static class Generic { static Generic () { ...... } public static string ClearValue (string value) { ...... return value; } }
普通得不能再普通了,可是一调用就出错。于是我就把这个类删除,还是出错。可是这个dll用得好好的啊。Google了半天才发现这又是个64位的问题!为什么有个“又”,是的,我在window server 2008 R2上开发SharePoint的时候已经遇到不少32位和64位兼容的问题了。
二、原因
在添加上面那个普通类之前,我用下面的方法加了个excel访问的类:@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullFilePathAndName + @";Extended Properties=""Excel 12.0;HDR=YES;"""
要能正常调用这个excel访问类,必须把项目的platform target改成x86,否则会出现下面的错误:
System.InvalidOperationException: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
于是我把这个dll和调用dll的应用程序的platform target都改成x86就能正常调用这个excel访问类了——这是第一个错误。
第二个错误似乎更严重:我没有测试其他类和方法,有的类可能只能用在x64环境上。
三、解决
该dll的platform target改回Any CPU;调用该dll的应用程序改成x86。
这样就解决了。也就是说只有调用方需要修改platform target为x86,被调用方不用动,还是any cpu。
可是,如果主应用程序又要同时调用该dll中只能在x64环境下的类,又会出现一样的错误,这又怎么办呢?可能只能修改访问excel的方法了吧——不用x86的方式。
四、其他解决方案
网上找到,还没机会试,不过如果再出现类似的错误,可能就有用了:用ProcMon.exe(免费的微软工具)看看到底是调用哪里的哪个dll。
在IIS中设置允许32位的程序在64位环境上:
(1)打开命令行,到下面目录下:
cd %systemdrive%/Inetpub/AdminScripts
(2)输入以下命令:
cscript.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 1
五、参考文献
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/13f991a5-45eb-496c-8618-2179c3753bb0.mspx?mfr=true相关文章推荐
- Could not load file or assembly An attempt was made to load a program with an incorrect format.
- BadImageFormatException or An attempt was made to load a program with an incorrect format
- Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. An attempt was made
- 64bit系统运行32位Web程序出现错误:An attempt was made to load a program with an incorrect format
- 解决An attempt was made to load a program with an incorrect format.问题
- An attempt was made to load a program with an incorrect format
- An attempt was made to load a program with an incorrect format” even when the platforms are the same
- (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
- ASP.NET: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. on Win7 64bit
- ASP.NET 解决An attempt was made to load a program with an incorrect format.问题
- 错误处理:"Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was
- EF Code First - Add-Migration fails with Could not load file or assembly error
- System.BadImageFormatException: Could not load file or assembly
- BadImageFormatException : Could not load file or assembly or one of its dependencies.
- Could not load file or assembly … The parameter is incorrect … HRESULT: 0x80070057 (E_INVALIDARG
- Could not load file or assembly ‘AjaxControlToolkit’ or one of its dependencies. The parameter is incorrect. (Exception
- Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft.Build.En
- NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
- Unhandled Exception: System.BadImageFormatException: Could not load file or assembly
- Bug: How to fix Exception Could not load file or assembly 'EntityFramework, Version=4.0.0.0[…]' or o