简体   繁体   English

是否必须在NuGet软件包中指定所有中间目标框架版本?

[英]Do all intermediate target framework versions have to be specified in NuGet packages?

The NuGet documentation on enforced package conventions explains: 关于强制打包约定NuGet文档说明:

NuGet copies assemblies from only a single library folder. NuGet仅从单个库文件夹复制程序集。 For example, suppose a package has the following folder structure: 例如,假设一个程序包具有以下文件夹结构:

 \\lib \\Net20 \\MyAssembly.dll (v1.0) \\MyAssembly.Core.dll (v1.0) \\Net40 \\MyAssembly.dll (v2.0) 

When the package is installed in a project that targets the .NET Framework 4, MyAssembly.dll (v2.0) is the only assembly installed. 当该软件包安装在面向.NET Framework 4的项目中时,MyAssembly.dll(v2.0)是唯一安装的程序集。 MyAssembly.Core.dll (v1.0) is not installed. 未安装MyAssembly.Core.dll(v1.0)。 (One reason why NuGet behaves this way is that MyAssembly.Core might have been merged into version 2.0 of MyAssembly.) (NuGet如此行为的一个原因是MyAssembly.Core可能已合并到MyAssembly的2.0版中。)

In this example, if you want MyAssembly.Core.dll to be installed in a project that targets the .NET Framework 4, you must include it in the Net40 folder as well as in the Net20 folder. 在此示例中,如果要将MyAssembly.Core.dll安装在以.NET Framework 4为目标的项目中,则必须将其包含在Net40文件夹以及Net20文件夹中。

The rule about copying assemblies from only one folder also applies to the root lib folder. 仅从一个文件夹复制程序集的规则也适用于根lib文件夹。 Suppose a package has the following folder structure: 假设程序包具有以下文件夹结构:

 \\lib \\MyAssembly.dll (v1.0) \\MyAssembly.Core.dll (v1.0) \\Net40 \\MyAssembly.dll (v2.0) 

In projects that target the .NET Framework 2.0 and the .NET Framework 3.5, NuGet copies both MyAssembly.dll and MyAssembly.Core.dll. 在针对.NET Framework 2.0和.NET Framework 3.5的项目中,NuGet会同时复制MyAssembly.dll和MyAssembly.Core.dll。 But as was true of the previous example, in projects that target the .NET Framework 4, only MyAssembly.dll *from the *Net40 folder will be copied. 但是,与前面的示例一样,在面向.NET Framework 4的项目中,将仅复制* Net40文件夹中的MyAssembly.dll。

So, for the second example, an explanation is provided what happens when loading the package into .NET 3.5 projects. 因此,对于第二个示例,提供了一个解释,将包加载到.NET 3.5项目时会发生什么。

Unfortunately, this information is missing for the first example. 不幸的是,第一个示例缺少此信息。 What files will .NET 3.5 projects get from the NuGet package shown in the first example? .NET 3.5项目将从第一个示例中显示的NuGet包中获取哪些文件? Will .NET 3.5 projects simply get no files, or will they get the library targeted at the highest framework version supported by backwards compatibility in the target project (for .NET 3.5 projects, this would mean the .NET 2.0 version of the library)? .NET 3.5项目将仅获取文件,还是将目标程序中的向后兼容所支持的最高框架版本作为目标库(对于.NET 3.5项目,这将意味着该库是.NET 2.0版本)?

There are two concrete implications of why I am interested in this question: Assume I am providing an assembly for which I have compiled a .NET 2.0 version (which should be used for all .NET Framework versions from 2.0 to 4.6), and a .NET 4.6.1 version (using some new BCL API introduced in .NET 4.6.1, meant to be used in .NET 4.6.1 and higher). 我对这个问题感兴趣的原因有两个具体含义:假设我提供的程序集已经为我编译了.NET 2.0版本(应将其用于从2.0到4.6的所有.NET Framework版本),以及一个。 NET 4.6.1版本(使用在.NET 4.6.1中引入的一些新的BCL API,打算在.NET 4.6.1及更高版本中使用)。

1) Can I simply declare the two "key" versions in the .nuspec file: 1)我可以简单地在.nuspec文件中声明两个“密钥”版本:

<file src="net20\MyLibrary.dll" target="lib\Net20"/>
<file src="net461\MyLibrary.dll" target="lib\Net461"/>

or do I have to list all intermediate supported versions: 还是我必须列出所有中间支持的版本:

<file src="net20\MyLibrary.dll" target="lib\Net20"/>
<file src="net20\MyLibrary.dll" target="lib\Net35"/>
<file src="net20\MyLibrary.dll" target="lib\Net40"/>
<file src="net20\MyLibrary.dll" target="lib\Net403"/>
<file src="net20\MyLibrary.dll" target="lib\Net45"/>
<file src="net20\MyLibrary.dll" target="lib\Net451"/>
<file src="net20\MyLibrary.dll" target="lib\Net452"/>
<file src="net20\MyLibrary.dll" target="lib\Net46"/>
<file src="net461\MyLibrary.dll" target="lib\Net461"/>

? 1 1个

2) Likewise, once I have published this package, will it work for future versions of the framework, or will I need to update the package as soon as there is .NET 4.6.2, 4.6.3, 4.7, or in general, anything greater than .NET 4.6.1? 2)同样,一旦发布了此软件包,它将适用于该框架的未来版本,还是在存在.NET 4.6.2、4.6.3、4.7或一般情况下,我需要立即更新该软件包,什么比.NET 4.6.1大?

Please be aware that I am asking only one question. 请注意,我只问一个问题。 I have merely written it down in three different ways to illustrate its significance. 我只是以三种不同的方式写下来说明其重要性。

1 : This enumeration is based on the list of supported target frameworks at the time of writing. 1 :此枚举基于撰写本文时受支持的目标框架列表 Furthermore, note that I do not want to support .NET 1.1 (nor any other frameworks), hence I do not want to add any libraries directly to the lib folder as shown in the second example in the documentation cited above. 此外,请注意,我不想支持.NET 1.1(也不支持任何其他框架),因此,我不想将任何库直接添加到lib文件夹中,如上面引用的文档中的第二个示例所示。

With a NuGet package containing: 使用包含以下内容的NuGet软件包:

\lib
    \Net20
        \MyAssembly.dll (v1.0)
        \MyAssembly.Core.dll (v1.0)
    \Net40
        \MyAssembly.dll (v2.0)

If the above is installed into a project that targets .NET 3.5 then the assemblies from the Net20 folder will be referenced. 如果将上述内容安装到面向.NET 3.5的项目中,则将引用Net20文件夹中的程序集。 NuGet will pick the target .NET framework version which has the same version or older since it considers all these versions compatible. NuGet将选择具有相同版本或更旧版本的目标.NET Framework版本,因为它认为所有这些版本都兼容。

So you do not need to have a directory in the NuGet package for every framework version. 因此,您无需在每个框架版本的NuGet包中都有一个目录。

The general rule is that your NuGet package should have directories only for those frameworks for the assemblies you include actually target. 一般规则是,您的NuGet软件包应只为那些实际包含的程序集的框架提供目录。 When a newer version of the .NET framework is released the NuGet package can still be used. 当发布.NET框架的较新版本时,仍然可以使用NuGet软件包。 So in your second example I would just create the NuGet package using the simpler .nuspec file: 因此,在第二个示例中,我将使用更简单的.nuspec文件创建NuGet包:

<file src="net20\MyLibrary.dll" target="lib\Net20"/>
<file src="net461\MyLibrary.dll" target="lib\Net461"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM