173

I'm getting the following exception:

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.

as I was trying to export crystal report from my WPF program...

I have added the following in the app.config already...

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
  <NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>

Any experts can help????

Reference I found: http://www.davidmoore.info/2010/12/17/running-net-2-runtime-applications-under-the-net-4-runtime

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Christina Wong
  • 1,739
  • 2
  • 11
  • 4
  • 2
    IMPORTANT: If the error happens with error column "File" as `SGEN`, then the fix needs to be in a file `sgen.exe.config`, next to `sgen.exe`. For example, for VS 2015, create `C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\sgen.exe.config`. Source: [SGEN Mixed mode assembly](https://support.microsoft.com/en-us/help/2572158/sgen-mixed-mode-assembly-is-built-against-version-v2-0-50727-of-the-ru) Minimum file contents: `` – ToolmakerSteve Oct 06 '17 at 01:15
  • 1
    Be aware that you may not only have "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\", but under that, a 'x64' directory that also has an sgen.exe, and possibly several other copies/versions of sgen all in different paths, depending on how many and which versions of VS you have installed. – MickeyfAgain_BeforeExitOfSO Oct 26 '17 at 17:59
  • @ToolmakerSteve Please post your comment as an answer so I can upvote it; none of the other answers helped me. – Malcolm Jun 24 '19 at 18:08
  • @Malcolm - OK, [posted as an answer](https://stackoverflow.com/a/56761796/199364) – ToolmakerSteve Jun 25 '19 at 20:49

8 Answers8

256

Try to use this exact startup tag in your app.config under configuration node

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <requiredRuntime version="v4.0.20506" />
  </startup>
David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Talha
  • 18,898
  • 8
  • 49
  • 66
94

The exception clearly identifies some .NET 2.0.50727 component was included in .NET 4.0. In App.config file use this:

<startup useLegacyV2RuntimeActivationPolicy="true" /> 

It solved my problem

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
Bibaswann Bandyopadhyay
  • 3,389
  • 2
  • 31
  • 30
15

Please add attribute useLegacyV2RuntimeActivationPolicy="true" in your applications app.config file.

Old Value

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
  </startup>

New Value

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
  </startup>

It will solve your problem.

4

I actually had this identical issue with the inverse solution. I had upgraded a .NET project to .NET 4.0 and then reverted back to .NET 3.5. The app.config in my project continued to have the following which was causing the above error in question:

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

The solution to solve the error for this was to revert it back to the proper 2.0 reference as follows:

<startup>
  <supportedRuntime version="v2.0.50727"/>
</startup>

So if a downgrade is producing the above error, you might need to back up the .NET Framework supported version.

atconway
  • 20,624
  • 30
  • 159
  • 229
4

If the error happens with error column "File" as SGEN, then the fix needs to be in a file sgen.exe.config, next to sgen.exe. For example, for VS 2015, create C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\sgen.exe.config. Minimum file contents: <configuration><startup useLegacyV2RuntimeActivationPolicy="true"/></configuration>

Source: SGEN Mixed mode assembly

Malcolm
  • 510
  • 3
  • 5
  • 19
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
2

Enabling the legacy from app.config didn't work for me. For unknown reasons, my application wasn't activating V2 runtime policy. I found a work around here.

Enabling the legacy from app.config is a recommended approach but in some cases it doesn't work as expected. Use the following code with in your main application to force Legacy V2 policy:

public static class RuntimePolicyHelper
{
public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }

static RuntimePolicyHelper()
{
    ICLRRuntimeInfo clrRuntimeInfo =
        (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
            Guid.Empty, 
            typeof(ICLRRuntimeInfo).GUID);
    try
    {
        clrRuntimeInfo.BindAsLegacyV2Runtime();
        LegacyV2RuntimeEnabledSuccessfully = true;
    }
    catch (COMException)
    {
        // This occurs with an HRESULT meaning 
        // "A different runtime was already bound to the legacy CLR version 2 activation policy."
        LegacyV2RuntimeEnabledSuccessfully = false;
    }
}

[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();
}
}
Talha Imam
  • 1,046
  • 1
  • 20
  • 22
1

Try to use another config file (not the one from your project) and RESTART Visual Studio:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.x86.exe.config
(32-bit)

or

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.exe.config
(64-bit)
Udhay Titus
  • 5,761
  • 4
  • 23
  • 41
  • I would expand on this, because circumventing the native `.config` that resides in an application and typically needed for additional application configurations would not be a good solution. – atconway Mar 04 '14 at 18:58
  • 3
    For unit tests, the config change must happen in the vstest.executionengine.x86.exe.config file because that executable is the parent process of the code you're testing. This issue was supposedly fixed in Visual Studio 2012 SP4, but it is still present on my machine. – Brandon S Mar 10 '14 at 15:34
  • (Note you shouldn't have to restart Visual Studio for the changes to take effect. You should just need to end task on the vstest.* processes because those hang around even after your unit tests complete.) – Brandon S Mar 10 '14 at 15:36
  • Upvoating this as it lead me to the actual place where I had to change this. – Janis S. Jan 21 '17 at 12:36
1

For me this was thrown when running unit tests under MSTest (VS2015). Had to add

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

in

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

Mixed-Mode Assembly MSTest Failing in VS2015

Community
  • 1
  • 1
Janis S.
  • 2,526
  • 22
  • 32