C# 无法加载文件或程序集异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9419403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:20:51  来源:igfitidea点击:

Could not load file or assembly exception

c#web-services

提问by Sharpeye500

Any thoughts on what might be causing this exception?

关于可能导致此异常的任何想法?

I have a webservice proj, when i load the link i get

我有一个网络服务项目,当我加载链接时,我得到

Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Exception Details: System.BadImageFormatException: Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

无法加载文件或程序集“Interop.DIB”或其依赖项之一。试图加载格式不正确的程序。
异常详细信息:System.BadImageFormatException:无法加载文件或程序集“Interop.DIB”或其依赖项之一。试图加载格式不正确的程序。

Inner exceptions:

内部异常:

[BadImageFormatException: Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

[ConfigurationErrorsException: Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

[HttpException (0x80004005): Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

[BadImageFormatException: 无法加载文件或程序集“Interop.DIB”或其依赖项之一。试图加载格式不正确的程序。]

[ConfigurationErrorsException: 无法加载文件或程序集“Interop.DIB”或其依赖项之一。试图加载格式不正确的程序。]

[HttpException (0x80004005): 无法加载文件或程序集“Interop.DIB”或其依赖项之一。试图加载格式不正确的程序。]

Version Information:
Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

版本信息:
Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.272

采纳答案by Sharpeye500

Ok the answer is Got to Start->Run->type inetmgr and on the left application pools, select DefaultAppPool and the virtual directory name of the app and for both make sure to enable 32 -bit applications to true, am using IIS7.0 and windows 7 64-bit. enter image description here

好的答案是开始->运行->输入inetmgr,然后在左侧的应用程序池中,选择 DefaultAppPool 和应用程序的虚拟目录名称,并确保将 32 位应用程序启用为 true,我使用的是 IIS7.0和 Windows 7 64 位。 在此处输入图片说明

回答by Gregory A Beamer

If I had to take a guess, it is either a) not finding the interoped assembly or b) the COM DLL is not registered in the local registry. Simply copying DIB to the /bin folder is not enough when you are monkeying with COM.

如果我不得不猜测,要么 a) 找不到互操作程序集,要么 b) COM DLL 未在本地注册表中注册。当您使用 COM 时,简单地将 DIB 复制到 /bin 文件夹是不够的。

I believe (B) is the most likely answer to what is happening.

我相信(B)是对正在发生的事情最有可能的答案。

回答by KeithS

The most common cause in my experience is that you made a change to a referenced assembly that requires rebuilding other assemblies using that changed assembly, and didn't rebuild them.

根据我的经验,最常见的原因是您对引用的程序集进行了更改,该程序集需要使用该更改的程序集重建其他程序集,而没有重建它们。

Example #1: you have an EXE that references a DLL. You add something to the referenced DLL that adds a new method, new parameter, whatever. This changes the external "signature" of the DLL; that is, the location in memory of various entry points. You don't rebuild the EXE. When the EXE loads and tries to reference the new DLL, its old entry point is no longer valid, so it cannot execute the code it needs.

示例 #1:您有一个引用 DLL 的 EXE。您向引用的 DLL 添加一些内容,以添加新方法、新参数等。这会更改 DLL 的外部“签名”;即各种入口点在内存中的位置。你不重建EXE。当 EXE 加载并尝试引用新 DLL 时,其旧入口点不再有效,因此无法执行所需的代码。

Example #2: you have an x86 EXE that references a DLL. This DLL must also be compiled for x86 (or Any CPU). If you rebuild it for x64, the EXE, running in a 32-bit space, will not understand the instructions and register references to the 64-bit "extended" world, and will cry uncle.

示例#2:您有一个引用 DLL 的 x86 EXE。此 DLL 还必须针对 x86(或任何 CPU)进行编译。如果您为 x64 重建它,运行在 32 位空间中的 EXE 将无法理解对 64 位“扩展”世界的指令和寄存器引用,并且会哭叔叔。

回答by Maciej

BadImageFormatExceptionusually means 64 vs 32 bit conflict. One of the assemblies is set to a specific platformi.e. 64 bit or 32 bit while the other is set or defaults to a different one. Check if both assemblies are for the same platform, preferably "Any CPU". In other words it could be that a 64 bit assembly is trying to load 32 bit one or vice versa.

BadImageFormatException通常意味着 64 位与 32 位冲突。其中一个程序集设置为特定平台,即 64 位或 32 位,而另一个设置或默认为不同的平台。检查两个程序集是否用于同一平台,最好是“任何 CPU”。换句话说,可能是 64 位程序集正在尝试加载 32 位程序集,反之亦然。

This also applies if you're calling a COM or a DLL which is compiled for different platform, for example you call 32 bit COM/DLL from an assembly on a 64 bit system where assembly's platform would default to x64. In this case adjust your assembly's platform to match.

如果您正在调用为不同平台编译的 COM 或 DLL,这也适用,例如,您从 64 位系统上的程序集调用 32 位 COM/DLL,其中程序集的平台默认为 x64。在这种情况下,调整您的程序集的平台以匹配。

To change platform go to project Properties -> Build -> Platform.

要更改平台,请转到项目属性 -> 构建 -> 平台。

回答by Dublin7

What worked for me was to add the assembly to GAC. To do that I ran gacutil -i PATH_TO_ASSEMBLYfrom Visual Studio Command Prompt

对我有用的是将程序集添加到 GAC。为此,我从 Visual Studio 命令提示符运行gacutil -i PATH_TO_ASSEMBLY

回答by Aligned

I finally got around this exception by deleting the entry in the applicationhost.config for IIS Express (C:\Users{username}\Documents\IISExpress\config\applicationhost.config).

我终于通过删除 IIS Express (C:\Users{username}\Documents\IISExpress\config\applicationhost.config) 的 applicationhost.config 中的条目解决了这个异常。

I had also stopped the IIS express instance, cleaned and rebuilt in VS. Then change the config file, then restarted VS 2013.

我还停止了 IIS express 实例,在 VS 中进行了清理和重建。然后更改配置文件,然后重新启动VS 2013。

回答by user3821479

An alternate to this issue is to change the build properties of your project.

此问题的替代方法是更改​​项目的构建属性。

For vb.net got to Project--> Properties --> Compile --> Advanced Compile Options Here change the target CPU

对于 vb.net 到 Project--> Properties --> Compile --> Advanced Compile Options 在这里更改目标 CPU

回答by Andrew Swallows

I had the same issue on Visual Studio 2015 on Windows 10 x64. I was already compiling in Any CPU mode. This was a default MVC4 application (nothing added). I found a simple solution over here that worked for me: https://github.com/aspnet/Home/issues/524

我在 Windows 10 x64 上的 Visual Studio 2015 上遇到了同样的问题。我已经在 Any CPU 模式下编译了。这是一个默认的 MVC4 应用程序(没有添加任何内容)。我在这里找到了一个对我有用的简单解决方案:https: //github.com/aspnet/Home/issues/524

In VS 2015: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express for websites and projects

在 VS 2015 中:工具 > 选项 > 项目和解决方案 > Web 项目 > 将 64 位版本的 IIS Express 用于网站和项目

回答by Mike Gledhill

I had this issue, when trying to use 64-bit .dlls in my ASP.Net project, in Visual Studio 2013.

我在 Visual Studio 2013 中尝试在我的 ASP.Net 项目中使用 64 位 .dll 时遇到了这个问题。

The solution was to click on Tools\Options, and tick this box:

解决方案是单击Tools\Options,然后勾选此框:

enter image description here

在此处输入图片说明

回答by radkan

What worked for me is doing a BIOS update on my machine!

对我有用的是在我的机器上更新 BIOS!