.net System.BadImageFormatException:无法加载文件或程序集(来自 installutil.exe)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/323140/
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-09-03 10:41:32  来源:igfitidea点击:

System.BadImageFormatException: Could not load file or assembly (from installutil.exe)

.netwindows-servicesx8664-bitx86-64

提问by Epaga

I am trying to install a Windows service using InstallUtil.exe and am getting the error message

我正在尝试使用 InstallUtil.exe 安装 Windows 服务并收到错误消息

System.BadImageFormatException: Could not load file or assembly '{xxx.exe}' or one of its dependencies. An attempt was made to load a program with an incorrect format.

System.BadImageFormatException: 无法加载文件或程序集“ {xxx.exe}”或其依赖项之一。试图加载格式不正确的程序。

What gives?

是什么赋予了?



EDIT: (Not by OP) Full message extracted from dup getting way more hits [for googleability]:

编辑:(不是 OP)从 dup 中提取的完整消息获得了更多的点击率 [for googleability]:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe C:\xxx.exe Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1 Copyright (c) Microsoft Corporation. All rights reserved.

Exception occurred while initializing the installation: System.BadImageFormatException: Could not load file or assembly 'file:///C:\xxx.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format..

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe C:\xxx.exe Microsoft (R) .NET Framework 安装实用程序版本 4.0.30319.1 版权所有 (c) Microsoft Corporation。版权所有。

初始化安装时发生异常:System.BadImageFormatException: 无法加载文件或程序集“file:///C:\xxx.exe”或其依赖项之一。试图加载格式不正确的程序。

采纳答案by Epaga

Make sure the newest Framework (the one you compiled your app with) is first in the PATH. That solved the problem for me. (Found on a forum)

确保最新的框架(您编译应用程序的框架)是 PATH 中的第一个。那为我解决了问题。(在论坛上找到

回答by Ruben Bartelink

Some more detail for completeness in case it helps someone...

一些完整性的更多细节,以防它帮助某人......

Note that the most common reason for this exception these days is attempting to load a 32 bit-specific (/platform:x86) DLL into a process that is 64 bit or vice versa (viz. load a 64 bit-specific (/platform:x64) DLL into a process that is 32 bit). If your platformis non-specific (/platform:AnyCpu), this won't arise (assuming no referenced dependencies are of the wrong bitness).

请注意,目前出现此异常的最常见原因是尝试将 32 位特定 ( /platform:x86) DLL 加载到 64 位进程中,反之亦然(即,将 64 位特定 ( /platform:x64) DLL加载到32 位)。如果您platform是非特定的 ( /platform:AnyCpu),则不会出现这种情况(假设没有引用的依赖项是错误的)。

In other words, running:

换句话说,运行:

%windir%\Microsoft.NET\Framework\v2.0.50727\installutil.exe

%windir%\Microsoft.NET\Framework\v2.0.50727\installutil.exe

or:

或者:

%windir%\Microsoft.NET\Framework64\v2.0.50727\installutil.exe

%windir%\Microsoft.NET\Framework 64\v2.0.50727\installutil.exe

will not work (substitute in other framework versions: v1.1.4322(32-bit only, so this issue doesn't arise) and v4.0.30319as desired in the above).

将不起作用(替代其他框架版本:(v1.1.4322仅限 32 位,因此不会出现此问题)并且v4.0.30319如上所示)。

Obviously, as covered in the other answer, one will also need the .NET version number of the installutilyou are running to be >= (preferably =) that of the EXE/DLL file you are running the installer of.

显然,如另一个答案所述,您还需要installutil您正在运行的 .NET 版本号>=(最好是 =)您正在运行安装程序的 EXE/DLL 文件的版本号。

Finally, note that in Visual Studio 2010, the tooling will default to generating x86 binaries(rather than Any CPUas previously).

最后,需要注意的是在Visual Studio 2010,该工具会默认生成x86二进制文件而不是任何CPU如前)。

Complete details of System.BadImageFormatException(saying the only cause is mismatched bittedness is really a gross oversimplification!).

System.BadImageFormatException 的完整细节(说唯一的原因是不匹配的bittedness 真的是过于简单化了!)。

Another reason for a BadImageFormatExceptionunder an x64installer is that in Visual Studio 2010, the default .vdprojInstall Project type generates a 32-bit InstallUtilLibshim, even on an x64 system(Search for "64-bit managed custom actions throw a System.BadImageFormatException exception" on the page).

x64安装程序BadImageFormatException下的另一个原因是在 Visual Studio 2010 中,默认安装项目类型生成 32 位垫片,即使在 x64 系统上(搜索“64 位托管自定义操作引发 System.BadImageFormatException 异常”在这一页)。.vdprojInstallUtilLib

回答by Sachin Kalia

I think you are using the 64-bit version of the tool to install a 32-bit application. I've also faced this issue today and used this Framework path to cater .

我认为您正在使用该工具的 64 位版本来安装 32 位应用程序。我今天也遇到了这个问题,并使用这个框架路径来满足 .

C:\Windows\Microsoft.NET\Framework\v4.0.30319

C:\Windows\Microsoft.NET\Framework\v4.0.30319

and it should install your 32-bit application just fine.

它应该可以很好地安装您的 32 位应用程序。

回答by zar

The key is to set match processor settings for the project which are at two places.

关键是在两个地方为项目设置匹配处理器设置。

enter image description here

在此处输入图片说明

And also make sure the architecture settings is same in Test menu >> Test Settings >> Default Processor Architecture >> as show below.

并确保测试菜单>>测试设置>>默认处理器架构>>中的架构设置相同,如下所示。

enter image description here

在此处输入图片说明

This is for VS2013 but maybe same for other versions too.

这是针对 VS2013 的,但对于其他版本也可能相同。

Update - For VS2019:

更新 - 对于 VS2019:

enter image description here

在此处输入图片说明

回答by James Crowther

OK, this is the problem I had, and, what fixed it, seems very relevant to the above.

好的,这就是我遇到的问题,并且修复它的方法似乎与上述问题非常相关。

I am using Visual Studio 2010 Express. I wrote a test service that didn't really do anything. It was just practice for the real thing later.

我正在使用 Visual Studio 2010 Express。我写了一个没有真正做任何事情的测试服务。这只是后来真正的练习。

I wrote the service and tried to install it using installutil.exeand got the following error:

我编写了该服务并尝试使用安装它installutil.exe并收到以下错误:

System.BadImageFormatException: Could not load file or assembly '{filename.exe}' or one of its dependencies. An attempt was made to load a program with an incorrect format.

System.BadImageFormatException: 无法加载文件或程序集“{filename.exe}”或其依赖项之一。试图加载格式不正确的程序。

So far the same as the original author.

到目前为止与原作者相同。

Ruben's observationabove about the 32 bit output of Visual Studio 2010 was the saviour here.

Ruben上面关于 Visual Studio 2010 的 32 位输出的观察是这里的救星。

I used the 64-bit version of the installutil.exeand sure enough, the output of the Visual Studio 2010 build was 32-bit. Just to add a little extra value here, you can find the 32-bit version of the latest .NET framework and the associated installutil.exein the C:\Windows\Microsoft.NET\frameworkfolder. Using this version of the installutil.exefixed my problem; the service installed without a hitch!

我使用了 64 位版本,installutil.exe果然,Visual Studio 2010 版本的输出是 32 位的。只是在这里添加一点额外的价值,您可以installutil.exeC:\Windows\Microsoft.NET\framework文件夹中找到最新 .NET 框架的 32 位版本和相关联。使用此版本installutil.exe修复了我的问题;服务安装顺利!

I hope this helps someone else out there.

我希望这可以帮助其他人。

回答by SohamC

After trying all the mentioned solutions I found the PlatformTargetsomehow added to AnyCPUconfiguration in my project .csproj.

在尝试了所有提到的解决方案后,我发现以PlatformTarget某种方式添加到AnyCPU我的项目 .csproj 中的配置中。

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

Removing the line worked for me.

删除线路对我有用。

回答by Michael Staples

I had this issue with a WinForms Project using VS 2015. My solution was:

我在使用 VS 2015 的 WinForms 项目中遇到了这个问题。我的解决方案是:

  1. right click the Project
  2. select properties
  3. check "Prefer 32-bit"
  4. Platform target: Any CPU
  1. 右键单击项目
  2. 选择属性
  3. 勾选“首选 32 位”
  4. 平台目标:任何 CPU

回答by VK_217

Target build x64 Target Server Hosting IIS 64 Bit

目标构建 x64 目标服务器托管 IIS 64 位

Right Click appPool hosting running the website/web application and set the enable 32 bit application = false.

右键单击运行网站/Web 应用程序的 appPool 托管并设置启用 32 位应用程序 = false。

enter image description here

在此处输入图片说明

回答by ash

I have faced this issue today. In my case, my application's (had a reference to a 64-bit dll)platform target was set to AnyCPUbut Prefer 32-bitcheck boxunder platform target section was ticked by default. This was the problem and worked all fine after un-checking Prefer 32-bitoption.

我今天遇到了这个问题。就我而言,我的应用程序(引用了 64 位 dll)平台目标设置为AnyCPU但默认情况下勾选了平台目标部分下的Prefer 32-bit复选框。这就是问题所在,取消选中Prefer 32-bit选项后一切正常。

回答by Logan

We found a different solution to a problem with the same symptom:

对于具有相同症状的问题,我们找到了不同的解决方案:

We saw this error when we updated the project from .net 4.7.1 to 4.7.2.

当我们将项目从 .net 4.7.1 更新到 4.7.2 时,我们看到了这个错误。

The problem was that even though we were not referencing System.Net.Http any more in the project, it was listed in the dependentAssembily section of our web.config. Removing this and any other unused assembly references from the web.config solved the problem.

问题是,即使我们在项目中不再引用 System.Net.Http,它也被列在了 web.config 的dependentAssembily 部分。从 web.config 中删除这个和任何其他未使用的程序集引用解决了这个问题。