545

I have a project in which I'd like to use some of the .NET 4.0 features but a core requirement is that I can use the System.Data.SQLite framework which is compiled against 2.X. I see mention of this being possible such as the accepted answer here but I don't see how to actually achieve this.

When I just try and run my 4.0 project while referencing the 2.X assembly I get:

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.

What "additional configuration" is necessary?

Community
  • 1
  • 1
jamone
  • 17,253
  • 17
  • 63
  • 98
  • 5
    See also: http://stackoverflow.com/questions/1604663/what-does-uselegacyv2runtimeactivationpolicy-do-in-the-net-4-config – Mikhail Poda Oct 13 '10 at 12:10
  • 1
    Try it ! [http://social.msdn.microsoft.com/Forums/en/clr/thread/58271e39-beca-49ac-90f9-e116fa3dd3c0](http://social.msdn.microsoft.com/Forums/en/clr/thread/58271e39-beca-49ac-90f9-e116fa3dd3c0) Thanks. Have fun. –  Oct 26 '11 at 09:41
  • 1
    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:20

17 Answers17

693

In order to use a CLR 2.0 mixed mode assembly, you need to modify your App.Config file to include:

<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

The key is the useLegacyV2RuntimeActivationPolicy flag. This causes the CLR to use the latest version (4.0) to load your mixed mode assembly. Without this, it will not work.

Note that this only matters for mixed mode (C++/CLI) assemblies. You can load all managed CLR 2 assemblies without specifying this in app.config.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    @Reed I have tried your suggestion, but the exception still pops up. I have double-checked the app.config that gets copied to my EXE folder and it still doesn't work. It comes up when using log4net. I can't find anything about this error re: log4net except for here: http://stackoverflow.com/questions/1866735/log4net-and-net-4-0, but it doesn't say much. Any ideas on how I can get more information about my particular issue? – Dave Nov 15 '10 at 19:32
  • 1
    @Dave: That's suggesting that you can just use a .NET 4 native version of log4net - that seems like the best option. If there's a .NET 4 version, use it... – Reed Copsey Nov 15 '10 at 20:57
  • @Reed there isn't one, and I actually interpreted that answer to mean that you would have to compile log4net yourself. I guess I can do that, but I'd rather just take their precompiled binary and reference it from my project. I'll look into it some more. But can you think of why changing app.config doesn't make it work? I thought maybe I had to use programname.exe.config, but I tried that too and it didn't work. In my limited experience, either filename does the same thing. – Dave Nov 15 '10 at 23:39
  • @Dave I've had the same problem when running tests from NUnit GUI runner launched from Visual Studio (tests use SQLite). Reed's advice did the job for a scenario when I launch the runner NOT from VS. I noticed however that the error is still there if I try to debug it directly from VS. Strange. Maybe it helps you somehow. – Filip Zawada Jan 26 '11 at 02:13
  • 20
    What helped me was putting this line to the *NUnit's* config file: – Filip Zawada Jan 26 '11 at 03:02
  • Piggy backing on Filip Zawada's point, this solved my problem when I added the `useLegacyV2RuntimeActivationPolicy` attribute to NAnt's config file, as that is my startup process. – flipdoubt Aug 31 '11 at 16:07
  • 77
    Microsoft should make a button in exception dialog: "Search this exception message on stackoverflow" – Davi Fiamenghi Mar 18 '13 at 21:33
  • This solution does not work in VS2015 http://stackoverflow.com/questions/36484717/uselegacyv2runtimeactivationpolicy-true-does-not-work-in-vs2015-how-to-fix – Charles Okwuagwu Apr 07 '16 at 19:45
37

This forum post on the .NET Framework Developer Center. It might provide some insight.

(Add to the app's config file.)

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>
JasCav
  • 34,458
  • 20
  • 113
  • 170
15

Depending on what version of the framework you're targeting, you may want to look here to get the correct string:

http://msdn.microsoft.com/en-us/library/ee517334.aspx

I wasted hours trying to figure out why my release targeting .Net 4.0 client required the full version. I used this in the end:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0.30319" 
               sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
Alex
  • 14,104
  • 11
  • 54
  • 77
Anthony Wieser
  • 4,351
  • 1
  • 23
  • 25
11

Once you set the app.config file, visual studio will generate a copy in the bin folder named App.exe.config. Copy this to the application directory during deployment. Sounds obvious but surprisingly a lot of people miss this step. WinForms developers are not used to config files :).

Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • Another way is to delete the app.config and then add a new one from Project->Add->New item and choose General->Application Configuration File (which is not the same as just creating a text file called app.config) – smirkingman Mar 26 '14 at 20:55
8

Using 2.0 and 4.0 assemblies together isn't quite straight forward.

The ORDER of the supported framework declarations in app.config actually have an effect on the exception of mixed mode being thrown. If you flip the declaration order you will get mixed mode error. This is the purpose of this answer.

So if you get the error in a Windows Forms app, , try this, mostly Windows Forms apps.

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
    <supportedRuntime version="v2.0.50727"></supportedRuntime>
  </startup>

Or if the project is not Windows Form. In a Web project add this to web.config file.

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"></supportedRuntime>
  </startup>
Wiser Web
  • 81
  • 1
  • 1
7

Was able to solve the issue by adding "startup" element with "useLegacyV2RuntimeActivationPolicy" attribute set.

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

But had to place it as the first child element of configuration tag in App.config for it to take effect.

<?xml version="1.0"?>
  <configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      <supportedRuntime version="v2.0.50727"/>
    </startup>
  ......
....
Deshan
  • 2,112
  • 25
  • 29
5

The above didnt work for me (I am working on a web app) - but this did...

Edit the sgen.exe.config file in the folder (I had to create one first); C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools (There is also one in v7.0 folder, but I didnt need to change that one, I am using VS2012)

The conents of the XML should look like this (same in previous answers)

<?xml version ="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <requiredRuntime safemode="true" imageVersion="v4.0.30319" version="v4.0.30319"/>
    </startup>
</configuration>
MarkPm
  • 194
  • 1
  • 4
5

If your are working in a web service and the v2.0 assembly is a dependency that has been loaded by WcfSvcHost.exe then you must include

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
</startup>

in ..\Microsoft Visual Studio 10.0\Common7\IDE\ WcfSvcHost.exe.config file

This way, Visual Studio will be able to send the right information through the loader at runtime.

MrBit
  • 51
  • 1
  • 3
4

I ran into this issue when we changed to Visual Studio 2015. None of the above answers worked for us. In the end we got it working by adding the following config file to ALL sgen.exe executables on the machine

<?xml version ="1.0"?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0" />
        </startup>    
</configuration>

Particularly in this location, even when we were targeting .NET 4.0:

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools

Run CMD
  • 2,937
  • 3
  • 36
  • 61
  • can you be more specific? Do you mean you added this config options to all existing *.config files or that you created a .config file for sgen.exe? – Adam Spicer Aug 24 '15 at 17:23
  • For my I was having this issue with Visual Studio 2015 Test Projects. This post helped me. https://devbraindump.wordpress.com/2015/07/29/hello-world/ – Adam Spicer Aug 24 '15 at 17:26
  • 1
    @AdamSpicer We added this to all .config files of all sgen.exe's. If none existed, we created one. – Run CMD Aug 25 '15 at 14:58
3

I used this config:

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v2.0"/>
    <supportedRuntime version="v4.0"/>
</startup>

Worked for me

OmriSela
  • 566
  • 1
  • 6
  • 15
1

I had this problem when upgrading to Visual Studio 2015 and none of the solutions posted here made any difference, although the config is right the location for the change is not. I fixed this issue by adding this configuration:

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

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

Then restarted Visual Studio.

Michael Armitage
  • 1,502
  • 19
  • 17
0

I found a way around this after 3-4 hours of googling. I have added the following

<startup selegacyv2runtimeactivationpolicy="true">
  <supportedruntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>

If this doesn't solve your problem then--> In the Project References Right Click on DLL where you getting error --> Select Properties--> Check the Run-time Version --> If it is v2.0.50727 then we know the problem. The Problem is :- you are having 2.0 Version of respective DLL. Solution is:- You can delete the respective DLL from the Project references and then download the latest version of DLL's from the corresponding website and add the reference of the latest version DLL reference then it will work.

Mani
  • 17,549
  • 13
  • 79
  • 100
venu
  • 41
  • 2
  • 1
    This would fix it because you're assuming the latest version of the DLL is compiled against a newer version of .NET? – Lucas Dec 11 '14 at 19:01
0

I was experiencing this same error, and spent forever adding the suggested startup statements to various config files in my solution, attempting to isolate the framework mismatch. Nothing worked. I also added startup information to my XML schemas. That didn't help either. Looking at the actual file that was causing the problem (which would only say it was "moved or deleted") revealed it was actually the License Compiler (LC).

Deleting the offending licenses.licx file seems to have fixed the problem.

Community
  • 1
  • 1
mono código
  • 405
  • 1
  • 6
  • 10
0

I was facing a similar issue while migrating some code from VS 2008 to VS 2010 Making changes to the App.config file resolved the issue for me.

<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"
         sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
</configuration>
0

Add following at this location C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64 FileName: sgen.exe.config(If you dont find this file, create and add one)

 <?xml version ="1.0"?>

<configuration>
 <runtime>        
        <generatePublisherEvidence enabled="false"/>    
    </runtime>

    <startup useLegacyV2RuntimeActivationPolicy="true">

                <supportedRuntime version="v4.0" />

    </startup>    

</configuration>

Doing this resolved the issue

Muheeb
  • 216
  • 2
  • 6
0

I Use

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
</startup>

It's works but just before de </configuration> tag otherwise the startup tag doesn't work properly

RubenP5
  • 11
  • 4
-1

Also i had this issue with the class library, If any one have the issue with the class library added to your main application. Just add

<startup useLegacyV2RuntimeActivationPolicy="true">

to you main application which would then be picked by the class library.

Bert Huijben
  • 19,525
  • 4
  • 57
  • 73