user3497702
user3497702

Reputation: 851

Need to update all Nuget packages to latest version in a solution that has projects which targets both .NET 4.7.1 and .NET 8

I have many solutions, and each solution has many projects. Some projects may be used in multiple solutions.

The ASP.NET MVC web app is targetting .NET 4.7.1. The API and library project both target .NET 4.7.1 and .NET 4.6. The ASP.NET MVC web app and the API reference the library project.

I migrated the API and Library to .NET 8.0 and it works.

There are many NuGet packages referenced in this solution which are deprecated, vulnerable and old versions. So I try updated all the Nuget packages to the latest version, but found challenges.

Some Nuget packages in their latest version are not compatible with .NET 4.7.1, so those are not getting installed.

Since the library project targets both .NET 4.7.1 and .NET 8.0, I don't know whether to update such nuget packages, and need advise here.

Can I convert this library to a .NET Standard project that is compatible with both .NET 4.7.1 and .NET 8.0 ? If I do that, can I update the NuGet packages to their latest versions, since it is not targeting .NET 4.7.1 but it should work with the ASP.NET MVC web app which is on .NET 4.7.1.

Or should I refactor the code which uses that NuGet package when converting to .NET Standard ?

The question is not specific to any particular NuGet package, since there are many NuGet packages, and need advise.

Please advise.

Upvotes: 1

Views: 409

Answers (1)

J.Memisevic
J.Memisevic

Reputation: 1454

It depends on few things, but for the most NuGet packages that you use you can convert them to target .netstandard2.0 and they will be compatible with .Net8 and .NET 4.7.1.

Biggest issue here if your NuGet packages have dependencies that are not compatible with .netstandard2.0 and they are framework specific.

For example you have in you library a package that is compatible with .NET4.7.1 - and have replacement that targets .NET8 then you should have a NuGet with multitarget frameworks:

 <TargetFrameworks>net8.0;net4.7.1</TargetFrameworks>

and in code use directives to implement diffent packages and have different packages as dependecies.

For example:

    <ItemGroup Condition="'$(targetframework)'=='net6.0'">
        <PackageReference Include="SkiaSharp" Version="2.88.6" />
        <PackageReference Include="System.Drawing.Common" Version="8.0.1" />
    </ItemGroup>
    <ItemGroup Condition="'$(targetframework)'=='net8.0'">
        <PackageReference Include="SkiaSharp" Version="2.88.6" />
        <PackageReference Include="System.Drawing.Common" Version="8.0.1" />
    </ItemGroup>
  <ItemGroup Condition="'$(targetframework)'=='net48'">
    <Reference Include="System.Net" />
    <Reference Include="System.Net.Http" />
  </ItemGroup>

and in code it would be something like this:

#if NET6_0_OR_GREATER
using SkiaSharp;
#endif
#if NET48
using System.Drawing;
#endif

namespace SomeNameSpace;

public class SomeClass
{
  #if NET6_0_OR_GREATER
        public SKImage Image { get; set; }
#endif
#if NET48
        public Image Image { get; set; }
#endif
}

Upvotes: 0

Related Questions