Home > Back-end >  Right way to produce multiple versions of Nuget package
Right way to produce multiple versions of Nuget package

Time:01-07

I am working on a NuGet package that I want to be compatible with multiple Framework versions.

As an example, let's say I want to have methods available to support a new feature of .NET 7 like Int128. When the consumer of the package is a .NET 7 application, they will get that functionality, but if they are on a lower version then they won't have that functionality available.

What is the correct method to solve this problem? Would producing different packages for different versions be the right way or is there a more elegant way to solve this problem?

This code will work in .NET 7, but not .NET 6. How do I have a NuGet package or compiler hints to support either framework version.

public Int128 ToInt128(IFormatProvider? provider) { return (Int128)IntegerValue; }

Is it traditional to have multiple versions of a NuGet package available? Like MyStuff.Library.6.0 and MyStuff.Library.7.0.

CodePudding user response:

You publish a single package version that targets multiple frameworks, and use #if etc in the source code.

For example, my NodaTime package, version 3.1.0 targets TFMs net6.0 and netstandard2.0. It targets net6.0 so that I can provide conversions for DateOnly and TimeOnly.

So in the project file, I have:

<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>

... and then in the source for DateOnlyExtensions I have:

#if NET6_0_OR_GREATER
// Code here
#endif
  • Related