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.
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>
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With