C# System.BadImageFormatException:无法加载文件或程序集...尝试使用 installutil.exe 安装服务时格式不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16658976/
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
System.BadImageFormatException:Could not load file or assembly … incorrect format when trying to install service with installutil.exe
提问by aami
I know i am going to ask duplicatequestion but my scenario is totally different i think so because when i go to do unit testing with nunit tool of my program then in NUnit this error happen
我知道我会问重复的问题,但我的情况完全不同,我认为是因为当我使用程序的 nunit 工具进行单元测试时,在 NUnit 中会发生此错误
"NewTest.testtest (TestFixtureSetUp): SetUp : System.BadImageFormatException : Could not load file or assembly 'AUTO_REPAIR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."
“NewTest.testtest (TestFixtureSetUp): SetUp: System.BadImageFormatException: 无法加载文件或程序集 'AUTO_REPAIR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。尝试加载一个格式不正确的程序。”
I wonder why this error is happening to this tool ? i am sure i dont have error in project or in any test case.
我想知道为什么这个工具会发生这个错误?我确信我在项目或任何测试用例中都没有错误。
Please help me out.
请帮帮我。
here is image of this error
这是此错误的图像


采纳答案by Ammar Asjad
There are two ways to solve this error
有两种方法可以解决这个错误
- Set Platform target to x86 for your project in visual studio(Right click on your solution and then go to properties then you will find build option and when you click on build you will find platform target). After you set your platform target to x86 try to do your unit test using nunit.
- 在 Visual Studio 中将您的项目的平台目标设置为 x86(右键单击您的解决方案,然后转到属性,然后您将找到构建选项,当您单击构建时,您将找到平台目标)。将平台目标设置为 x86 后,尝试使用 nunit 进行单元测试。
or
或者
- Simply find out nunit-x86.exe in bin folder of your nunit folder and just run this exe file and you will not see any exception again :)
- 只需在您的 nunit 文件夹的 bin 文件夹中找到 nunit-x86.exe 并运行此 exe 文件,您将不会再次看到任何异常:)
回答by bryanbcook
BadFormatException occurs when you attempt to load an x86 assembly in a 64bit process or vice versa. As per NUnit documentation (http://www.nunit.org/index.php?p=nunit-gui&r=2.4.2) the default runner is compiled as AnyCPU which means its a 64bit process on a 64bit machine. If one of your assemblies or any of its dependencies are directly compiled for x86 you will get this exception.
BadFormatException 在您尝试在 64 位进程中加载 x86 程序集时发生,反之亦然。根据 NUnit 文档(http://www.nunit.org/index.php?p=nunit-gui&r=2.4.2),默认运行程序被编译为 AnyCPU,这意味着它是 64 位机器上的 64 位进程。如果您的一个程序集或其任何依赖项是为 x86 直接编译的,您将收到此异常。
Switch to the Nunit-x86 or Nunit-console-x86.exe and everything should load correctly.
切换到 Nunit-x86 或 Nunit-console-x86.exe,一切都应该正确加载。
回答by viggity
To tack onto Ammar's comment. Don't rely solely on what Configuration Manager tells you is the active platform. Go to the project properties and check there too, that is what is the real platform for the project
附上 Ammar 的评论。不要仅仅依赖 Configuration Manager 告诉您的活动平台。转到项目属性并检查那里,这就是项目的真正平台



