Mixed-Mode Assembly MSTest Failing in VS2015

自闭症网瘾萝莉.ら 提交于 2019-12-21 03:53:06

问题


When attempting to run unit tests that use mixed mode assemblies in VS2015 the tests fail to execute with the usual message:

System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

Creating an app.config and adding useLegacyV2RuntimeActivationPolicy to it has no effect - it seems as though this configuration is impossible to change.

This previously worked with no manual steps in VS2013.


回答1:


Alternative 1: Configuration

Add the startup configuration to C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TE.ProcessHost.Managed.exe.config:

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

Alternative 2: At Runtime

This may stop working.

Simply add this class to the unit test project (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();
    }
}


来源:https://stackoverflow.com/questions/32478917/mixed-mode-assembly-mstest-failing-in-vs2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!