简体   繁体   English

NUnit 项目引起的 System.BadImageFormatException

[英]System.BadImageFormatException caused by NUnit project

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.服务器和我的电脑都是 x64。 I have followed a lot of advice which I have found to no avail.我遵循了很多建议,但发现都无济于事。

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 System.BadImageFormatException: 无法加载文件或程序集
'Spin.TradingServices.DataAcquisition.Test.NUnit, Version=1.0.12103.2060, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 'Spin.TradingServices.DataAcquisition.Test.NUnit, Version=1.0.12103.2060, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。 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'文件名:'Spi​​n.TradingServices.DataAcquisition.Test.NUnit,版本=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.Proxy服务器堆栈跟踪:在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, 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) 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) 在 NUnit.Core.Proxy TestRunner.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) TestRunner.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, 对象服务器, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) 在 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)在 [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)

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.要启用程序集绑定失败日志记录,请将注册表值 [HKLM\\Software\\Microsoft\\Fusion!EnableLog] (DWORD) 设置为 1。注意:有一些与程序集绑定失败日志记录相关的性能损失。 To turn this feature off, remove the registry value [HKLM\\Software\\Microsoft\\Fusion!EnableLog].要关闭此功能,请删除注册表值 [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. http://app1017-build.oy.gb.sportingindex.com:8080/job/TradingServices.DataAcquisition-Dev/ws/DataAcquisition/build.proj(86,5) :错误MSB6006:“nunit-console.exe”退出代码-100。 Done Building Project " (default targets) -- FAILED.完成构建项目”(默认目标)——失败。

Build FAILED.构建失败。

PLEASE NOTE: We have reverted our build on Hudson and now re-committing files more gradually.请注意:我们已经恢复了我们在 Hudson 上的构建,现在更逐渐地重新提交文件。 I will report back on how this goes.我将报告情况如何。 Tried get a few heads involved on this one to no avail unfortunately.不幸的是,尝试让几个人参与其中但无济于事。 Shame!耻辱!

Update I 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.那些在这里找到自己方式的人可能应该对每个选项给予同等的信任。

I had this problem with a console app on X64 pc, the build was set as x86 and it still crashed.我在 X64 pc 上的控制台应用程序遇到了这个问题,构建设置为 x86,但它仍然崩溃。 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.我进入了控制台应用程序上的属性,在构建下我将平台目标从 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”被构建为“Any CPU”,但项目属性(真正重要的设置)将它构建为“x86”。

在此处输入图片说明

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.您可能需要更改 NUnit runner 平台(对我来说是 Resharper)本身,就像我的情况一样。 See example查看示例在此处输入图片说明

Make sure that this setting is correct: Test menu -> Test Settings -> Default Processor Architecture -> x64 / x86 .确保此设置正确: Test menu -> Test Settings -> Default Processor Architecture -> x64 / x86 It was incorrect in my case and it failed with the same issue.在我的情况下它是不正确的,它因同样的问题而失败。

Check the target framework version of your assembly are same as nUnit test runner supports.检查程序集的目标框架版本是否与 nUnit 测试运行程序支持的版本相同。 See runFile.exe.config for list of supported runtimes.有关支持的运行时列表,请参阅 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 不同)。

I come across this often, when I test against Console or WPF application.当我针对 Console 或 WPF 应用程序进行测试时,我经常遇到这种情况。 A few causes are几个原因是

  • First, make sure you application doesn't run on .Net Framework 3 or 4 Client profile首先,确保您的应用程序不在 .Net Framework 3 或 4 客户端配置文件上运行
  • Then compare the target framework .NET version on both application and test projects.然后比较应用程序和测试项目上的目标框架 .NET 版本。 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 ".例如,如果测试项目具有“ Any CPU ”目标,则应用程序必须具有“ Any CPU ”。

For me, the exception was caused by invalid /process=single argument of nunit.对我来说,异常是由无效的/process=single参数引起的。 If I change the value, then the exception is gone:如果我更改该值,则异常消失:

/process=separate

or或者

/process=multiple

p/s: late answer, but just for reference.. p/s: 迟到的答案,但仅供参考..

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.它实际上是一台 32 位机器。 So NUnit.exe ran as 32-bit (obviously), and can't understand 64-bit tests.因此 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.但值得一试。

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

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 不暂停,则出错。

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 无法正确确定它应该使用的框架版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nunit System.BadImageFormatException - Nunit System.BadImageFormatException 使用 NUnit 和 Resharper 时出现 System.BadImageFormatException - System.BadImageFormatException while using NUnit and Resharper System.BadImageFormatException错误 - System.BadImageFormatException error 无法在 NUnit 测试项目(System.BadImageFormatException:无法加载文件或程序集)上加载 Oracle.DataAccess dll,但应用程序运行正常 - Cannot load Oracle.DataAccess dll on NUnit Test project (System.BadImageFormatException : Could not load file or assembly) but app runs fine 未处理System.BadImageFormatException-简单修复 - System.BadImageFormatException was unhandled - simple fix pinvoke c函数-System.BadImageFormatException - pinvoke c function - System.BadImageFormatException 获取AssemblyVersion时发生System.BadImageFormatException - System.BadImageFormatException when getting the AssemblyVersion 目标框架为 4.0 时的 System.BadImageFormatException - System.BadImageFormatException when target framework is 4.0 System.BadImageFormatException,如何找到有问题的 dll - System.BadImageFormatException, how to find the offending dll System.BadImageFormatException: '无法加载文件或程序集 - System.BadImageFormatException: 'Could not load file or assembly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM