Wednesday, May 23, 2012

What "additional configuration" is necessary to reference a .NET 2.0 mixed mode assembly in a .NET 4.0 project?


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?


Source: Tips4all

7 comments:

  1. 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.

    ReplyDelete
  2. 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>

    ReplyDelete
  3. Depending on what version of the framework you're targetting, 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 targetting .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>

    ReplyDelete
  4. I used this config:

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


    Worked for me

    ReplyDelete
  5. 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 :).

    ReplyDelete
  6. 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>

    ReplyDelete
  7. Additionally, you need to add a reference to System.configuration to fix the problem stated by Dave in the comments above.

    ReplyDelete