简体   繁体   English

为多个.NET Framework版本创建NuGet包

[英]Creating NuGet package for multiple .NET Framework versions

I'm developing a library and want to create NuGet package for multiple versions of .NET Framework. 我正在开发一个库,并希望为多个版本的.NET Framework创建NuGet包。 I didn't find any article with full list of actions to do this. 我没有找到任何包含完整行动列表的文章。

The first option I've tried was creation of bunch of configurations like Release_Net45 and so on. 我尝试的第一个选项是创建一些配置,如Release_Net45等。 I then edited csproj to correct output directories (lib/net45 and so on) and target framework versions for each configuration. 然后我编辑了csproj以更正每个配置的输出目录(lib / net45等)和目标框架版本。 Then I built each configuration. 然后我构建了每个配置。 After that I ran nuget pack and package was succesfully created with one warning that one dll was not added since it is already in the package... 之后我运行nuget pack并成功创建了一个包,警告说没有添加一个dll,因为它已经在包中了...

Then I've tried to edit csproj to include TargetFrameworks element and elements for package metadata to the PropertyGroup corresponding to the Release configuration. 然后我尝试编辑csproj以将TargetFrameworks元素和包元数据的元素包含到与Release配置对应的PropertyGroup中。 After reloading the project after editing, new group called Dependencies was appeared in the Solution Explorer. 在编辑完重新加载项目后,解决方案资源管理器中出现了一个名为Dependencies的新组。 I made a try to create package with msbuild /t:pack /p:Configuration=Release in Developer Command Prompt but it was finished with error that there is no pack target. 我尝试使用msbuild / t:pack / p:Configuration = Release在Developer命令提示符下创建包,但是它已经完成,但是没有包目标。 To fix it I added NuGet.Build.Tasks.Pack package and after that the package was built. 为了修复它,我添加了NuGet.Build.Tasks.Pack包,然后构建了包。 BUT my configurations suddenly became corrupted when I looking at them in Configuration Manager. 但是当我在Configuration Manager中查看它们时,我的配置突然损坏了。

I didn't expect that setting up a package for multiple target frameworks will be such painful :( What is the correct way to do this? Is there any complete guide for this task? 我没想到为多个目标框架设置一个包将是如此痛苦:(这样做的正确方法是什么?这个任务有没有完整的指南?

The TargetFramework / TargetFrameworks properties are only supported in SDK-based projects. TargetFramework / TargetFrameworks属性仅在基于SDK的项目中受支持。 Currently, only the project templates for .NET Standard, .NET Core and ASP.NET Core projects use this SDK-based project format. 目前,只有.NET Standard,.NET Core和ASP.NET Core项目的项目模板使用这种基于SDK的项目格式。 So you can create a .NET Standard library and then edit the csproj (right click on the project > edit). 因此,您可以创建.NET标准库,然后编辑csproj(右键单击项目>编辑)。

A minimal example would be: 一个最小的例子是:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net45;net461</TargetFrameworks>
  </PropertyGroup>
</Project>

Notice the presence of the Sdk attribute which provides a lot of defaults and additional build logic. 注意Sdk属性的存在,该属性提供了许多默认值和额外的构建逻辑。 You do not need to specify any .cs files in the folder structures and can use the project's property pages to set NuGet-related properties. 您不需要在文件夹结构中指定任何.cs文件,并且可以使用项目的属性页来设置与NuGet相关的属性。

The reason the dependencies node was shown in your case is because VS 2017 ships with two distinct project systems for .NET projects - the classic one that has been in VS for a long time and a new one being developed on a new project system model (CPS) which doesn't support all the features of the classic project system yet but has additional ones like a new dependencies model, property pages for NuGet and support for editing project files while projects are loaded. 在您的案例中显示依赖关系节点的原因是因为VS 2017为.NET项目提供了两个不同的项目系统 - 经典的一个已经在VS中很长一段时间,而一个新的项目系统是在一个新的项目系统模型上开发的( CPS)不支持经典项目系统的所有功能,但还有其他功能,如新的依赖项模型,NuGet的属性页以及在加载项目时支持编辑项目文件。 The selection which project system to load is based on the presence of either TargetFramework or TargetFrameworks in the project file. 选择要加载的项目系统是基于项目文件中是否存在TargetFrameworkTargetFrameworks

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

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