【问题标题】:Mixed-Mode Assembly MSTest Failing in VS2015VS2015 中的混合模式组装 MSTest 失败
【发布时间】:2015-12-05 09:14:45
【问题描述】:

当尝试在 VS2015 中运行使用混合模式程序集的单元测试时,测试无法执行并显示通常的消息:

System.IO.FileLoadException:混合模式程序集是针对运行时版本“v2.0.50727”构建的,如果没有额外的配置信息,则无法在 4.0 运行时中加载。

创建一个 app.config 并向其中添加 useLegacyV2RuntimeActivationPolicy 没有任何效果 - 似乎无法更改此配置。

这以前在 VS2013 中无需手动步骤即可工作。

【问题讨论】:

标签: unit-testing visual-studio-2015 mstest


【解决方案1】:

备选方案 1:配置

将启动配置添加到C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TE.ProcessHost.Managed.exe.config:

<startup useLegacyV2RuntimeActivationPolicy="true">
</startup>

备选方案 2:在运行时

这可能会停止工作。

只需将这个类添加到单元测试项目中(source):

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public static class RuntimePolicyHelper
{
    [AssemblyInitialize]
    public static void SetPolicy(TestContext ctx)
    {
        var clrRuntimeInfo =
            (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
                Guid.Empty,
                typeof(ICLRRuntimeInfo).GUID);

        // Allow errors to propagate so as to fail the tests.
        clrRuntimeInfo.BindAsLegacyV2Runtime();
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
    private interface ICLRRuntimeInfo
    {
        void xGetVersionString();
        void xGetRuntimeDirectory();
        void xIsLoaded();
        void xIsLoadable();
        void xLoadErrorString();
        void xLoadLibrary();
        void xGetProcAddress();
        void xGetInterface();
        void xSetDefaultStartupFlags();
        void xGetDefaultStartupFlags();

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        void BindAsLegacyV2Runtime();
    }
}

【讨论】:

  • 我在以下位置找不到文件:C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TE.ProcessHost.Managed.exe。我尝试添加一个,但没有奏效。但是,添加代码似乎可行。
  • @Derek 我找到 .config 文件的方式是查看“输出”或“测试”窗口。它将为您提供 .exe 名称(“Process TE.ProcessHost.Managed.exe exited with code X”)。在你的 VS 目录中搜索它。替代方案 2 一开始对我有用,然后完全停止工作 - 我会避免它。
  • @Derek 我刚刚注意到路径不正确(facepalm)。我已经更正了。
  • 我不知道为什么 - 我什至在那个位置没有“TestWindow”文件夹。
  • 对于 Visual Studio 2012(版本 11),我修改了以下文件(我可以使用 @Derek 描述的任务管理器解决方案找到该文件):C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.x86.exe.config。我在现有的&lt;configuration&gt; 节点下添加了以下行:&lt;startup useLegacyV2RuntimeActivationPolicy="true" /&gt;。
猜你喜欢
  • 2016-10-19
  • 2017-03-04
  • 2011-04-14
  • 2017-08-20
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 2016-12-24
相关资源
最近更新 更多