280

I can't run my unit tests.

I have the next error:

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

In app.config:

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>

In Project > Properties > Application > TargetFramework (.NET Framework 4.6.2)

How can I fix it?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272

16 Answers16

598

Please make the next steps

  1. Clean solution
  2. Clean folder "packages"
  3. Delete folder "bin"
  4. Delete folder "obj"
Larissa Savchekoo
  • 6,412
  • 1
  • 13
  • 7
  • 102
    #3 and #4 solved the issue. I had a branch that was an upgrade task to 4.7.2 however I had to switch to another branch that was targeting 4.7.1. Do these steps to solve that issue. – jjhayter Mar 06 '19 at 17:56
  • 6
    Had to do one more step, from the solution directory: del /S project.assets.json – Jannes May 07 '19 at 16:43
  • After doing this clean I found out that I had another error in the project which was displayed the .Net Framework error. Fixing it solved the issue. – meJustAndrew Aug 26 '19 at 11:33
  • 3
    https://stackoverflow.com/a/755433/769137 has batch scripts for deleting these. – Vedran Nov 22 '19 at 14:02
  • no 3 and 4 reslove my issue but I have to exit from the visual studio. – user1154390 Mar 09 '20 at 12:34
  • after deleting `/packages` I had to also run `nuget restore` – imnk Apr 07 '20 at 06:55
  • 7
    I wonder why the `Clean` button doesn't clean the `obj` folder. That would solve this problem. – HackSlash Aug 03 '20 at 16:48
  • You can use this powershell script to delete all bin&object https://gist.github.com/alikrc/f6c08b77d5c58c5b196368169ad24777 – Ali Karaca Aug 11 '20 at 09:55
  • 2
    I upgraded from 4.7.2 to 4.8, and it was sufficient for me to delete the `obj/` folders. – skst Sep 14 '20 at 23:33
  • 2
    any idea of why this annoying bugs happen on a product like vs? – 0x777 Dec 04 '20 at 15:13
  • For Mercurial users (after closing your IDE): `hg purge --all` (**beware**: this deletes all files that are not under version control what just is what you expect in most cases of *clean solution*) – CodeFox Dec 28 '20 at 08:38
  • 8
    It's almost 2022, and it's still happening! $MSFT, please fix it. – Huy Hoang Oct 14 '21 at 13:48
  • Maybe also delete the hidden `.vs/` folder – Leif Jones Dec 10 '21 at 19:17
  • @HackSlash After I deleted the obj folder it suddenly during build could not find the projects.assets.json (this is probably why clean does not clean that folder) https://stackoverflow.com/questions/48440223/assets-file-project-assets-json-not-found-run-a-nuget-package-restore I needed to switch to another minor version of the framework of that project that was complaining (it then generated the file (NPM restore did not do that) then I switched back and the build worked fine again – Null Mar 24 '23 at 07:55
  • @Null I've had it break NuGet packages too. So clean needs to delete obj, then do a NuGet restore. I think the point is that Microsoft could figure this out and fix it. – HackSlash Mar 24 '23 at 21:33
  • In my case I had to remove obj and bin folders from all the projects that composed my solution. Including test projects. – Gorka Lertxundi May 02 '23 at 09:06
63

I experienced similar issue, but with v4.7.2. Namely, I kept getting build log message like this:

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

Despite the fact that it looked similar, none of the above proposed steps worked for me. I kept seeing this message after each build. Nothing seemed to be able to help.

In fact, the problem was related to that, due to migration, I had to put two projects in one folder of code. One of them was targeted at .Net Core, another at .Net Framework, both referenced same .Net Standard libraries. Apparently, they share the same obj folder where Core projects put project.assets.json file. Actually, exactly this file interferres with the Framework project preventing its normal build. Seems even if you performed Migrate from packages.config to PackageReference... which was recommended as one of possible solution.

You can try to fix the issue by putting the following snippet into your Framework project file:

<Project>
  ...
  <PropertyGroup>
    <BaseOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/bin</BaseOutputPath>
    <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/obj</BaseIntermediateOutputPath>
  </PropertyGroup>
  ...
</Project>

It immediately worked for me, it was only later when I attentively read why we need it and why it works. I unexpectedly found it in part 2 of Migrating a Sample WPF App to .NET Core 3 under Making sure the .NET Framework project still builds section. BaseOutputPath and BaseIntermediateOutputPath msbuild variables can be found there, not sure if they are documented well anywhere.

Daniel Fisher lennybacon
  • 3,865
  • 1
  • 30
  • 38
moudrick
  • 2,148
  • 1
  • 22
  • 34
  • 1
    In VS2019 I found that the properties you mention need to be before the OutputPath. I initially just dropped them at the bottom of the csproj with no success. This post has some additional details and implies that this might get fixed at some point: https://github.com/dotnet/msbuild/issues/2070 – John Dyer Sep 01 '20 at 12:38
  • I was trying to migrate a solution from .Net Framework 4.7.2 to Net Core 5 and ran into some issues with a dependency. When I reverted, I was getting this error. Build Clean didn't do anything, but deleting all obj (and bin for good measure) directories got me back on track. – Brandon Barkley Jun 04 '21 at 18:57
  • I solved a similar issue by moving the projects to their separate folders. Having multiple csproj files in the same folder seems to cause problems. – PMF Jul 09 '21 at 14:02
  • Likewise this solved my problem. Had a flat folder structure where sln and project were in the same root folder. Moved the project to src\ and this fixed the build errors on Azure pipelines. – nh43de May 05 '22 at 18:24
63

The file that was causing issue for me was obj/project.assets.json inside project folder. Deleting it and rebuilding the project worked.

Ajith
  • 2,041
  • 1
  • 14
  • 6
45

That happened to me when opening a VS2015 project in VS2017. Deleting the project.assets.json from the obj folder did the trick.

Anyway, the Framework from the message was missing in the file, I did not add it there though, went with deleting it.

tanuk
  • 498
  • 9
  • 10
  • 5
    Simplest solution of all. Just search for the file in solution and delete them at once :) – Imad Aug 06 '20 at 11:33
  • 1
    The Pre-build command "IF EXIST $(ProjectDir)obj\project.assets.json del /F $(ProjectDir)obj\project.assets.json" in the common library worked for me. Thanks. – Michael Erickson Jun 21 '22 at 01:06
45
git clean -xdf

That should do the trick. It worked for us also in Jenkins. (we simply replayed the failed build with a modified script that ran git clean first).

For some reason MSBuild / Visual Studio get confused when switching between branches that target different versions of .NET Framework, so I had to do git cleans regularly while switching between branches.

Shahin Dohan
  • 6,149
  • 3
  • 41
  • 58
9

I had deleted the obj folder and rerun the build after choosing the target framework required in the property window it worked for me.

3Lok
  • 109
  • 1
  • 3
6

I up-voted Larissa but I thought it might be helpful to know how I got into this. I added a .net standard project file to my build (we target lots of platforms) and it produced the debris found in the obj folder. When the android sanity build came around, it threw up on the obj folder. My solution was to clean that folder out as a pre-build step. This is a difficult problem because it was working just fine for years...needle meet haystack.

Pale Ale
  • 478
  • 7
  • 11
6

For my case, delete the .pkgrefgen/ folder under the project folder works, it contains a file project.assets.json that refer to old .net framework

Yitong Feng
  • 243
  • 3
  • 9
  • 4
    Mine was a project.assets.json file in the obj folder. Deleted bin and obj folders and the issue went away. – Ceres Apr 14 '20 at 12:08
4

I ran into the same thing with .net 4.71. In my case, I simply migrated from packages.config to "package references" per

Migrate from packages.config to PackageReference

... and it fixed my issue. For me, I was going to do this anyway, so if you're going this way already, I'd just skip the above and migrate to package references.

ebol2000
  • 1,195
  • 11
  • 16
3

For whatever reason, I got this build error in VS2022.

The same build in VS2019 was successful.

Greg Trevellick
  • 1,361
  • 1
  • 16
  • 26
  • This should be a comment, doesn't really answer the OP's question. – mxmissile Jul 07 '22 at 18:16
  • 1
    I can see it both ways - it doesn't explain the cause, but does offer a solution. I did consider a comment but as none of the answers cured the issue for me I opted to post an answer for others in the same boat as me. – Greg Trevellick Jul 08 '22 at 19:53
2

Renaming the project solved the error for me. The issue happened after I created .NET Core project, then I deleted it and created a .NET Standard one with the same name. Obj folder was not present at all. Deleting bin folder, packages, clean and rebuild solution and getting latest with override did not help.

I have not tried this, but this thread proposed workaround is to include into csproj tag:

<ResolveNuGetPackages>false</ResolveNuGetPackages>
K. B.
  • 3,342
  • 3
  • 19
  • 32
1

I am using a very old .NET project, and it was working fine until it stopped all of a sudden. Upgrading Visual Studio fixed for me thou.

Edgar Froes
  • 768
  • 8
  • 20
1

On VS2019 I had to follow the error message and edit the project.json file that was in the project directory.

was ".NETFramework,Version=v4.0": {} // whatever the copied project was set to
now ".NETFramework,Version=v4.7.2": {} // set to whatever the current build is set to

ergohack
  • 1,268
  • 15
  • 27
1

Problem: In VS2017. Missing reference to .netframework 4.5.2, even though it was referenced as the target framework.

My solution: Verified framework was installed and restarted machine. After a git clean and simply right clicking on the solution in the explore and 'Restore nuget packages' did the trick.

thefoyer
  • 315
  • 3
  • 19
1

I had the same issue in CI/CD process, when i had upgraded .net framework version from 4.6.1 to 4.7.2 which worked fine locally without any other modification.

However, the jenkins 'slave' node where the build was actually getting generated had some issue with nuget restore and it was not able to pick the latest build for some reason.

Logged into jenkins slave machine/node (basically the machine which jenkins uses to create the build/artifact), go to deployment path and then try deleting projects old builds along with .nugets folder and trigger CI/CD process again worked for me.

mabiyan
  • 667
  • 7
  • 25
1

This error also occurs if you have removed an old SDK that provided nuget packages, but it is still referenced in your package sources list under Nuget manager/settings. Remove the nuget package source no longer in use to fix this. Otherwise, Visual studio on building will create the project.assets.json file with a reference to the old sdk and if the path is not there, you get the OP's error. In my case, I had DevExpress 20.2 in my list which I removed to resolve this issue.

enter image description here

Dranyar
  • 580
  • 1
  • 10
  • 16