.NET 4 配置中的“useLegacyV2RuntimeActivationPolicy”有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1604663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:28:59  来源:igfitidea点击:

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

.net.net-4.0app-configmixed-mode

提问by Cameron MacFarland

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

在将使用 SlimDX 且因此具有非托管代码的项目转换为 .NET 4.0 时,我遇到了以下错误:

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.

混合模式程序集是针对运行时的“v2.0.50727”版本构建的,如果没有附加配置信息,则无法在 4.0 运行时中加载。

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 useLegacyV2RuntimeActivationPolicydoing? I can't find any documentation about it.

我的问题是,这是在useLegacyV2RuntimeActivationPolicy做什么?我找不到任何关于它的文档。

采纳答案by Cameron MacFarland

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

经过一段时间(和更多搜索),我找到了 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:

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

我们最近看到的一个问题是,由于支持并行运行时,.NET 4.0 改变了它绑定到旧混合模式程序集的方式。例如,这些程序集是从 C++\CLI 编译的程序集。当前可用的 DirectX 程序集是混合模式。如果您看到这样的消息,那么您就知道遇到了问题:

混合模式程序集是针对运行时的“v1.1.4322”版本构建的,如果没有其他配置信息,则无法在 4.0 运行时中加载。

[剪辑]

对于应用程序来说,好消息是您可以选择通过设置 app.config 标志来为这些程序集回退到 .NET 2.0 时代绑定,如下所示:

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

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 useLegacyV2RuntimeActivationPolicyattribute reverts back to CLR 2.0 loading.

因此,运行时加载混合模式程序集的方式似乎发生了变化。我找不到有关此更改的任何详细信息,也找不到更改的原因。但该useLegacyV2RuntimeActivationPolicy属性恢复为 CLR 2.0 加载。

回答by Mark Miller

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)

这是我最近写的一个解释,以帮助解决有关此属性的信息的空白。 http://www.marklio.com/marklio/PermaLink,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx(InternetArchive Wayback Machine 链接)

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.

[安装 .NET] v4 是“无影响的”。安装时不应更改现有组件的行为。

useLegacyV2RuntimeActivationPolicy 属性基本上可以让您说:“我对遗留的 shim API 有一些依赖性。请让它们在选择的运行时间方面按照过去的方式工作。”

为什么我们不将此作为默认行为?您可能会争辩说,这种行为更兼容,并且可以更轻松地从以前的版本移植代码。如果您还记得,这不能是默认行为,因为它会使 v4 的安装产生影响,这可能会破坏您计算机上安装的现有应用程序。

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

完整的帖子更详细地解释了这一点。在 RTM 上,关于这方面的 MSDN 文档应该会更好。