How to fix: Add a reference to ".NETFramework,Version=v4.7.1" in the "TargetFrameworks" property of your project file and then re-run NuGet restore?

Viewed 10968

And yes, I have tried all similar QAs and nothing helped.

What I have tried, none helped:

  1. Delete bin/ & obj/
  2. Delete project.assets.json
  3. Delete .vs/
  4. Reset entire git repo
  5. Clean nuget caches
  6. Change .NetFramework version

What I know:

  1. This happened to my colleague month ago on Win7, VS19 15.x, update did nothing. Thought she was crazy and fucked up something in system. Now happened to me, Win10, VS19, all up to date.
  2. Yesterday VS seamed slow, so I have deleted .vs, could be related.
  3. I have isolated first affected project, it is pure c# .net framework 4.7.1 lib, no other dependencies than system .net libs, does not have any nuget reference.
  4. After all that deleting and reseting one thing helped. I have renamed DotNetExtensions.csproj to DotNetExtensions1.csproj and now shit builds like a charm, but when I rename it back same error again.

WTF? How is this possible? What else can i clean?

Full log:

Rebuild started...
1>------ Rebuild All started: Project: DotNetExtensions, Configuration: Release Any CPU ------
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\NuGet\16.0\Microsoft.NuGet.targets(198,5): error : Your project does not reference ".NETFramework,Version=v4.7.1" framework. Add a reference to ".NETFramework,Version=v4.7.1" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
5 Answers

I tried to port my projects to .net 5.0 and encountered this problem when reverting back to 4.8. This worked for me:

  1. Clean project
  2. Delete bin and obj folders
  3. Builds again.

I had a very similar error except it was:

Your project does not reference ".NETFramework,Version=v4.8" framework. Add a reference to ".NETFramework,Version=v4.8" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.

What worked for me was to use Visual Studio to 1) clean the solution and then 2) delete all obj and bin folders. Then it built without restarting VS.

I think it was a bad cache of the project.assets.json file. It must have gotten corrupt when restoring a stash that required a merge. VS didn't automatically load the projects. It isn't related to the TargetFrameworks because when you add that property to the csproj file, a different build step fails.

I got the same problem using .net framework 4.8 and visual studio 16.10.2 and I fixed it using below options

  • Delete Bin/Obj folder from project folder
  • Close Visual studio and reopen
  • Clean Solution

Solved my problem.

If still this not help you then

  • Delete .vs folder from your solution folder (if exists)
  • Clear nuget cache (Tools/Options/Nuget Package Manager/ Clear nuget package cache button).
  • Delete folder (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files)

Get a log file and run it through the MSBuild Structured Log Viewer (see the readme for instructions). If things work with one name but not with the other, it sounds like it's either caching or that the name of the project (DotNetExtensions) conflicts with something in the build process. In either case, the log files will contain clues.

It may also be that a dependent NuGet package has been updated to require .NET Framework 4.7.1, in which case actually re-running the NuGet restore in full might be necessary, particularly on old-style .csprojs where changing the target framework still keeps the current package versions around. In that case you have to delete the packages folder, if it exists, or target the previous package version that made everything work.

delete bin and obj folders , then build must solve the problem

Related