C# 当 AnyCPU 测试程序集实现来自 x64 生产程序集的接口时出现 BadImageFormatException

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

BadImageFormatException when AnyCPU test assembly implements interface from x64 production assembly

c#visual-studio-2010mstest64-bit

提问by David

I seem to have hit on a scenario where when I run mstest on an AnyCPU assembly which references an x64 assembly, I get a BadImageFormatException.

我似乎遇到了一个场景,当我在引用 x64 程序集的 AnyCPU 程序集上运行 mstest 时,我得到一个 BadImageFormatException。

The issue occurs when an interface in x64Production.dll is implemented (even if unused) by the AnyCPUTestingx64Production.dll test assembly:

当 x64Production.dll 中的接口由 AnyCPUTestingx64Production.dll 测试程序集实现(即使未使用)时,会出现此问题:

Unable to load the test container 'D:\AnyCPUTestingx64Production.dll' 
or one of its dependencies. error details:
System.BadImageFormatException: 
    Could not load file or assembly 'x64Production, 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.
  • mstest is running on windows 7 64-bit
  • the test assembly is built as AnyCPU to get it to run in 64bit on a 64bit host (as outlined here)
  • the testsettings file specifies <Execution hostProcessPlatform="MSIL"/>
  • peverify and corflags reveal nothing interesting
  • this is readily reproducible in a toy solution, i.e. where
    • x64Production
      • references no other assemblies
      • includes only an empty public interface IExampleInterface
      • has <PlatformTarget> set to x64
    • AnyCPUTestingx64Production
      • references only x64Production.dll (i.e. this issue is present even without a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework)
      • includes only an empty implementation of x64Production.IExampleInterface
      • has <PlatformTarget> set to x64
  • nunit can load and run the test assembly (once I've converted all the test attributes)
    • but is not a good short term solution to the larger problem (which involves a huge number of project files)
  • the same issue arises whether the projects target 3.5 or 4.0
  • the same issues arises whether the VS2008 or VS2010 c# compiler is used
  • the same issue arises whether mstest from VS2010 or Test Agents is used
  • it is mstest which fails while loading AnyCPUTestingx64Production - i.e this is not an issue with attempting to load the assembly in the wrong QTAgent (nothing shows up in Process Monitor and renaming QTAgent32.exe has no effect):
  • mstest 在 Windows 7 64 位上运行
  • 测试组件建成AnyCPU得到它在64位主机上运行64位(如概述这里
  • testsettings 文件指定 <Execution hostProcessPlatform="MSIL"/>
  • peverify 和 corflags 没有显示任何有趣的东西
  • 这很容易在玩具解决方案中重现,即
    • x64制作
      • 不引用其他程序集
      • 仅包含一个空的公共接口 IExampleInterface
      • 将 <PlatformTarget> 设置为 x64
    • AnyCPUTestingx64Production
      • 仅引用 x64Production.dll(即,即使没有引用 Microsoft.VisualStudio.QualityTools.UnitTestFramework 也存在此问题)
      • 仅包含 x64Production.IExampleInterface 的空实现
      • 将 <PlatformTarget> 设置为 x64
  • nunit 可以加载和运行测试程序集(一旦我转换了所有测试属性)
    • 但对于更大的问题(涉及大量项目文件)来说,这不是一个好的短期解决方案
  • 项目的目标是 3.5 还是 4.0 也会出现同样的问题
  • 无论使用 VS2008 还是 VS2010 c# 编译器都会出现同样的问题
  • 是否使用来自 VS2010 或 Test Agents 的 mstest 会出现同样的问题
  • 它是 mstest 在加载 AnyCPUTestingx64Production 时失败 - 即这不是尝试在错误的 QTAgent 中加载程序集的问题(进程监视器中没有任何显示并且重命名 QTAgent32.exe 无效):
    *** Assembly Binder Log Entry  (09/02/2012 @ 09:44:26) ***

    The operation failed.
    Bind result: hr = 0x8007000b. An attempt was made to load a program with an incorrect format.

    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe
    --- A detailed error log follows. 

    === Pre-bind state information ===
    LOG: User = David
    LOG: DisplayName = x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///D:/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = MSTest.exe
    Calling assembly : AnyCPUTestingx64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe.Config
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///D:/x64Production.DLL.
    LOG: Assembly download was successful. Attempting setup of file: D:\x64Production.dll
    LOG: Entering run-from-source setup phase.
    LOG: Assembly Name is: x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

Has anyone else ascertained whether or not this is simply unsupported in the VS2010 mstest?

有没有其他人确定这在 VS2010 mstest 中是否完全不受支持?

采纳答案by Joshua

From reading this, MSTest.exe is 32 bit.

通过阅读本文,MSTest.exe 是 32 位的。

回答by David

Having followed this blog post, the following, run from a VS command prompt (so CorFlags.exe is in the PATH), gets the tests running for my toy solution:

遵循此博客文章后,以下内容从 VS 命令提示符运行(因此 CorFlags.exe 在 PATH 中),为我的玩具解决方案运行测试:

@echo off
REM remove the 32Bit flag, forcing the executable to be 64-bit when run on a 64 bit os.
REM Expect the following output:
REM "
REM corflags : warning CF011 : The specified file is strong name signed.  Using /Force will invalidate the signature of this image and will require the assembly to be resigned.
REM "
CorFlags.exe "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe" /32BIT- /Force

REM skip the strong name verification, because the 32-bit flag was modified 
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\StrongName\Verification\MSTest,b03f5f7f11d50a3a /f

REM copy over registry keys to the 64-bit shadow registry.
REM Without the "{13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b}\Extensions" subkey, mstest will output
REM "
REM File extension specified '.dll' is not a valid test extension.
REM "
reg copy HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio.0\EnterpriseTools\QualityTools\TestTypes HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio.0\EnterpriseTools\QualityTools\TestTypes /s /f

It remains to be seen how this will fare against the real production code.

这将如何与真正的生产代码发生冲突还有待观察。

回答by Anupam

I came here searching for the solution for a similar problem. Posting this answer just in case the solution I found helps someone else. This solved it for me in Visual Studio (2012):

我来到这里寻找类似问题的解决方案。发布此答案以防万一我找到的解决方案对其他人有所帮助。这在 Visual Studio (2012) 中为我解决了这个问题:

Add New Item -> Test Settings Add Test Setting ItemChange the testsetting Run test in 64 bit processBy default this is set to "Force test to run in 32 bit process"

添加新项目 -> 测试设置添加测试设置项更改测试设置在 64 位进程中运行测试默认情况下,这设置为“强制测试在 32 位进程中运行”

From the menu: Test -> Test Settings -> Select Test Settings File -> Choose the test settings file you have created.

从菜单中:测试 -> 测试设置 -> 选择测试设置文件 -> 选择您创建的测试设置文件。

Now run the tests.

现在运行测试。

回答by JustTrying

Now with Visual Studio 2013 (at least, didn't try in 2012) I didn't have to do anything but choose Test->Test Settings->Default Processor Architecture->x64. Can also use test settings file to achieve the same result. None of those old kluges necessary that you see in other answers and various posting on the web. Since my stuff has to use x64, I added these test cases too just to remind me if I have some setting wrong.

现在有了 Visual Studio 2013(至少在 2012 年没有尝试过),我什么都不用做,只需要选择测试-> 测试设置-> 默认处理器架构-> x64。也可以使用测试设置文件来达到相同的结果。您在其他答案和网络上的各种帖子中看到的那些旧 kluges 都不是必需的。由于我的东西必须使用 x64,我也添加了这些测试用例,只是为了提醒我是否有设置错误。

    [TestMethod]
    public void Running_64Bit_OS()
    {
        // It was determined to run only in 64 bits.
        bool is64BitOS = System.Environment.Is64BitOperatingSystem;
        Assert.AreEqual(is64BitOS, true);
    }

    [TestMethod]
    public void Running_64Bit_Process()
    {
        // We have a requirement that one of the unmanaged DLL is built for 64 bits.
        // If you are running MS Test in Visual Studio 2013 and this
        // is failing, go to Test->Test Settings->Default Processor Architecture and
        // chose x64, then run the tests again.  This is not the only method.  You
        // could also use a test settings file.
        bool is64BitProcess = System.Environment.Is64BitProcess;
        Assert.AreEqual(is64BitProcess, true);
    }

回答by wonster

If you have ReSharper installed, refer to the following link

如果您安装了 ReSharper,请参考以下链接

Basically, you need to create a test settings file in your solution as indicated in the other answers then update the ReSharper option for MsTest to point to the same settings file.

基本上,您需要按照其他答案中的说明在解决方案中创建一个测试设置文件,然后更新 MsTest 的 ReSharper 选项以指向相同的设置文件。

I cam across this issue using Visual Studio 2013 Update 4 and Resharper 8.2.

我在使用 Visual Studio 2013 Update 4 和 Resharper 8.2 时遇到了这个问题。

回答by hagensoft

In my case, it seemed to have nothing to do with x86 or x64 platform or test configuration settings or the project's .NET version. BTW the BadImageFormatException error I got also mentioned something about 'the signature is incorrect'.

就我而言,它似乎与 x86 或 x64 平台或测试配置设置或项目的 .NET 版本无关。顺便说一句,我得到的 BadImageFormatException 错误也提到了一些关于“签名不正确”的内容。

The issue was that when using Moq, we need to add missing references to the unit test project for classes/interfaces that are dependent of the object we are trying to mock. Look into the references of the project you are testing to get an idea of what assemblies you might be missing that are related to the object you are mocking.

问题是,在使用 Moq 时,我们需要为依赖于我们尝试模拟的对象的类/接口添加对单元测试项目的缺失引用。查看您正在测试的项目的引用,以了解您可能缺少哪些与您正在模拟的对象相关的程序集。

http://vibrantcode.com/2012/05/18/badimageformatexception-the-signature-is-incorrect/

http://vibrantcode.com/2012/05/18/badimageformatexception-the-signature-is-incorrect/

回答by Wray Smallwood

Thanks for the tip on Resharper, because it pointed out the problem could be avoided by switching to MSTest. I could not get Resharper to work. Testing a 64 bit third party 64 bit DLL even if you are only Mocking it (still have to load it) only appears to work with MsTest in 64 bit mode. The Resharper option for MSTest to "Use this Test Run Configuration" only has "Default" as a dropdown option and "Edit" is greyed out. The other setting "Use Test Run Configuration specified in metadata file" does not work either and this assumes one knows what or where this metadata file is. Resharper would not run in 64 bit mode as proved by the above Environment variable Is64BitProcess. (VS 2013 Update 4, Resharper 8.2)

感谢有关 Resharper 的提示,因为它指出可以通过切换到 MSTest 来避免该问题。我无法让 Resharper 工作。测试 64 位第三方 64 位 DLL,即使您只是模拟它(仍然必须加载它)似乎也只能在 64 位模式下与 MsTest 一起使用。MSTest 的 Resharper 选项“使用此测试运行配置”只有“默认”作为下拉选项,“编辑”显示为灰色。另一个设置“使用元数据文件中指定的测试运行配置”也不起作用,这假设人们知道这个元数据文件是什么或在哪里。正如上述环境变量 Is64BitProcess 所证明的那样,Resharper 不会在 64 位模式下运行。(VS 2013 更新 4,Resharper 8.2)

回答by fernacolo

In addition to @Anupam solution, on VS2013, you can go to TEST > Test Settings > Default Processor Architectureand change between X86and X64. It's almost the same as selecting a test settings file, except that you don't need the file for only specifying the test platform.

除了@Anupam 解决方案,在 VS2013 上,您可以转到TEST > Test Settings > Default Processor Architecture并在X86X64之间进行更改。这与选择测试设置文件几乎相同,只是您不需要该文件仅用于指定测试平台。

回答by user2545071

Also, you can go to menu Test->Test Settings->Default Procesor Architecture->X64. It may work.

此外,您可以转到菜单 Test->Test Settings->Default Processor Architecture->X64。它可能会起作用。

回答by jrh

How to set up MSTest to test a 64 bit assembly

如何设置 MSTest 来测试 64 位程序集

In addition to the .testsettings information provided by the other answerers on this question, this answer covers some of the quirks of Visual Studio 2015 and early versions of Visual Studio 2017, this fix may also work for Visual Studio 2013 but I don't have a machine available to test with for that.

除了其他回答者在这个问题上提供的 .testsettings 信息之外,这个答案还涵盖了 Visual Studio 2015 和 Visual Studio 2017 早期版本的一些怪癖,这个修复程序也可能适用于 Visual Studio 2013,但我没有一台可用于测试的机器。

1. Add a .testsettings file

1.添加一个.testsettings文件

Right click on the Solution(not the unit test project), then under the Test Settings category, add a .testsettings file. It can be named anything you want.

右键单击解决方案(不是单元测试项目),然后在测试设置类别下,添加一个 .testsettings 文件。它可以命名为您想要的任何名称。

Add test settings window screenshot

添加测试设置窗口截图



2. Configure the .testsettings file to use a 64 bit process

2.配置.testsettings文件使用64位进程

In the "Test Settings" wizard that comes up, the only thing that you must customize is under the "Hosts" tab, set the "Run tests in 32 bit or 64 bit process" to "Run tests in 64 bit process on 64 bit machine". While you are here it may be a good idea to review the default settings to make sure they make sense. Click Apply, then Close when you are done.

在出现的“测试设置”向导中,您唯一必须自定义的是在“主机”选项卡下,将“在 32 位或 64 位进程中运行测试”设置为“在 64 位进程中运行测试”机器”。当您在这里时,检查默认设置以确保它们有意义可能是个好主意。单击应用,然后在完成后关闭。

Test settings configuration window, showing where to set it to 64 bit

测试设置配置窗口,显示将其设置为 64 位的位置

Now your .testsettings file will show up in solution explorer.

现在您的 .testsettings 文件将显示在解决方案资源管理器中。

Image showing the test settings file in the solution explorer

显示解决方案资源管理器中的测试设置文件的图像



Extra Workaround for a bug in Visual Studio 2015

Visual Studio 2015 中错误的额外解决方法

  • It seems like Visual Studio 2017 (tested using version 15.3.3 Community) has made steps 3 and 4 unnecessary. I will leave these steps here for those of you who are using older versions of Visual Studio, or in case there is still a way to reproduce the behavior.

  • In Visual Studio 2015, if you just set the default processor architecture through Test -> Test Settings -> Default Processor Architecture -> x64, Visual Studio will forget your setting (see this bug report). This was tested in Visual Studio 2015 Professional Update 3.

  • From what I have read, Visual Studio 2013 has a similar bug to Visual Studio 2015 when it comes to remembering CPU architecture. I haven't tested this on Visual Studio 2013 (I don't have it) but it might be worth a try.
  • 似乎 Visual Studio 2017(使用版本 15.3.3 社区测试)使第 3 步和第 4 步变得不必要。我将把这些步骤留在这里给那些使用旧版本 Visual Studio 的人,或者如果仍然有办法重现该行为。

  • 在 Visual Studio 2015 中,如果您只是通过 Test -> Test Settings -> Default Processor Architecture -> x64 设置默认处理器架构,Visual Studio 将忘记您的设置(请参阅此错误报告)。这在 Visual Studio 2015 Professional Update 3 中进行了测试。

  • 从我读到的内容来看,Visual Studio 2013 在记住 CPU 架构方面有一个与 Visual Studio 2015 类似的错误。我还没有在 Visual Studio 2013 上测试过这个(我没有),但它可能值得一试。

3. Add a .runsettings file to set your tests to be permanently 64 bit

3. 添加一个 .runsettings 文件以将您的测试设置为永久 64 位

Open notepad (or your XML file editor of choice), and paste this into it.

打开记事本(或您选择的 XML 文件编辑器),并将其粘贴到其中。

<?xml version="1.0" encoding="utf-8"?>  
<RunSettings>  
    <!-- Configurations that affect the Test Framework -->  
    <RunConfiguration>  
        <!-- [x86] | x64 -->  
        <TargetPlatform>x64</TargetPlatform> 
    </RunConfiguration> 
</RunSettings> 

Then save the file, I saved it as DemoTest.runsettings in my solution's directory alongside DemoTest.testsettings.

然后保存文件,我将其另存为 DemoTest.runsettings 与 DemoTest.testsettings 一起保存在我的解决方案目录中。

  • Please see Configure unit tests by using a .runsettings filefor more information about this file.

  • Note: It is safe to have a .runsettings file with just this entry in it, because...

    Each element of the file is optional, because every value has a default.

  • I recommend adding your .runsettings file to your solution, just so developers can see it in the Solution Explorer, though this does not affect functionality in any way.

  • 有关此文件的更多信息,请参阅使用 .runsettings 文件配置单元测试

  • 注意: .runsettings 文件中只包含此条目是安全的,因为...

    文件的每个元素都是可选的,因为每个值都有一个默认值。

  • 我建议将您的 .runsettings 文件添加到您的解决方案中,这样开发人员就可以在解决方案资源管理器中看到它,尽管这不会以任何方式影响功能。



4. Load your .runsettings file

4. 加载您的 .runsettings 文件

In the menu bar, click Test -> Test Settings -> Select Test Settings File

在菜单栏中,点击测试 -> 测试设置 -> 选择测试设置文件

Image showing how to load a test settings file using the Test menu

显示如何使用“测试”菜单加载测试设置文件的图像

Select your runsettingsfile. Not your testsettings file.

选择您的运行设置文件。不是您的 testsettings 文件。



Now you should be able to run your tests without issues.

现在您应该能够毫无问题地运行您的测试。

image showing that the tests are okay

显示测试正常的图像

MSTest Limitations

MSTest 限制

  • Note that MSTest will only work with Unit Test Projects compiled as Any CPU. An x64 test project will not show any tests under Test Explorer.

  • The assembly to be tested can be x64, but the unit test library itself must be Any CPU.

  • 请注意,MSTest 仅适用于编译为 Any CPU 的单元测试项目。x64 测试项目不会在测试资源管理器下显示任何测试。

  • 要测试的程序集可以是 x64,但单元测试库本身必须是 Any CPU。

Configuration manager showing that the unit test library must be built as Any CPU

配置管理器显示单元测试库必须构建为 Any CPU