在.net 4.0上执行的应用程序在.net 2.0下编译时 [英] When executing an application on .net 4.0, compiled under .net 2.0

查看:93
本文介绍了在.net 4.0上执行的应用程序在.net 2.0下编译时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定:


  1. 下面的C#源代码是在.NET 2.0(CLR 2.0)下编译的;和

  2. 上面的应用程序使用下面列出的 app.config

  3. 在执行应用程序的客户端环境中仅安装.NET 4.0(CLR 4.0),

  1. The C# source code below is compiled under .NET 2.0 (CLR 2.0); and
  2. The above application uses the app.config listed below; and
  3. Only .NET 4.0 (CLR 4.0) is installed on the environment of the client executing the application,

然后内部加载哪个版本的.NET在客户端环境上执行应用程序?

then which version of .NET is internally loaded to execute the application on the client's environment?

下面的控制台应用程序将简单地显示其CLR版本在控制台中为 v4.0.30319 ,但是@Reed Copsey对堆栈的回答(CLR 2.0与4.0性能?)表明在这种情况下已加载.NET 2.0。此外,在 MSDN 中,它说 useLegacyV2RuntimeActivationPolicy 设置为false false

The console application below will simply show that its CLR version is v4.0.30319 in the console, but @Reed Copsey's answer of the stack (CLR 2.0 vs 4.0 performance?) shows that .NET 2.0 is loaded in this case. Moreover, at MSDN it says when useLegacyV2RuntimeActivationPolicy is set to false false:


使用.NET Framework 4和更高版本的默认激活策略
,它允许传统的运行时激活技术将CLR
版本1.1或2.0加载到该过程中。

尽管 app.config 带有。 NET 4.0配置。我误解了吗?

It sounds like .NET 2.0 is loaded in spite of the app.config having a .NET 4.0 configuration. Have I misunderstood anything?

C#源代码

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string version = Environment.Version.ToString();
            Console.WriteLine(version);
        }
    }
}

app.config

app.config

<?xml version="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="false">
        <supportedRuntime version="v4.0.30319"/>
    </startup>
</configuration>


推荐答案

最重要的是,在您的方案中,您指定了.Net 4是您唯一受支持的运行时,因此您的应用程序将使用CLR 4加载。

The bottom line is that under your scenario, you specified .Net 4 as your only supported runtime, so your app will load with CLR 4.

程序的CLR行为完全符合设计要求:

The CLR behavior with your program is exactly as designed:

当我使用 supportedRuntime 作为v4.0运行测试应用程序时,Process Explorer显示它加载了mscorlib v4.0.30319。

When I run your test app with the supportedRuntime as v4.0, Process Explorer shows it loads mscorlib v4.0.30319.

当我以 supportedRuntime 作为v2.0.50727运行时,Process Explorer显示它加载了mscorlilb v2.0.50727。

When I run with supportedRuntime as v2.0.50727, Process Explorer shows it loads mscorlilb v2.0.50727.

当我在没有 supportedRuntime 元素的情况下运行时,Process Explorer显示它加载了mscorlilb v2.0.50727。

When I run with no supportedRuntime element, Process Explorer shows it loads mscorlilb v2.0.50727.

来自Microsoft的此内容指出 supportedRuntime 元素定义运行程序的特定版本:

This blurb from Microsoft states that the supportedRuntime element defines the specific version on which your program runs:


默认情况下,应用程序运行反之为其构建的.NET Framework中的n。如果不存在该版本,并且应用程序配置文件未定义受支持的版本,则可能会发生.NET Framework初始化错误。在这种情况下,运行该应用程序的尝试将失败。

By default, an application runs on the version of the .NET Framework that it was built for. If that version is not present and the application configuration file does not define supported versions, a .NET Framework initialization error may occur. In this case, the attempt to run the application will fail.

要定义运行应用程序的特定版本,请在应用程序的配置文件中添加一个或多个元素。每个元素都列出了运行时的受支持版本,第一个指定了首选版本,最后一个指定了首选版本。

To define the specific versions on which your application runs, add one or more elements to your application's configuration file. Each element lists a supported version of the runtime, with the first specifying the most preferred version and the last specifying the least preferred version.






这里有两个独立的元素。仅 supportedRuntime 元素适用于您的方案。


There are two separate elements at play, here. Only the supportedRuntime element applies to your scenario.

supportedRuntime 元素按首选顺序定义将在其上运行您的应用程序的CLR版本。如果列出受支持的运行时,则将使用这些CLR版本,从上到下从列表中向下查找,直到找到已安装的CLR版本。如果未列出支持的运行时,则程序将使用编译时所针对的CLR版本运行。

The supportedRuntime element defines the CLR versions on which your app will run, in the preferred order. If you list supported runtimes, then those CLR versions will be used, going down the list from top to bottom until an installed CLR version is found. If you don't list support runtimes, then your program will run with the version of the CLR against which it was compiled.

useLegacyV2RuntimeActivationPolicy 元素仅适用于混合模式程序集---包含托管(.Net )和非托管(本机)代码。您的示例程序不是混合模式程序集。对于混合模式程序集,将值设置为 false (默认值),或者不全部设置,将使用新的.Net 4进程内并行加载混合模式程序集,因此您的应用程序可以与CLR 4一起运行,并使用CLR 1.0-2.0在同一过程中加载混合模式程序集。将其设置为 true 实质上会还原到.Net 4之前的先前功能,在该功能中,进程内并行功能被禁用,并且选择了任何CLR版本运行该应用程序将尝试加载您的混合模式程序集。
用于加载混合模式程序集的CLR版本将是选择哪个版本来运行该应用程序,具体取决于用于编译该应用程序的版本以及您列出的受支持的运行时。

The useLegacyV2RuntimeActivationPolicy element applies only to mixed-mode assemblies --- programs or DLLs that contain managed (.Net) and unmanaged (native) code. Your sample program isn't a mixed-mode assembly. For mixed-mode assemblies, setting the value to false (the default), or not setting it all, uses the new .Net 4 in-process side-by-side loading for mixed-mode assemblies, so your app can run with CLR 4, and load a mixed-mode assembly in the same process using CLR 1.0-2.0. Setting it to true essentially reverts to the previous functionality prior to .Net 4, where the in-process side-by-side functionality is disabled, and whatever CLR version is selected to run the app will attempt to load your mixed-mode assembly. The CLR version used to load the mixed-mode assembly will be whichever one is selected to run the app, based on which version was used to compile the app, and your listed supported runtimes, if any.

有一个 MSDN杂志文章和有关.Net的 MSDN文章 4加载和COM组件的进程内并行(In-Proc SxS)执行,这也会对没有COM组件的情况产生影响。在.Net 4之前,如果您使用版本的CLR编译应用程序,并且该版本在运行时在系统上不可用,则该应用程序将在新版本的CLR上自动运行(如果已安装)。从.Net 4开始,除非您在 supportedRuntimes 元素中指定了新版本,否则应用程序现在将无法使用CLR的更高版本。

There's an MSDN Magazine article and an MSDN article about .Net 4 loading and In-Process Side-by-Side (In-Proc SxS) execution for COM components, which also has an impact on your scenario without COM components. Prior to .Net 4, if you compiled your app with a version of the CLR, and that version wasn't available on the system at runtime, the app would automatically run on a newer version of the CLR if it was installed. As of .Net 4, apps now won't run with a newer version of the CLR unless you specify the newer version in the supportedRuntimes element.

以下是MSDN文章的引文:

Here's a quote from the MSDN article:


应用程序开发人员。并行托管对应用程序开发人员几乎没有影响。默认情况下,应用程序始终在其构建时所使用的.NET Framework版本上运行;这没有改变。但是,开发人员可以覆盖此行为,并指导应用程序在新版本的.NET Framework下运行(请参阅方案2)。

Application developers. Side-by-side hosting has almost no effect on application developers. By default, applications always run against the version of the .NET Framework they were built on; this has not changed. However, developers can override this behavior and direct the application to run under a newer version of the .NET Framework (see scenario 2).

库开发人员和使用者。并行托管不能解决库开发人员面临的兼容性问题。由应用程序直接加载的库(通过直接引用或通过Assembly.Load调用)继续使用加载到其中的AppDomain的运行时。您应该针对要支持的所有.NET Framework版本测试您的库。如果应用程序是使用.NET Framework 4运行时编译的,但包含使用较早版本的运行时构建的库,则该库也将使用.NET Framework 4运行时。但是,如果您有一个使用较早的运行时构建的应用程序和一个使用.NET Framework 4构建的库,则必须强制您的应用程序也使用.NET Framework 4(请参阅方案3)。

Library developers and consumers. Side-by-side hosting does not solve the compatibility problems that library developers face. A library that is directly loaded by an application -- either through a direct reference or through an Assembly.Load call -- continues to use the runtime of the AppDomain it is loaded into. You should test your libraries against all versions of the .NET Framework that you want to support. If an application is compiled using the .NET Framework 4 runtime but includes a library that was built using an earlier runtime, that library will use the .NET Framework 4 runtime as well. However, if you have an application that was built using an earlier runtime and a library that was built using the .NET Framework 4, you must force your application to also use the .NET Framework 4 (see scenario 3).

最后,如果您使用的是Vista,Win7,Server 2008,Server 2008 R2,您会自动安装CLR 2.0 。因此,如果要删除 supportedRuntimes 元素,或将其更改为v2.0.50727,则可能仍可以将CLR 2.0用作运行时。

Finally, if you're on Vista, Win7, Server 2008, Server 2008 R2, you automatically have CLR 2.0 installed. Therefore, if you were to remove your supportedRuntimes element, or change it to v2.0.50727, you might still have CLR 2.0 available as a runtime.

这篇关于在.net 4.0上执行的应用程序在.net 2.0下编译时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆