C# NUnit 项目引起的 System.BadImageFormatException

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

System.BadImageFormatException caused by NUnit project

c#64-bitnunitbadimageformatexception

提问by Matt Canty

Good day everyone. I have been having the same problem all day at work and am struggling to find any new paths to go down.

今天是个好日子。我一整天都在工作中遇到同样的问题,并且正在努力寻找任何新的路径。

I am getting the following error when my solution builds on server. I have no problem running/debugging all tests in the solution and it builds fine. Both server and my PC are x64. I have followed a lot of advice which I have found to no avail.

当我的解决方案在服务器上构建时出现以下错误。我在解决方案中运行/调试所有测试都没有问题,并且它构建得很好。服务器和我的电脑都是 x64。我遵循了很多建议,但发现都无济于事。

I have set Platform Target to x86 for all projects in my solution under all configurations.

在所有配置下,我已将解决方案中的所有项目的 Platform Target 设置为 x86。

I am aware that there is an nunit-console-x86.exe which could make all the difference but I'm not sure where to specify this in the code.

我知道有一个 nunit-console-x86.exe 可以发挥所有作用,但我不确定在代码中的何处指定它。

Please realise I have trail-blazed the internet, so apologies if I have missed something.

请意识到我已经开辟了互联网,所以如果我错过了什么,请道歉。

System.BadImageFormatException: Could not load file or assembly
'Spin.TradingServices.DataAcquisition.Test.NUnit, Version=1.0.12103.2060, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'Spin.TradingServices.DataAcquisition.Test.NUnit, Version=1.0.12103.2060, Culture=neutral, PublicKeyToken=null'

Server stack trace: at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.Assembly.Load(AssemblyName assemblyRef) at NUnit.Core.Builders.TestAssemblyBuilder.Load(String path) at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, Boolean autoSuites) at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, String testName, Boolean autoSuites) at NUnit.Core.TestSuiteBuilder.BuildSingleAssembly(TestPackage package) at NUnit.Core.TestSuiteBuilder.Build(TestPackage package) at NUnit.Core.SimpleTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.RemoteTestRunner.Load(TestPackage package) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at NUnit.Core.TestRunner.Load(TestPackage package) at NUnit.Util.TestDomain.Load(TestPackage package) at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options) at NUnit.ConsoleRunner.Runner.Main(String[] args)

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

http://app1017-build.oy.gb.sportingindex.com:8080/job/TradingServices.DataAcquisition-Dev/ws/DataAcquisition/build.proj(86,5): error MSB6006: "nunit-console.exe" exited with code -100. Done Building Project " (default targets) -- FAILED.

Build FAILED.

System.BadImageFormatException:无法加载文件或程序集
“Spin.TradingServices.DataAcquisition.Test.NUnit,Version=1.0.12103.2060,Culture=neutral,PublicKeyToken=null”或其依赖项之一。试图加载格式不正确的程序。
文件名:'Spi​​n.TradingServices.DataAcquisition.Test.NUnit,版本=1.0.12103.2060,Culture=neutral,PublicKeyToken=null'

服务器堆栈跟踪:在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean for Introspection, Boolean suppressSecurityChecks) 在 System.Reflection.RuntimeAssembly EvidenceName(InternalLoadAsss)在 NUnit.Core.Builders.TestAssemblyBuilder.Load(String path) 在 System.Reflection.Assembly.Load(AssemblyName assemblyRef) 在 NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName) , Boolean autoSuites) 在 NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, String testName,Boolean autoSuites) at NUnit.Core.TestSuiteBuilder.BuildSingleAssembly(TestPackage package) at NUnit.Core.TestSuiteBuilder.Build(TestPackage package) at NUnit.Core.SimpleTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) ) 在 NUnit.Core.ProxyTestRunner.Load(TestPackage package) 在 NUnit.Core.RemoteTestRunner.Load(TestPackage package) 在 System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext) 的 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)TestSuiteBuilder.Build(TestPackage package) at NUnit.Core.SimpleTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.RemoteTestRunner。在 System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) 在 System.Runtime.Remoting.Messaging 加载(TestPackage 包)。 StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)TestSuiteBuilder.Build(TestPackage package) at NUnit.Core.SimpleTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.ProxyTestRunner.Load(TestPackage package) at NUnit.Core.RemoteTestRunner。在 System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) 在 System.Runtime.Remoting.Messaging 加载(TestPackage 包)。 StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)在 NUnit.Core.RemoteTestRunner.Load(TestPackage package) 在 System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[] & outArgs) 在 System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)在 NUnit.Core.RemoteTestRunner.Load(TestPackage package) 在 System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[] & outArgs) 在 System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)

在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at NUnit.Core 处的 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 处。 TestRunner.Load(TestPackage package) at NUnit.Util.TestDomain.Load(TestPackage package) at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options) at NUnit.ConsoleRunner.Runner.Main(String[] args)

警告:程序集绑定日志已关闭。要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) 设置为 1。注意:有一些与程序集绑定失败日志记录相关的性能损失。要关闭此功能,请删除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

http://app1017-build.oy.gb.sportingindex.com:8080/job/TradingServices.DataAcquisition-Dev/ws/DataAcquisition/build.proj(86,5):错误MSB6006:“nunit-console.exe”退出代码-100。完成构建项目”(默认目标)——失败。

构建失败。

PLEASE NOTE:We have reverted our build on Hudson and now re-committing files more gradually. I will report back on how this goes. Tried get a few heads involved on this one to no avail unfortunately. Shame!

请注意:我们已经恢复了我们在 Hudson 上的构建,现在更逐渐地重新提交文件。我将报告情况如何。不幸的是,尝试让几个人参与其中但无济于事。耻辱!

UpdateI haven't been back to this page for a while but it looks like there are lots of different solutions. If I could mark them all as the answer I would! Those of you finding your way here should probably give equal credit to each option.

更新我有一段时间没有回到这个页面,但看起来有很多不同的解决方案。如果我可以将它们全部标记为答案,我会!那些在这里找到自己方式的人可能应该对每个选项给予同等的信任。

采纳答案by ili

Check the target framework version of your assembly are same as nUnit test runner supports. See runFile.exe.config for list of supported runtimes.

检查程序集的目标框架版本是否与 nUnit 测试运行程序支持的版本相同。有关支持的运行时列表,请参阅 runFile.exe.config。

Also if you have megrated from FW 3 to FW 4, they has different runtime (CLR is different).

此外,如果您已从 FW 3 迁移到 FW 4,它们的运行时不同(CLR 不同)。

回答by Theresa Forster

I had this problem with a console app on X64 pc, the build was set as x86 and it still crashed. I went into Properties on the Console App and under build I changed my Platform Target from x86 to Any CPU then suddenly all the tests worked and ran successfully.

我在 X64 pc 上的控制台应用程序遇到了这个问题,构建设置为 x86,但它仍然崩溃。我进入了控制台应用程序上的属性,在构建下我将平台目标从 x86 更改为任何 CPU,然后突然所有测试都成功并成功运行。

of note, the Configuration Manager's platform field can "lie" to you and doesn't actually have to reflect what the project's properties are actually configured to do. My configuration manager said that "Common.dll" was being built as "Any CPU", but the project properties (the setting that really matters) was building it as "x86".

值得注意的是,配置管理器的平台字段可能会对您“撒谎”,并且实际上不必反映项目的属性实际配置为做什么。我的配置经理说“Common.dll”被构建为“任何 CPU”,但项目属性(真正重要的设置)将它构建为“x86”。

enter image description here

在此处输入图片说明

回答by Matt Canty

Thanks Ashes999 for waking me up.

感谢 Ashes999 唤醒我。

This problem did return again. And rolling back and uploading with didn't work. It turned out to be a monitor object which we no longer even use. Commented out and fixed.

这个问题又回来了。并且回滚和上传不起作用。结果证明它是一个我们甚至不再使用的监视器对象。注释掉并修复。

The way to find this is by doing Debug All Unit Tests. Fix everywhere it pauses. If t doesn't pause then err.

找到这一点的方法是进行调试所有单元测试。修复它暂停的任何地方。如果 t 不暂停,则出错。

回答by ashes999

This might sound stupid, but, check your project and processor architectures. In my case, I was running 64-bit tests on what I assumed was a 64-bit machine(since it was building 64-bit projects).

这听起来可能很愚蠢,但是,请检查您的项目和处理器架构。就我而言,我在我假设的 64 位机器上运行 64 位测试(因为它正在构建 64 位项目)。

Guess what? It was actually a 32-bit machine. So NUnit.exe ran as 32-bit (obviously), and can't understand 64-bit tests.

你猜怎么着?它实际上是一台 32 位机器。因此 NUnit.exe 以 32 位(显然)运行,并且无法理解 64 位测试。

The solution? Build a x86 and x64 version of your tests, and run the x86 ones on the x86 machines, and the x64 ones on the x64 machines.

解决方案?构建 x86 和 x64 版本的测试,并在 x86 机器上运行 x86 版本,在 x64 机器上运行 x64 版本。

Obvious. But worth checking out.

明显的。但值得一试。

回答by Andy

I come across this often, when I test against Console or WPF application. A few causes are

当我针对 Console 或 WPF 应用程序进行测试时,我经常遇到这种情况。几个原因是

  • First, make sure you application doesn't run on .Net Framework 3 or 4 Client profile
  • Then compare the target framework .NET version on both application and test projects. They should be the same
  • Check Platform target on build tab. They should be the same. For example, if test project has "Any CPU" target, the application must have "Any CPU".
  • 首先,确保您的应用程序不在 .Net Framework 3 或 4 客户端配置文件上运行
  • 然后比较应用程序和测试项目上的目标框架 .NET 版本。他们应该是一样的
  • 在构建选项卡上检查平台目标。他们应该是一样的。例如,如果测试项目具有“ Any CPU”目标,则应用程序必须具有“ Any CPU”。

回答by checksum

For me, the exception was caused by invalid /process=singleargument of nunit. If I change the value, then the exception is gone:

对我来说,异常是由/process=singlenunit的无效参数引起的。如果我更改该值,则异常消失:

/process=separate

or

或者

/process=multiple

p/s: late answer, but just for reference..

p/s: 迟到的答案,但仅供参考..

回答by Alex M

A minor addition to all answers. You might need to change the NUnit runner platform (Resharper for me) itself as it was in my case. See example enter image description here

所有答案的小补充。您可能需要更改 NUnit runner 平台(对我来说是 Resharper)本身,就像我的情况一样。查看示例 在此处输入图片说明

回答by ben

My situation was a bit different. The error appeared quite randomly after running a Unit Test that had been modified.

我的情况有点不同。在运行已修改的单元测试后,错误出现的非常随机。

I removed the test and the error went away.

我删除了测试,错误消失了。

I recreated the test and the error came back.

我重新创建了测试,错误又回来了。

I renamed the test and the error went away.

我重命名了测试,错误消失了。

I renamed the test back and the problem did not happen again.

我重命名了测试,问题没有再次发生。

Hope this helps!

希望这可以帮助!

Ben

回答by Zenel

For anyone still having this issue, you also may need to set the framework field on the test runner to match the one in your included project (The field just to the right of the one referenced by AlexM).

对于仍然存在此问题的任何人,您可能还需要在测试运行器上设置框架字段以匹配包含项目中的字段(该字段位于 AlexM 引用的字段的右侧)。

In my case, I was writing a test suite for a Windows Phone 8 library, and NUnit was unable to correctly figure out the framework version that it should use.

就我而言,我正在为 Windows Phone 8 库编写测试套件,而 NUnit 无法正确确定它应该使用的框架版本。

回答by MaurGi

Make sure that this setting is correct: Test menu -> Test Settings -> Default Processor Architecture -> x64 / x86. It was incorrect in my case and it failed with the same issue.

确保此设置正确:Test menu -> Test Settings -> Default Processor Architecture -> x64 / x86。在我的情况下它是不正确的,它因同样的问题而失败。