ChatGPT解决这个技术问题 Extra ChatGPT

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?

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 Minimum file contents: <configuration><startup useLegacyV2RuntimeActivationPolicy="true"/></configuration>

K
KyleMit

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.


@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: 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: 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 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.
What helped me was putting this line to the NUnit's config file:
Microsoft should make a button in exception dialog: "Search this exception message on stackoverflow"
J
JasCav

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>

How this app config should be used for NANT build ?
A
Alex

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>

R
Raheel Khan

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


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)
W
Wiser Web

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>

D
Deshan

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

What happened to the one with version="v2.0"?
Build number is required for 2.0 but not for 4.0 so the top version is incorrect. Bottom is correct.
M
MarkPm

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>

M
MrBit

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.


R
Run CMD

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


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?
For my I was having this issue with Visual Studio 2015 Test Projects. This post helped me. devbraindump.wordpress.com/2015/07/29/hello-world
@AdamSpicer We added this to all .config files of all sgen.exe's. If none existed, we created one.
O
OmriSela

I used this config:

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

Worked for me


should be v2.0.50727, for v2.0 build number is required, but for v4.0 build number is not needed
M
Michael Armitage

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.


M
Mani

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.


This would fix it because you're assuming the latest version of the DLL is compiled against a newer version of .NET?
C
Community

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.


S
Sonali.thecoder

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>

M
Muheeb

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)

Doing this resolved the issue


R
RubenP5

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


B
Bert Huijben

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.


This is an incomplete solution. the names in the config are case sensitive, and you forgot a U.