66

Just a few days ago got this error, after updating to Visual Studio Community 2022 v17.2 (from v17.1.6):

Error NETSDK1005 Assets file 'C:.........XXXXXX.Web\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project. XXXXXX.Web C:\Program Files\dotnet\sdk\6.0.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets 267

  • Uninstalled everything related with VS2022 + Installer
  • Rebooted
  • Fresh Git Cloned the project I'm working on (I work on several computers all with Win 10 and all with the latest updates, this is the only VS installation that presents this problem)
  • Reinstalled VS 2022 v17.2 (with .NET 6.0, the usual install)

The .csproj file has everything in place:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);(SpaRoot)\**\node_modules\**;</DefaultItemExcludes>
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>

Always delete the 'bin' and 'obj' folders before build or rebuild....

Cannot get away with the compilation, and always receiving that NETSDK1005 error...

Getting desperate :(

Thanks in advance for any help

P.S. - already checked question 70711153

LLuz
  • 1,351
  • 1
  • 6
  • 8
  • 1
    My peer had same problem, made him -> signout of all accounts from the Visual Studio -> Close VS -> Reopen VS -> Signin with account that can access Nuget and that fixed for him – prvn Jun 30 '22 at 14:47
  • 2
    In my case I had to update the .pubxml file to the correct [Target Framework](https://stackoverflow.com/a/74964417/1819403) – Anthony Griggs Dec 30 '22 at 17:55
  • I changed publish setting "Modo de implementación" = "Independiente" (deployment mode = Independent) – Chesare Mar 03 '23 at 23:44
  • I was getting this error when trying to do an iOS release build with .net7. and finally found a workaround: I had to remove all target frameworks except net7.0-ios from TargetFrameworks in my proj file when doing a release build. So it looks like: net7.0-ios; (note: I needed to do this in my main app project and also in a dependant class library proj. – Sam Aug 22 '23 at 14:06

25 Answers25

59

I found the problem and it indeed had to do with restoring NuGet Packages, in that I have a connection to a corporate NuGet repository, and the call to it was breaking due to wrong credentials.

What was troubling was that the error did not identify the nature of problem with the connection or the username of the credentials getting refused.

On the logged in user popup dialog window, where the several used usernames are presented, there was one username that was required to re-enter its password.

That was all it took.

Visual Studio > Tools > Options > Azure Service Authentication. ReBuild and the NuGet Packages will be restored and build successful.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
LLuz
  • 1,351
  • 1
  • 6
  • 8
  • I faced the same error and reapplying creds fixed the problem. Thanks – Romesh D. Niriella Jul 11 '22 at 03:26
  • This didn't help me. Still got the same errors. – AH. Sep 20 '22 at 07:15
  • 2
    Thank you for answering, solved my problem. If you are using Rider IDE you can try to run dotnet build --interactive instead. I was getting this warning in the build output: The plugin credential provider could not acquire credentials. Authentication may require manual action. Consider re-running the command with --interactive for `dotnet`, /p:NuGetInteractive="true" for MSBuild. – Vinicius Gonçalves Dec 20 '22 at 15:23
  • Also occurs if you have a corporate NuGet repo and your VPN connection isn't active. – John Meyer May 03 '23 at 19:13
43

We had this issue in our Azure DevOps pipeline and it ended up being that the "NuGet Restore" task was using an old version of NuGet. You can see which version the pipeline is using if you check the logs for the "NuGet Restore" task and look for the "Detected NuGet" line. We:

  • added in the "NuGet Tool Installer" task before the "NuGet Restore" task
  • Under the "Version of NuGet.exe to install", list the version you want to use, or the minimum version (e.g. >=6.1.0)
  • (this step is possibly overkill) Under the "NuGet Restore" Task, check "Disable local cache"
Matt Lane
  • 531
  • 3
  • 5
  • 2
    this was the solution for me when using azure devops pipelines – BozoJoe Jan 11 '23 at 22:31
  • This also worked for me (without having to use "Disable local cache"). – Mr.Zzyzzx Jun 30 '23 at 16:40
  • I use NuGetToolInstaller@1 without specifying the version but check latest is true. this step is run successfully but after I try to restore Nuget using DotNetCoreCLI@2 it is also successfully restored but I am getting the same error in the next dotnet publish command. Can anyone tell me what is the better way to restore the NuGet.either dotnet restore, NuGetCommand@2, or DotNetCoreCLI@2 – RMR Aug 07 '23 at 19:28
14

This happens because NuGet writes a file named project.assets.json in the obj\ folder & the .NET SDK uses it to get information about packages to pass into the compiler. In .NET 5, Nuget added a new field called TargetFrameworkAlias, and thus in MSBuild versions < 16.8 or NuGet versions < 5.8, it is possible that you can generate an assets file without the TargetFrameworkAlias as it will read the property and not find it.

You can resolve this issue by ensuring you are on MSBuild version 16.8+ & using NuGet version 5.8+.

In my case I have commented out the TargetAlias line and it published successfully.

Reference: https://developercommunity.visualstudio.com/t/error-netsdk1005-assets-file-projectassetsjson-doe/1248649

enter image description here

Mamdouh Emam
  • 195
  • 1
  • 9
13

For me, this happened after switching from .net6.0 to .net 7.0 in asp.net core / blazor project. The error occured when trying to publish the project to IIS. Solution was to switch the "target framework" in the publishing configuration (.pubxml) in the "Publish"-tab. enter image description here

Cleo
  • 620
  • 2
  • 10
  • 32
10

For me this fix worked:

If you don't have the dotnet cmd line tool, download and install the .NET 6 SDK.

Open a cmd prompt and run the command:

 dotnet restore <path to your solution>

(for instance: dotnet restore c:\app\myapp.sln)

AH.
  • 2,913
  • 1
  • 28
  • 26
6

For me, I was getting this when updating my projects from .NET Core 3.1 to .NET 6. I had my .NET 6 code in another Git branch and when I switched from the main 3.1 branch to the 6 branch and then tried to build the solution, I would get that message.

After some trial and error, the solution that worked for me was doing the Git checkout and the restore via command line.

  1. Close the solution

  2. From the Developer Powershell (or using regular Powershell or the Visual Studio 2022 Command Prompt), navigate to the local directory that has your repo, and then:

    git checkout [branch name]

    dotnet restore

  3. Then back in Visual Studio, reopen the solution and build, which would work.

howcheng
  • 2,211
  • 2
  • 17
  • 24
  • "dotnet restore" was the golden ticket. Thank you! – adamjhilton Oct 11 '22 at 17:42
  • I tried right-clicking on my Solution and doing "Restore NuGet Packages" but VS2022 said there was nothing to restore. So I: 1) Exited VS2022. 2) Navigated to C:\Users\{my_user_name}\.nuget\packages 3) Deleted all sub-folders. 4) Re-started VS2022. 5) Re-opened Solution. VS2022 auto-restores the deleted NuGet Packages and apparently repairs problems in the process so strangely everything worked after that! – Kevin North Aug 08 '23 at 20:13
5

I was facing the below mentioned issue while deploying to the simulator

/../obj/project.assets.json' doesn't have a target for 'net7.0-ios/ios-arm64'. Ensure that restore has run and that you have included 'net7.0-ios' in the TargetFrameworks for your project. You may also need to include 'ios-arm64' in your project's RuntimeIdentifiers. (NETSDK1047)..

Adding the <RuntimeIdentifier>ios-arm64</RuntimeIdentifier> leading some other error. This issue mostly related to package restore.

Below steps worked for me:

  • Delete bin and obj for the host project.

  • Right click solution and restore package.(While restoring if target changes to generic device then set it to simulator and restore)

  • If again target changes to 'generic device' then Quit VS and repeat above steps

  • Deploy now.

Suchith
  • 1,276
  • 17
  • 39
1

I had this bug in a solution with several SDK plus non-SDK C# projects.

What fixed my case:

  1. Close the solution.

  2. Separately open the first project of the solution that Visual Studio failed to build.

  3. Build the project. --> "Error not found and build is OK"

  4. Reopen solution. --> "Error disappeared"

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
AhmadYo
  • 334
  • 3
  • 6
1

nuget restore resolved the same issue and/or dotnet restore

garik
  • 5,669
  • 5
  • 30
  • 42
1

For me, the issue was directly due to an invalid Package Version value. E.g., instead of 1.2.2-beta, I inadvertently had it as 1.2.2.beta instead. Once I corrected it to a properly formatted value, the project/package compiled successfully.

Joshua Barker
  • 113
  • 1
  • 8
0

I got the same error when publishing Web API to the cloud. Use Tools ->Command line -> Developer command prompt in Visual Studio 2022, enter AZ login, and after login, restart the visual studio, it is working for me again.

Tony Dong
  • 3,213
  • 1
  • 29
  • 32
0

Had the same problem in Azure Devops, using a Windows 2019 build server with VisualStudio 2022

Error: ##[error]C:\Program Files\dotnet\sdk\6.0.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): Error NETSDK1005: Assets file 'C:\agent\vso_work\4\s<..>.API\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project.

Resolved by adding a "NuGet Tool Installer" using version >=6.0.0 Before the NuGet restore task

enter image description here

Shlomi
  • 33
  • 7
0

we got this problem when added cache on gitlab, and started to use dotnet restore, it happend because we missed the "runtime" parameter to the restore command,

- dotnet restore --packages .nuget --runtime win-x64
- dotnet publish --no-restore --runtime win-x64
shaig
  • 35
  • 4
0

In my case the problem was I had updated the Nuget package version in one assembly but not in another, so check you have the same nuget package versions across your solution.

Chris
  • 1,101
  • 8
  • 13
0

For me, it works to set the target framework to another framework like .NET Core 3.1, build the application, set the framework to the original framework and rebuild.

Florian
  • 1,019
  • 6
  • 22
0

In my case I had inadvertently added a couple of projects to the solution that were in another folder. I received no errors until I changed NET version from NET6.0 to NET6.0-windows on one of the projects. The solution then didn't build, with 100s of errors, but each individual project built OK. I noticed that "project.assets.json' doesn't have a target" error among all the errors, pointing to the outside folder. Bringing those projects into the solution folder fixed the error.

davidslade
  • 11
  • 4
0

I got the same error sometime back. This worked for me: Logout from visual studio and login to visual studio account

0

I had an error:

Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project. You may also need to include 'win10-x64' in your project's RuntimeIdentifiers.

I removed bin and obj folders for this project and rebuilt the project. dotnet clean, dotnet restore didn't work for me.

Jaryn
  • 446
  • 4
  • 16
0

I had the same problem ("...\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore...") with clean batch compilation of my sln: msbuild 17.4, nuget 4.7.

I replaced string

nuget.exe restore my.sln

with string

msbuild.exe my.sln /t:Restore

that was before

msbuild.exe my.sln /t:Build

and everything worked.

Bunker
  • 49
  • 5
0

In my case, the issue occurred after manually migrating a WPF application (still based on .NET framework 4.6) to the new "SDK style" project format. I used a combination of the proposed solutions in this thread for performing the migration.

I had also enabled the packages.lock.json file by using RestorePackagesWithLockFile and RestoreLockedMode.

Everything was working fine, but when the project was being built in the GitLab pipeline, the assets file error occurred, stating the following:

error NETSDK1047: Assets file '[BlaBla]\obj\project.assets.json' doesn't have a target for 'net46/win7-x86'. Ensure that restore has run and that you have included 'net46' in the TargetFrameworks for your project. You may also need to include 'win7-x86' in your project's RuntimeIdentifiers.

I wondered about "win7" and "x86" since I had never specified this. Therefore, I ignored the proposed solutions from the error message and had a look at the project properties instead. It turned out that the "Platform target" was set to "x86", which seems to be the default. I changed this to "Any CPU", performed a rebuild, committed the changes and the pipeline succeeded.

After changing the target platform to "Any CPU" and performing the rebuild, the packages.lock.json was also modified. It had contained a section for ".NETFramework,Version=v4.6/win7-x86" previously, but that had now been removed. Most probably, this was causing the build to fail.

gehho
  • 9,049
  • 3
  • 45
  • 59
0

In my case I had

<TargetFramework>net6</TargetFramework>

instead of

<TargetFramework>net6.0</TargetFramework>

in my .csproj. Surprisingly everything else has worked, short of publishing...

mcpiroman
  • 175
  • 2
  • 11
-1

I got the same error this morning. This worked for me: right click on solution explorer in visual studio -> 'Restore NuGet Packages'. Hope this helps.

ittufis
  • 1
  • 1
-1

One another reason just came across to this problem while upgrading an existing Xamarin.Forms project to Maui. When you add a new Maui project or convert using the upgrade assistant, you may get this problem if Xamarin.forms project was using package.config reference instead of Project reference. Even if you add a new project to the existing solution, it doesnt compile but if you open in a new solution it works. I have compared what what could be the difference and there is no difference. Solution would be to upgrade from package.config reference to Project references first for nuget and then upgrade to Maui. This could be also similar problem from older Blazor projects to net6 or 7 based projects upgrade.

Emil
  • 6,411
  • 7
  • 62
  • 112
-1

This can happen during publishing if your target framework becomes out of sync with the target runtime you have set. Double-check this as follows:

  1. On the Publish page, click Show all settings.
  2. If there is a compatibility problem, you may see a red exclamation mark (!) next to the Target framework value. Select a different framework.

You would see the error if, for example, you were targeting the Windows runtime (e.g., win-x64), but not targeting Windows for the framework.

Tawab Wakil
  • 1,737
  • 18
  • 33
-1

I tried to build my .Net Core MVC locally via VS. Everything is fine. But I got this error when I pushed it to Azure Dev Ops. Finally, I addressed this issue by aligning the NuGet versions.

My local NuGet Version is 6.6.0. The ADO NuGet version, however, is 5.5.1.

I changed ADO pipeline NuGet version to 6.6.1 (somehow ADO pipeline task doesn't support 6.6.0 so I chose the closest one.). The error disappeared then.