简体   繁体   中英

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

I'm developing a project on github and publish to nuget. Every version when I create I have to build multiple .net frameworks and use these dll's for the nuget package. Is there a easy way to publish all this .net framework versions? Thank you.

  1. Target Framework Moniker (TFM) compatibility

There exists TFM compatibility. In general, lower versions of a TFM family are compatible with higher versions. 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. Similarly, a NuGet package targeting .NET Standard 1.2 can be used in a project targeting .NET Standard 2.0.

.NET Standard is also special. 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.

Finally, there's a large overlap in APIs between .NET Core's runtime, netcoreapp , and the .NET Framework. 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. 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. 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.

In summary, you might not need to create a NuGet package that targets multiple TFMs. Just select the lowest TFM that has the APIs you need, target that, and let TFM compatibility make it work. 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. Your package doesn't need netstandard1.0 , netstandard1.1 , netstandard1.2 , netstandard1.3 , netstandard2.0 and netstandard2.1 TFMs.

  1. Use SDK style projects

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. 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. 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. 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 ).

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. 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. The .NET Framework class library project template uses the non-SDK style project that doesn't support multi-targeting. Then edit the csproj and change <TargetFramework>netstandard2.0</TargetFramework> to <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 ).

Here's a complete csproj sample that you can start with:

<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.

You use below code for multiple .net framework Like .Net Core 3.1 or higher version:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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