129

Using Visual Studio 2017, AspNetCore 1.1.2

All of a sudden I am getting following error when I am trying to publish (Release build) any project in the solution:

Assets file 'C:\example\obj\project.assets.json' doesn't have a target for '.NETFramework,Version=v4.5.2/win7-x86'. Ensure that restore has run and that you have included 'net452' in the TargetFrameworks for your project. You may also need to include 'win7-x86' in your project's RuntimeIdentifiers.

Have checked in the project.assets.json files, I have:

"targets": {
  ".NETFramework,Version=v4.5.2": {

and

"runtimes": {
  "win7-x86": {
    "#import": []
  }

In the *.csproj files I have:

  <PropertyGroup>
      <TargetFramework>net452</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
      <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup> 

Have made no changes to config in the projects. Only thing is that I have updated VS2017 to latest version today, 15.6.3. Could this cause issue?

Paolo B
  • 3,026
  • 5
  • 21
  • 43
  • 6
    Does deletion of "C:\example\ob" folder and rebuild help? – CodeFuller Mar 20 '18 at 18:59
  • 2
    No sorry same issue. – Paolo B Mar 20 '18 at 19:12
  • 1
    Please clarify: .NET Framework and .NET Core are two different things. It doesn't appear that you're using .NET Core at all here. – McGuireV10 Mar 20 '18 at 19:18
  • 1
    take a look: https://github.com/dotnet/sdk/issues/1321 – aleha_84 Mar 20 '18 at 19:21
  • 2
    Just in case someone finds this after running into the same problem as me: one of the issues you can run into is a mismatch between .NET Standard and .NET Core when you're doing multi-targeting. You can't have a library target .NET Standard and then use `dotnet build -f netcoreapp2.1` despite the code being technically compatible. You either need to use `-f netstandard2.0` and add that to your assets file as a target, or switch to targeting .NET Core 2.1. – Polynomial Nov 28 '18 at 17:14
  • https://developercommunity.visualstudio.com/t/error-NETSDK1005:-Assets-file-projecta/1248649?preview=true – kayleeFrye_onDeck Feb 03 '23 at 01:07

26 Answers26

158

According to the Microsoft blog (which, bizarrely, my account doesn't have permissions to post in), this isn't a bug, and is entirely caused by ReSharper. If you disable this, the problem goes away.

Errr, one problem: I'm getting this error, and I don't have ReSharper.

After a lot of hunting around, I found the reason I was getting the error on my .NET Core project which had been upgraded from 1.0 to 2.1.

When running my project in Debug or Release mode, everything worked fine, but when I tried to publish to Azure, I got that error:

Assets file '(mikesproject)\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.0'. Ensure that restore has run and that you have included 'netcoreapp2.0' in the TargetFrameworks for your project.

Although I had updated the version of .NET Core to 2.1 in Project\Properties and upgraded the various nuget packages, there was one place which hadn't picked up this change... the Publish Profile file.

I needed to go into the Properties\PublishProfiles folder in my solution, open up the .pubxml file relating to the way I was publishing to Azure, and change this setting from netcoreapp2.0 to netcoreapp2.1:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    . . . 
    <TargetFramework>netcoreapp2.0</TargetFramework>
    . . . 
  </PropertyGroup>
</Project>

Ridiculous, hey?

I do wish Microsoft error messages gave some clue as to the source of problems like this.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
  • 10
    Removed and created a new Publish Profile and then it worked! – Westerlund.io Jun 17 '19 at 10:37
  • I'm running into the same thing now. I have a simple class library project targeting both net47 and net6.0. Resharper disabled and no .pubxml file. I'm at a loss on why this is happening. (BTW, running "dotnet build" in "development powershell" works and creates a good project.assets.json file. =/ – MarcWeintraub Sep 19 '22 at 14:07
128

Restarting Visual Studio solved the error for me.

Ferro
  • 1,863
  • 2
  • 14
  • 20
  • 4
    Updated an open solution from github which included new framework targets, this answer fixed the problem when nothing else did. – Adrian Seeley Mar 26 '19 at 18:46
  • 3
    This solved my issue in VS 16.8.2 with netcoreapp3.1;netstandard2.0 – Josh Graham Mar 29 '21 at 22:42
  • I upgraded a handful of .NET standard 2.0 libraries in a solution to 2.1, everything was normal at first until I did some stashing and changed branches; then I was met with this when trying to run tests. Restarting VS cleared the issue... – Urk Aug 30 '22 at 17:32
  • 124 upvotes, including mine. Really. Just restarting it. – aleee Mar 29 '23 at 20:26
  • Still applicable in VS2022 and .NET 7.0. – Roman Starkov Apr 25 '23 at 19:29
54

Right click on the project file, and click unload. Then right click on the project and reload.

Paul
  • 2,330
  • 2
  • 19
  • 18
40

Had this error in similar situation. This has helped me: Link

This is my property group in *.csproj file of my .net core 3.0 project:

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.0</TargetFrameworks>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>  <----- SOLVES IT. Mandatory Line
</PropertyGroup>
CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
28

Migrating from nuget 5.4 to nuget 5.8 solve the problem on my devops build server

k4st0r42
  • 1,174
  • 1
  • 15
  • 29
13

Delete the publish profile you created and create a new one. The wizard will put in the correct targetframe work for you and publish again. It should solve the problem.

Eyad
  • 201
  • 1
  • 3
  • 5
5

If your build script starts with a dotnet restore and ends with a dotnet publish --no-restore, you must make sure that they both include the same --runtime parameter.

9Rune5
  • 373
  • 1
  • 4
  • 16
5

For me it was the happening because I had migrated my project from .net5.0 to .net6.0 and the problem was caused when I was publishing the project while debugging worked fine.

Checking the publishing profile showed that it had a configuration for .net5.0 in it:

.net5.0 publishing profile

Changing the existing .net core version with the desired one resolved the issue:


Change .net5.0 to .net6.0


Or you can directly change it by going into the publishing profile `.pubxml` file under `Properties > PublishingProfiles` directory.

.pubxml

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
4

Restarting Visual Studio or unloading/reloading the project didn't work for me, but deleting the "obj" folder and then rebuilding seems to have fixed the issue.

Dion V.
  • 2,090
  • 2
  • 14
  • 24
4

I got this error when upgrading a web project from netcoreapp3.1 to net5.0.

One of the answers here pointed me in the right direction:

The publish profile still had netcoreapp3.1 as target framework. Edited the publish profile and changed target framework to net5.0 and it worked.

(Visual Studio 16.8)

emilsteen
  • 496
  • 4
  • 14
3

For me the problem ended up being that one of my NuGet feeds was down, so a package was not getting updated properly. It wasn't until I ran a NuGet package restore directly on the solution that I saw any error messages related to my NuGet feed being down.

TJ Rockefeller
  • 3,178
  • 17
  • 43
3

You should try all the other solutions here first. Failing that you can try what eventually unblocked me when none of these did. I ran into this problem when porting a Jenkins build to an Azure DevOps pipeline on a pool of agents. It took about 60 builds before I tried every other possibility. I found out that needed to do two things:

  1. Ensure the tooling was consistent for this specific project
  2. Use a nuget restore friendly with the version of MSBuild used after finding out that mattered yet I couldn't use the proposed workaround for just updated nuget tooling.

The versions I needed to use are likely different than yours.

1:

call choco install windows-adk-all --version=10.0.15063.0 --force

call choco install windows-sdk-10.1 --version=10.1.15063.468 --force

2:

call MSBuild -t:restore Solution.sln

call MSBuild Solution.sln /t:rebuild;pack /p:Configuration=Release /p:Platform="Any CPU"
 
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
  • 1
    Sadly, the hyperlink here is the explanation that all of the other "upgrade XYZ" answers lacked. – JdeBP Nov 06 '21 at 03:45
2

To me, the error was caused because of an existing global.json file in the solution level folder, pointing to a different .NET version. Removing that file (or changing its SDK version) resolved the problem

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • We added a file 'global.json' and specified version as { "sdk": { "version": "2.1.509", "rollForward": "latestFeature" } } – Vsh Feb 16 '21 at 05:24
2

Upgrading NuGet version from 5.5.1 to 5.8.0 fixed the issue.

ak_01
  • 61
  • 1
  • 3
2

I had similar issue, when I installed a new sdk version.

Exception was:

Severity Code Description Project File Line Suppression State Error NETSDK1005
Assets file '.. \RazorPages\obj\project.assets.json' doesn't have a target for 
'netcoreapp3.1'. Ensure that restore has run and that you have included 'netcoreapp3.1' 
in the TargetFrameworks for your project. RazorPages 
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk
\targets\Microsoft.PackageDependencyResolution.targets    241

Solution was to select again the target version of the project.

  1. right click on solution
  2. Properties\Application Tab
  3. Change Target framework version to something different and change it back.
phoenixstudio
  • 1,776
  • 1
  • 14
  • 19
viktormark92
  • 111
  • 4
  • 1
    The error happened to me because I mistakenly compiled the solution in an old VS version, which restored odd dependencies. This answer solved the problem to me. – Cesar Mar 20 '21 at 15:08
2

Running dotnet restore --packages .nuget in the project directory fixed the issues for me.

steven01804
  • 335
  • 2
  • 13
1

A colleague ran into this after upgrading an application from dotnet core 1.1 to dotnet core 2.1. He properly updated all the targetFramework references within the various csproj files, and had no issues on his local development machine. However, we run Azure DevOps Server and build agents on-premises, so the build agent was reporting this error after a pull request build was executed.

The dotnet clean task was throwing an error because of the new targeted framework. dotnet clean uses the same targets as build, publish, etc, so after a change in target frameworks the dotnet restore must happen before the dotnet clean to update the dependent files. In hindsight this makes sense because you want to restore dependencies to the proper target framework before you do any building or deploying.

This may only affect projects with upgraded target frameworks but I have not tested it.

gregsonian
  • 1,126
  • 13
  • 15
1

Receiving similar error for 'netcoreapp3.1' when building using command line. It turned out to be an MsBuild switch that caused the issue. Specifically speaking:

/p:TargetFramework="netcoreapp3.1"

Removed the switch and the error was fixed.

Hamed
  • 279
  • 1
  • 3
  • 13
1

I upgraded from netstandard to net6.0, when publishing had to change TargetFramework to net6.0 enter image description here

Sergio Solorzano
  • 476
  • 9
  • 29
0

In my case updating visual studio 2019 to the latest version, fixed the issue.

TheLoneWolf91193
  • 415
  • 7
  • 19
0

In my case, if you have TargetFrameworks and TargetFramework together in the csrpoj file, remove TargetFramework will solve the problem.

edit it from:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>netstandard2.0;net461;</TargetFrameworks>
        <TargetFramework>net461</TargetFramework><!--remove this line-->
    </PropertyGroup>
</Project>

to

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>netstandard2.0;net461;</TargetFrameworks>
    </PropertyGroup>
</Project>
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

From my experience if you have dependencies in your solution built using "ProjectSection(ProjectDependencies) = postProject" then in this case dotnet build goes nuts.

0

I ran into the NETSDK1047 when playing around with Docker in a brand new dotnet project created using dotnet new worker and the docker file from dotnet-docker samples.

❯ docker build -t dockertest .

output elided...

/usr/share/dotnet/sdk/6.0.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1047: Assets file '/source/obj/project.assets.json' doesn't have a target for 'net6.0/linux-musl-x64'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project. You may also need to include 'linux-musl-x64' in your project's RuntimeIdentifiers. [/source/dockertest.csproj]
The command '/bin/sh -c dotnet publish -c release -o /app -r linux-musl-x64 --self-contained true --no-restore /p:PublishTrimmed=true /p:PublishReadyToRun=true /p:PublishSingleFile=true' returned a non-zero code: 1
dockertest on  main [✘] via .NET v6.0.202  net6.0
❯

The issue was because I forgot to add a .dockerignore file ignoring the bin and obj directories.

I only realized why because I tried different Dockerfiles from the dotnet-docker repo and got a different error which has this same resolution. I'll try to make a PR to the docs of NETSDK1047 to add this resolution. edit: link to PR https://github.com/dotnet/docs/pull/29530

willbush
  • 190
  • 4
  • 5
0

Had the same problem, for me it was that I had a space before my TargetFramework

<TargetFramework> net6.0</TargetFramework>
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
antca230
  • 56
  • 4
0

On my end I had this issue with net6.0 at build time, even before trying to publish. Even if everything was pointing to csproj and it had all the right TargetFramework, the issue was that our repo had a Nuget.Config at it's root and it included a configuration for a local disk Nuget Repo of another programmer. I disabled the Nuget.Config file and I was able to build the project. It was probably unable to restore the Nuget Packages but the error message was misleading.

-1

clean cache and restart VS then it worked for me.

Saige Zhang
  • 737
  • 1
  • 7
  • 18