41

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 ==========
watbywbarif
  • 6,487
  • 8
  • 50
  • 64
  • 1
    Could you please share your DotNetExtensions.csproj file with us? Did you use like this `net471;netcore3.1` in the csproj file? – Sara Liu - MSFT Apr 14 '21 at 02:50
  • @SaraLiu-MSFT No, I have old format `v4.7.1` https://1drv.ms/u/s!AsTfLY51CTFzpHmrt2supA4HyJwF?e=yKRqND – watbywbarif Apr 14 '21 at 13:36
  • 1
    It seems that you did not install any nuget packages in your project. PackageReference nuget management format will use `project.assets.json` file under obj folder. At the beginning, the error hints `TargetFrameworks`, I think you have used new-sdk format. In fact, that is not what I expected. Quite strange. Did you try to [repair VS](https://learn.microsoft.com/en-us/visualstudio/install/repair-visual-studio?view=vs-2019)? – Sara Liu - MSFT Apr 15 '21 at 08:21
  • @SaraLiu-MSFT No, but this happened on two different machines on 4 different VS versions (both machines updated VS) on only one project, and remaining 200+ projects are OK. Strangest thing is that renaming project file solves problem on both machines. – watbywbarif Apr 15 '21 at 10:30
  • Maybe you should make a clean, close all VS instances, shut down msbuild.exe,csccompile.exe by task manager, restart VS as Administrator to try again. – Sara Liu - MSFT Apr 16 '21 at 02:52

6 Answers6

92

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.
Gregory Liénard
  • 1,071
  • 3
  • 7
21

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.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
philipwolfe
  • 338
  • 1
  • 7
  • Clean was first thing I did + rebuild + deleting all folders i could think of. Still don't know wtf happened, I just renamed project and now everything works. – watbywbarif Apr 27 '21 at 15:00
9

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)
crokk
  • 91
  • 1
  • 2
5

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

Ali Hatam
  • 81
  • 1
  • 3
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30643623) – Marco Aurelio Fernandez Reyes Dec 21 '21 at 21:53
1

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.

Jesper
  • 7,477
  • 4
  • 40
  • 57
  • Thanks, never tried binary logs, looks useful but I don't see anything new from looking at it, if you can take a look i would be grateful. In the meantime, i will try full nugget restore part. Although as this has no nuget packages don't see how it can solve things. https://1drv.ms/u/s!AsTfLY51CTFzpHjOS0N_WZGI5BnC?e=46wJZ9 – watbywbarif Apr 13 '21 at 10:16
  • In that case it's probably an MSBuild conflict with something also named `DotNetExtensions` in the SDK. It's hard to explain why it suddenly works when renaming the project otherwise. – Jesper Apr 13 '21 at 10:21
  • Could be, this is consistent with appearing in few weeks apart for me and my colleague, it could be something new was added and delivered via update, because this project was building for like 10 years without a problem, code not touched for years and now this. If I don't find cause I will just rename it for time being and see what happens for a while. – watbywbarif Apr 13 '21 at 10:33
  • NuGet.exe restore DotNetExtensions.sln MSBuild auto-detection: using msbuild version '16.9.0.16703' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\bin'. Nothing to do. None of the projects in this solution specify any packages for NuGet to restore. – watbywbarif Apr 13 '21 at 10:38
  • 1
    That definitely sounds like a dependency of some sort - either a NuGet package (which you mentioned not using), or something toolchain related like an SDK, .NET targeting pack or Visual Studio update - changed for both of you over time to make the name of the project cause the error. If there's a third environment where it still works, collect logs from there too and look at the differences. – Jesper Apr 13 '21 at 12:14
0

You can put following statements in a .bat file and save it in your solution root directory:

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S bin') DO RMDIR /S /Q "%%G"
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"

and run the batch file to delete obj and bin of all your projects.

Masoud
  • 8,020
  • 12
  • 62
  • 123