LLuz
LLuz

Reputation: 2299

Assets file project.assets.json doesn't have a target for 'net6.0' - VS2022

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

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...

P.S. - already checked question 70711153

Upvotes: 128

Views: 139512

Answers (30)

Mamdouh Emam
Mamdouh Emam

Reputation: 345

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

A screenshot of the "project.assets.json" file, showing where the user has commented out the "targetAlias" field.

Upvotes: 28

Tawab Wakil
Tawab Wakil

Reputation: 2323

This can happen during publishing if your target framework gets 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 and see if the error clears.

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.

Upvotes: 2

Vincent
Vincent

Reputation: 762

I recently encountered the similar issue upgrade to .NET 8. I fixed this issue by just close my Visual Studio and reopen then build again. I know it sounds silly, but it works for me at the end. I hope if never tried this 101 troubleshooting approach, give it a go to see if works.

Upvotes: 17

CtrlCCtrlV
CtrlCCtrlV

Reputation: 101

I got this error after upgrading a .net 6 project to .net 8. It was because i had not changed the target framework in the publish profile to net8.0. So publish works fine after i changed the value.

Upvotes: 10

Moniker
Moniker

Reputation: 185

Make sure you have a target framework for the system you are running on.
If you're running on mac, add a target framework for maccatalyst
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst;net8.0</TargetFrameworks>

In my case I had the issue on a .Net Maui app with net8.0 unit tests. I was building only iOS and Android. However, the tests were running on a mac, so I had to have a target framework for maccatalyst. Otherwise. I received

doesn't have a target for 'net8.0-ios/ios-arm64'

Adding the maccatalyst target fixed the issue.

Upvotes: 4

th1rdey3
th1rdey3

Reputation: 4388

I got this error when I ran the dotnet publish command with --no-restore option like below -

dotnet publish -r win-x64 -c Release --self-contained --no-restore

Even though I ran dotnet clean, dotnet restore, and dotnet build command beforehand the error persisted. Dropping the --no-restore option from the publish command resolved the issue.

Upvotes: 1

CodeWhore
CodeWhore

Reputation: 981

I got the error when upgrading from net5.0 to net6.0.

For me I had to change the

<TargetFramework>net6.0<TargtetFramework> to 
<TargetFrameworks>net6.0<TargtetFrameworks> 

in my .csproj file. I had it in 3 places (2 in conditional PropertyGroups). Once I changed it my publish started working.

 <PropertyGroup Condition=" '$(Platform)' == 'x64' ">
   <!-- For publishing a single file exe -->
   <OutputType>Exe</OutputType>
   <TargetFrameworks>net6.0</TargetFrameworks>  <=== this worked
   <PublishSingleFile>true</PublishSingleFile>
   <SelfContained>true</SelfContained>
   <RuntimeIdentifier>win-x86</RuntimeIdentifier>
   <PublishReadyToRun>true</PublishReadyToRun>
 </PropertyGroup>

Upvotes: 1

gmancoda
gmancoda

Reputation: 85

My project was set to use .NET 6.0, and I had to get my aws-lambda-tools-defaults.json file to say "net6.0" for "framework" value. That's what fixed it for me.

The command failing was "dotnet lambda deploy-function"

Upvotes: 0

mcpiroman
mcpiroman

Reputation: 355

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...

Upvotes: 1

Peter Hou
Peter Hou

Reputation: 31

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.

Upvotes: 1

Emil
Emil

Reputation: 6911

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.

Upvotes: -1

Suchith
Suchith

Reputation: 1527

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.

Upvotes: 7

Joshua Barker
Joshua Barker

Reputation: 183

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.

Upvotes: 1

gehho
gehho

Reputation: 9248

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.

Upvotes: 0

Bunker
Bunker

Reputation: 59

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.

Upvotes: 1

Jaryn
Jaryn

Reputation: 486

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.

Upvotes: 0

Anil Maharjan
Anil Maharjan

Reputation: 1

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

Upvotes: 0

davidslade
davidslade

Reputation: 11

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.

Upvotes: 0

Cleo
Cleo

Reputation: 1171

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

Upvotes: 61

Florian
Florian

Reputation: 1089

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.

Upvotes: 1

AhmadYo
AhmadYo

Reputation: 476

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"

Upvotes: 1

garik
garik

Reputation: 5756

nuget restore resolved the same issue and/or dotnet restore

Upvotes: 1

Chris
Chris

Reputation: 1789

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.

Upvotes: 0

AH.
AH.

Reputation: 3101

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)

Upvotes: 18

shaig
shaig

Reputation: 95

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

Upvotes: 6

holly_cheng
holly_cheng

Reputation: 2481

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.

Upvotes: 7

LLuz
LLuz

Reputation: 2299

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.

Upvotes: 91

Shlomi
Shlomi

Reputation: 51

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

Upvotes: 2

Matt Lane
Matt Lane

Reputation: 631

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"

Upvotes: 53

Tony Dong
Tony Dong

Reputation: 3313

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.

Upvotes: 0

Related Questions