After migrating from .NET Core 2.1 to .NET Core 3.1, publish has a problem
Asked Answered
H

4

9

After migrating my project from .NET Core 2.1 to .NET Core 3.1, everything works fine but when publishing the project, Iget this error:

System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp3.1. Consider updating your TargetFramework to netcoreapp3.1 or later.

Hadst answered 21/11, 2021 at 9:18 Comment(3)
Please show your project file.Supinator
Is the .net core 3.1 there on machine?Nannettenanni
yes, .net core 3.1 is on machineHadst
W
9

Try this first!

For anyone suddenly running into this error for seemingly no reason, the error message may be a red herring. You should try deleting all bin and obj folders first before anything else.

  1. For each project in your solution, locate and delete the obj and bin folders (don't worry, they will be regenerated automatically).
  2. Right-click the solution and click "Clean Solution"
  3. Right-click your startup project and click "Rebuild"

For me, the problem occurred while attempting to publish, even though (a) no changes had taken place, (b) it worked the day before, and (c) I was actually using .NET 6.0!

After failing to fix the issue through any recommended solutions, I went ahead and actually removed the warning from the .targets file in the System.Runtime.CompilerServices.Unsafe package folder (i.e., I suppressed the error), but then the next package in the list started complaining about the same thing.

Solution

Deleting the bin/obj folders and cleaning the solution fixed the issue immediately.

Winburn answered 20/12, 2022 at 21:1 Comment(1)
OMG, cleaning up the bin and obj directories was it for me. Thank you so much!Longplaying
M
1

I resolved the problem after remove the tag <RuntimeIdentifier> from the publish profile.

Meneau answered 11/1, 2022 at 7:24 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Tree
T
0

The root cause of the issue for me is, one of the NuGet packages is referring to "Microsoft.Extensions.DependencyModel" v6.0.0. After downgrading the "Microsoft.Extensions.DependencyModel" NuGet to version 5.0.0 issue is resolved.

Please refer to the link for more info, https://github.com/dotnet/runtime/issues/62561

Taken answered 24/10, 2022 at 17:44 Comment(0)
B
-1

@@Morteza install System.Runtime.CompilerServices.Unsafe version 6.0.0 from NuGet into your project.

Install-Package System.Runtime.CompilerServices.Unsafe -Version 6.0.0
Brookes answered 21/11, 2021 at 9:42 Comment(1)
it does not workHadst

© 2022 - 2024 — McMap. All rights reserved.