问题
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