简体   繁体   English

如何为 nuget 包构建具有多个 .net 框架的项目

[英]How can I build a project with multiple .net frameworks for nuget package

I'm developing a project on github and publish to nuget.我正在 github 上开发一个项目并发布到 nuget。 Every version when I create I have to build multiple .net frameworks and use these dll's for the nuget package.当我创建每个版本时,我必须构建多个 .net 框架并将这些 dll 用于 nuget 包。 Is there a easy way to publish all this .net framework versions?有没有一种简单的方法来发布所有这些 .net 框架版本? Thank you.谢谢你。

  1. Target Framework Moniker (TFM) compatibility目标框架名称 (TFM) 兼容性

There exists TFM compatibility.存在 TFM 兼容性。 In general, lower versions of a TFM family are compatible with higher versions.通常,较低版本的 TFM 系列与较高版本兼容。 So a .NET Framework 4.7 assembly can be used in a project targeting .NET Framework 4.7.1, 4.7.2 or 4.8.因此,.NET Framework 4.7 程序集可用于面向 .NET Framework 4.7.1、4.7.2 或 4.8 的项目。 Similarly, a NuGet package targeting .NET Standard 1.2 can be used in a project targeting .NET Standard 2.0.同样,面向 .NET Standard 1.2 的 NuGet 包可用于面向 .NET Standard 2.0 的项目。

.NET Standard is also special. .NET Standard 也很特别。 It's not a runtime, it's a specification that runtimes implement.它不是运行时,而是运行时实现的规范。 Just like the docs on .NET Standard say, although .NET 4.6.1 was marked as compatible with .NET Standard 2.0 and below, there are issues, so it's recommended to use at least .NET Framework 4.7.2 or 4.8 is using packages, or project references, that target .NET Standard.就像.NET Standard 上的文档所说,虽然 .NET 4.6.1 被标记为与 .NET Standard 2.0 及以下版本兼容,但还是存在问题,因此建议至少使用 .NET Framework 4.7.2 或 4.8 正在使用的包或项目引用,目标是 .NET Standard。

Finally, there's a large overlap in APIs between .NET Core's runtime, netcoreapp , and the .NET Framework.最后,.NET Core 的运行时、 netcoreapp和 .NET Framework 之间的 API 有很大的重叠。 So, even though there are incompatibilities, in order to allow .NET Core applications to use NuGet packages that might be incompatible, .NET Core SDK 2.0 introduced a concept known as target fallback.因此,即使存在不兼容性,为了允许 .NET Core 应用程序使用可能不兼容的 NuGet 包,.NET Core SDK 2.0 引入了一个称为目标回退的概念。 If a project is not directly compatible with a package, if the package supports one of the target fallback TFMs, NuGet will allow it, with a warning.如果项目与包不直接兼容,如果包支持目标回退 TFM 之一,NuGet 将允许它,并显示警告。 It might fail at runtime, hence the warning, but it might work.它可能会在运行时失败,因此会发出警告,但它可能会起作用。 This is more important with .NET Core 3.0, with developers writing desktop apps in WPF and WinForms, wanting to use .NET Framework NuGet packages that provide desktop functionality.这对于 .NET Core 3.0 更为重要,开发人员在 WPF 和 WinForms 中编写桌面应用程序,希望使用提供桌面功能的 .NET Framework NuGet 包。

In summary, you might not need to create a NuGet package that targets multiple TFMs.总之,您可能不需要创建面向多个 TFM 的 NuGet 包。 Just select the lowest TFM that has the APIs you need, target that, and let TFM compatibility make it work.只需选择具有您需要的 API 的最低 TFM,将其作为目标,然后让 TFM 兼容性使其发挥作用。 However, if you want to use APIs only available in newer TFMs, while still supporting older TFMs, then you will need to multi-target your project & package, but again, you can minimise the number of TFMs you need to support.但是,如果您想使用仅在较新的 TFM 中可用的 API,同时仍支持较旧的 TFM,那么您将需要多目标您的项目和包,但同样,您可以最大限度地减少您需要支持的 TFM 数量。 Your package doesn't need netstandard1.0 , netstandard1.1 , netstandard1.2 , netstandard1.3 , netstandard2.0 and netstandard2.1 TFMs.您的软件包不需要netstandard1.0netstandard1.1netstandard1.2netstandard1.3netstandard2.0netstandard2.1 TFM。

  1. Use SDK style projects使用 SDK 风格的项目

Assuming you're developing with Visual Studio, install the .NET Core workload, even if you don't plan to do cross platform (Linux, Mac) development.假设您使用 Visual Studio 进行开发,请安装 .NET Core 工作负载,即使您不打算进行跨平台(Linux、Mac)开发。 With .NET Core, the project file was changed to be significantly simpler, and at the same time so much more powerful, including built-in support for targeting multiple TFMs in a single project file.使用 .NET Core,项目文件变得更加简单,同时功能更加强大,包括针对单个项目文件中的多个 TFM 的内置支持。 Previously it was necessary to create one csproj per TFM you wanted in your package, compile each project, then have a nuspec that pulls in everything into the right place.以前,您需要在包中为每个所需的 TFM 创建一个csproj ,编译每个项目,然后使用nuspec将所有内容放入正确的位置。 Now, simple have a single csproj that lists all the TFMs you want to target, and run dotnet pack (don't create a nuspec, the package metadata come from MSBuild properties ).现在,简单地有一个csproj列出您想要定位的所有 TFM,并运行dotnet pack (不要创建 nuspec, 包元数据来自 MSBuild 属性)。

So, once you have the .NET Core SDK installed, you can either create a .NET Standard class library from Visual Studio, or run dotnet new classlib on the command line.因此,一旦安装了 .NET Core SDK,就可以从 Visual Studio 创建一个 .NET Standard 类库,或者在命令行上运行dotnet new classlib It doesn't matter if you want to target the .NET Framework, create the .NET Standard class library, because that's how you get a SDK style project.如果您想以 .NET Framework 为目标,创建 .NET Standard 类库并不重要,因为这就是您获得 SDK 样式项目的方式。 The .NET Framework class library project template uses the non-SDK style project that doesn't support multi-targeting. .NET Framework 类库项目模板使用不支持多目标的非 SDK 样式项目。 Then edit the csproj and change <TargetFramework>netstandard2.0</TargetFramework> to <TargetFrameworks>net472;netstandard2.0</TargetFrameworks> .然后编辑csproj并将<TargetFramework>netstandard2.0</TargetFramework>更改为<TargetFrameworks>net472;netstandard2.0</TargetFrameworks> Notice that the TargetFramework (singular) became TargetFrameworks (plural), and the contents of the XML element is a semicolon delimited list of TFMs you want your project to target ).请注意, TargetFramework (单数)变成了TargetFrameworks (复数),并且 XML 元素的内容是以分号分隔的您希望项目定位TFM列表)。

Here's a complete csproj sample that you can start with:这是一个完整的csproj示例,您可以从它开始:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
    <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
  </PropertyGroup>

</Project>

As previously stated, once you have a csproj that looks like this, simply run dotnet pack from the command line (you can also right-click the project in Visual Studio's Solution Explorer, and select Pack), and you'll get a nupkg that both net472 and netstandard2.0 compiled assemblies in the correct locations in the package.如前所述,一旦你有一个看起来像这样的csproj ,只需从命令行运行dotnet pack (你也可以在 Visual Studio 的解决方案资源管理器中右键单击该项目,然后选择 Pack),你会得到一个nupkg net472netstandard2.0编译的程序集都位于包中的正确位置。

You use below code for multiple .net framework Like .Net Core 3.1 or higher version:您将以下代码用于多个 .net 框架,例如 .Net Core 3.1 或更高版本:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
        <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
      </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.7">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\OnlineShoppingCart\OnlineShoppingCart.csproj" />
  </ItemGroup>

</Project>

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

相关问题 如何使nuget包正确地针对多个框架? - How do I make a nuget package target multiple frameworks correctly? 我是否应该在NuGet包中包含多个目标框架的dll - Should I include in NuGet package dlls for multiple target frameworks 如何通过构建将nuget包添加到项目(不是解决方案) - How to add nuget package to project (not solution) with build 如何针对同一版本将多个.net框架作为目标 - How to target multiple .net frameworks for same build 如何在NuGet包中嵌入/引用.NET引用? - How can I embed / reference .NET references in a NuGet package? 由于NuGet程序包还原错误,.NET项目的VS Build失败 - VS Build fails for .NET project because of NuGet package restore errors 引用 a.Net Core 3.1 NuGet package 的 a.Net 5 Visual Studio 项目/解决方案如何编译成功? - How can a .Net 5 Visual Studio Project / Solution that reference a .Net Core 3.1 NuGet package compile successfuly? 如何将 .Net Standard Nuget 包添加到 .Net 4.5 Framework 项目中? - How to add .Net Standard Nuget package into .Net 4.5 Framework Project? 解决多个标准框架的 NuGet 包 - 可空值问题 - NuGet package addressing multiple Standard frameworks - problem with nullables 如何将nuget包.Net Standard嵌入到项目C#中? - How to embed nuget package .Net Standard into a project C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM