ChatGPT解决这个技术问题 Extra ChatGPT

What does 'useLegacyV2RuntimeActivationPolicy' do in the .NET 4 config?

While converting a project that used SlimDX, and therefore has unmanaged code, to .NET 4.0 I ran into the following error:

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.

Googling around gave me the solution, which is to add this to the applications config:

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

My question is, what is the useLegacyV2RuntimeActivationPolicy doing? I can't find any documentation about it.


C
Community

After a bit of time (and more searching), I found this blog entry by Jomo Fisher.

One of the recent problems we’ve seen is that, because of the support for side-by-side runtimes, .NET 4.0 has changed the way that it binds to older mixed-mode assemblies. These assemblies are, for example, those that are compiled from C++\CLI. Currently available DirectX assemblies are mixed mode. If you see a message like this then you know you have run into the issue: Mixed mode assembly is built against version 'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. [Snip] The good news for applications is that you have the option of falling back to .NET 2.0 era binding for these assemblies by setting an app.config flag like so:

So it looks like the way the runtime loads mixed-mode assemblies has changed. I can't find any details about this change, or why it was done. But the useLegacyV2RuntimeActivationPolicy attribute reverts back to CLR 2.0 loading.


It's worth noting here that meanwhile marklios answer (stackoverflow.com/questions/1604663/…) provides a link to his thorough explanation regarding this change.
A thorough explanation of this can be found on MSDN (Although it doesn't explicitly mention the solution mentioned above): msdn.microsoft.com/en-us/magazine/ee819091.aspx
What if I've added this to both the config for my application and a config for my UnitTest project and I am still receiving a file load error when running tests. Should I post a new question?
C
Community

Here's an explanation I wrote recently to help with the void of information on this attribute. http://www.marklio.com/marklio/PermaLink,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx (Internet Archive Wayback Machine link)

To quote the most relevant bits:

[Installing .NET] v4 is “non-impactful”. It should not change the behavior of existing components when installed. The useLegacyV2RuntimeActivationPolicy attribute basically lets you say, “I have some dependencies on the legacy shim APIs. Please make them work the way they used to with respect to the chosen runtime.” Why don’t we make this the default behavior? You might argue that this behavior is more compatible, and makes porting code from previous versions much easier. If you’ll recall, this can’t be the default behavior because it would make installation of v4 impactful, which can break existing apps installed on your machine.

The full post explains this in more detail. At RTM, the MSDN docs on this should be better.


user20493, can you run your app with the environment variable COMPlus_CLRLoadLogDir set to an empty directory the app has write access to and share the resulting logs (please scrub for any PII before sharing). It may help explain what's happening. The config attribute may not be applied to the context in which your app is running.
This link should also help you understand what the problem is, and what the solution is doing for you: msdn.microsoft.com/en-us/magazine/ee819091.aspx