什么时候以及为什么需要supportedRuntime元素和sku属性? [英] When and why do I need the supportedRuntime element and sku attribute?

查看:628
本文介绍了什么时候以及为什么需要supportedRuntime元素和sku属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio中创建的大多数(如果不是全部)C#(以及F#和VB)库和可执行项目中,都有一个自动添加的 app.config 文件,用于指定运行时版本和目标框架标记(TFM):

 < configuration> 
< startup>
< supportedRuntime版本= v4.0 sku =。NETFramework,Version = v4.0 />
。 。 。

即使完全没有 app.config 文件,编译器似乎总是生成程序集级的属性,如ILDASM所示:

  .custom实例void [mscorlib] System。 Runtime.Versioning.TargetFrameworkAttribute ::。ctor(string)=(01 // .... NETFramework 
.. //,Version = v4.6.1。
字节已被摘除-> .. // .T..FrameworkDis
.. // playName..NET Fr
61)// amework 4.6.1

.csproj 文件确实指定了目标框架,我想这就是目标所在的位置



在没有< startup> 的情况下,可执行文件运行得很好。配置文件中的部分。



如果我们省略了app.config,Windows将尝试运行它,然后应用程序第一次遇到.NET Framework 4.7.2特有的功能且已安装的框架版本中缺少该功能时,它将崩溃。



请注意,文档具有误导性,即 此元素应由使用.NET Framework 1.1版或更高版本构建的所有应用程序使用。 。它可能被解释为 该元素是应用程序在.NET 1.1+上运行所必需的,而实际上,这仅意味着.NET 1.1更改了以前在.NET 1.0中使用的语法 requiredRuntime 语法。通常,不需要 supportedRuntime 即可运行应用程序,这只是为了美观。



一种常见的情况是 supportedRuntime 确实是应用程序运行所需要的,这是当我们具有面向.NET 2.x-3.x的应用程序并尝试在计算机上运行它时仅具有4.x版本(例如,Windows 10具有4.6+版本,但默认情况下未安装.NET 2.x-3.x版本)。在这种情况下,即使4.x与以前的版本兼容,在app.config中没有 supportedRuntime 的情况下,该应用程序也将不会运行。添加< supportedRuntime version = v4.0 sku =。NETFramework,Version = v4.0 /> 将解决此问题。






总而言之,它不会复制程序集元数据中的信息,而是为Windows提供有关如何将应用程序与框架版本连接的其他信息它与目标计算机不兼容,并且要求用户安装哪个版本。


In most (if not all) C# (and F# and VB) library and executable projects created in Visual Studio there is an automatically added app.config file that specifies runtime version and target framework moniker (TFM):

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
. . .

Even absent the app.config file entirely, the compiler seems to always generate an assembly-level attribute, as ILDASM shows:

.custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01    // ....NETFramework
                                                                                                      ..    // ,Version=v4.6.1.
                                                                                      bytes snipped-> ..    // .T..FrameworkDis
                                                                                                      ..    // playName..NET Fr
                                                                                                      61  ) // amework 4.6.1

The .csproj file does specify the target frameworks, and my guess this is where the target is passed from on to the compiler during build.

The executable seems to run just fine without the <startup> section in the config file. The documentation explains what do the attributes mean, but, seeing them for many years, I never understood why they are needed in the configuration file. I mostly dealt with desktop applications for Windows, however.

This answer explicitly states that "making a program compiled to target .NET 4.0 behave like it runs on a higher version is not possible," and I would be really surprised if, conversely, running a program on a lower version of the framework were also possible.

So, under what scenarios does the application developer need to specify the version and TFM of the runtime in the .config file of the application, and does it have to always duplicate information that is hardcoded into the binary by the compiler? The requirement seems counterintuitive at first sight.


UPDATE 2018-06-29: X-ref: I asked for a clarification of the documentation in the GitHub issue dotnet/docs#6234.

解决方案

It's needed to declare which framework versions your application is actually compatible with. Suppose, we have an application that targets .NET Framework 4.7.2 and try to run it on the machine that have only .NET Framework 4.5 installed. If we add this app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup></configuration>

Windows will show a nice error message that asks to install needed framework version:

If we omit the app.config, Windows will try to run it and then application will crash first time it hits a feature specific to .NET Framework 4.7.2 and absent in installed framework version.

Note that documentation is misleading in saying that "This element should be used by all applications built with version 1.1 or later of the .NET Framework". It might be interpreted as "This element is required for application to run on .NET 1.1+", while in reality it only means that .NET 1.1 changed a syntax from previosly used in .NET 1.0 requiredRuntime syntax. More often then not supportedRuntime is NOT required for application to run, it's just for prettiness.

One common scenario when supportedRuntime is really needed for application to run is when we have application targeting .NET 2.x-3.x and try to run it on machine that have only 4.x (for example, Windows 10 have 4.6+ but does not have .NET 2.x-3.x installed by default). In this case, without supportedRuntime in app.config the application won't run at all, even though 4.x is mostly compatible with previous versions. Adding <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> will fix the issue.


So, to sum up, it does not duplicate the information in assembly metadata, but rather give Windows additional information on how to connect application with framework version it is compatible with, and what version to ask user to install if it's not present on target machine.

这篇关于什么时候以及为什么需要supportedRuntime元素和sku属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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