3

Can someone please help me with the following error, it comes up when I try to build. Below is the message from the output window.

Failed to restore 'c:\directoryName\projectName\projectName.csproj' NuGet package restore failed. Please see the Error List window for detailed warnings and errors.

C:\Program Files\dotnet\sdk\6.0.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1005: Assets file 'C:\DirectoryName\ProjectName\obj\project.assets.json' doesn't have a target for 'netstandard2.1'. Ensure that restore has run and that you have included 'netstandard2.1' in the TargetFrameworks for your project.

I've tried the below but with no success:

  • Right click on Solution and selected Restore Nuget packages
  • Deleted the obj folder from then build it again
  • Clean the solution and build again
  • Uninstalled and reinstalled Visual Studio Professional 2022
  • Cloned the repo
  • unload and reload the project
  • added this line in .csproj file <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  • Signed out of visual studio 2022, closed it and signed back in
  • Checked I was running the latest nuget version
  • did a dotnet restore from console
  • did msbuild -restore in console brings this error "MSB4181: The "RestoreTask" task returned false but did not log an error."
  • Signed out of visual studio, closed it, and signed back in
  • Signed with someone else's credentials
  • Tools>Options>NuGet Package Manager>General>Clear All Nuget Cache

The .csproj file has everything in place:

    <Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="company.RepositoryFactory.Abstractions" Version="109.0.0.16" />
    <PackageReference Include="company.UseCase.Abstractions" Version="96.0.0.5" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\company.ExternalprojectActivation.Messaging.Commands\company.ExternalprojectActivation.Messaging.Commands.csproj" />
  </ItemGroup>

</Project>

Environment:

  • Visual Studio 2022 Professional 17.2.6
  • Nuget Package Manager 6.2.1
  • Window Server 2016

Other developers in the team are able to build the above project without any errors.

Edit: I had updated the VS 2022 to 17.3.0. And now I am getting below

C:\Workspace\ProjectName\CompanyName.Project.UseCases.Abstractions\bin\Debug\netstandard2.1\CompanyName.Project.UseCases.Abstractions.dll' could not be found

gdogra
  • 130
  • 2
  • 12
  • Any of these answers help? https://stackoverflow.com/questions/72280776/assets-file-project-assets-json-doesnt-have-a-target-for-net6-0-vs2022 – Jeremy Lakeman Aug 08 '22 at 01:12
  • @Jeremy I am going to use someone else credentials today, and I think azure devops answers doesn’t apply as I am building on local. – gdogra Aug 08 '22 at 06:21
  • Are you referencing any NuGet packages which need `netstandard2.1`? Maybe you can show the complete `csproj` file – mu88 Aug 08 '22 at 07:23
  • @mu88 lot of company owned package refrences, and some legacy code. I don't think I am allowed to paste the full csproj file – gdogra Aug 08 '22 at 13:14
  • Make sure that all of your NuGet packages are compatible to your TFM .NET Core 3.1 - .NET Standard 2.1 is not compatible – mu88 Aug 08 '22 at 13:20
  • The error message mentions that TargetFrameworks needs to include "netstandard2.1". In the code you provided TargetFramewrok is "netcoreapp3.1". – Jingmiao Xu-MSFT Aug 09 '22 at 02:14
  • @jingmiao copied from the wrong file, updated the correct TargetFramework as it exists – gdogra Aug 09 '22 at 09:52
  • @gdogra Did you try to clear all nuget caches under Tools>Options>NuGet Package Manager and then rebuild the project? – Jingmiao Xu-MSFT Aug 16 '22 at 04:16
  • Can you share the whole .csproj file? – sa-es-ir Aug 18 '22 at 05:20
  • @SaeedEsmaeelinejad added whole csproj file. it is from one of the 7 projects in the solution – gdogra Aug 18 '22 at 13:37
  • You need to share this file because the error is in there: ``company.ExternalprojectActivation.UseCases.Abstractions\company.ExternalprojectActivation.UseCases.Abstractions.csproj`` – sa-es-ir Aug 18 '22 at 13:49
  • @SaeedEsmaeelinejad pasted **company.ExternalprojectActivation.UseCases.Abstractions\company.ExternalprojectActivation.UseCases.Abstractions.csproj** – gdogra Aug 19 '22 at 06:22
  • You can try to copy the packages and dll from other developers' pc in the team and check if the problem persists. The package "Microsoft.Extensions.DependencyInjection.Abstractions 5.0.0" has been deprecated as it is legacy and no longer maintained. Maybe update it can help you. – Jingmiao Xu-MSFT Aug 19 '22 at 08:02
  • @gdogra try to delete the project, then run VS as administrator, then clone it again. see if this would solve it. – iSR5 Aug 22 '22 at 11:58
  • Answer: I did not had the correct versions of the nuget packages, I did copy ONE of the missing packages but VS was still telling me that it had failed to restore the copied package, but when I copied correct version of each of the missing then I was able to restore everything. – gdogra Jan 28 '23 at 22:35

2 Answers2

1

With the current sample code, there is no way to debug the issue due to the csproj:

  • References 2 internal nuget packages
    • company.RepositoryFactory.Abstractions
    • company.UseCase.Abstractions
  • References 1 internal project:
    • company.ExternalprojectActivation.Messaging.Commands.csproj

Error 1

However, the error message is fairly clear:

'C:\DirectoryName\ProjectName\obj\project.assets.json' doesn't have a target for 'netstandard2.1'. Ensure that restore has run and that you have included 'netstandard2.1' in the TargetFrameworks for your project.

The above error means the target framework for C:\DirectoryName\ProjectName is not compatible with netstandard2.1

To resolve the error, verify the target framework for C:\DirectoryName\ProjectName is netstandard2.1

Error 2

C:\Workspace\ProjectName\CompanyName.Project.UseCases.Abstractions\bin\Debug\netstandard2.1\CompanyName.Project.UseCases.Abstractions.dll' could not be found

The new error is basically saying the referenced project wasn't built and wasn't copied into your current project.

To resolve this error, you'll first need to ensure the CompanyName.Project.UseCases.Abstractions project can be built by itself (right click on project and build).

In these types of scenarios, I will typically follow a methodical build test:

  • Clean the solution.
  • Build each project individually.
  • Start with the core/common project that does not reference any other projects.
  • Then move on to the next one with the least amount of project references.
  • Do this until you find the first failing project and identify exactly what is missing.

Finally

The error messages suggest to me that there are framework target mismatches somewhere in your solution. Go through every single project and make sure they are all targeting the same framework and/or are compatible with each other.

If the same solution can be build w/o error by other members of your team, then you'll want to do something like a directory diff between their solution and yours. Tools like Beyond Compare make the diff super easy.

And finally, I recently encountered a very similar build error. No matter what I tried, it simply failed (including reparing VS, etc...). I then recalled I had a BSOD during a Windows update. I threw my hands up, backed up everything, wiped my drive clean and laid down a fresh Windows install; and then installed my toolsets. Everything worked as expected after that. I'm not suggesting you do the same, but it is something to consider as a very last resort.

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
  • I thank you for the effort in trying to resolve my problem. I'll try Beyond compare in few days and update. Unfortunately, I am not able to re-install my windows because it is an amazon workspace machine set up by my company. – gdogra Aug 22 '22 at 07:45
  • Like I mentioned, the re-imaging is a last resort. Since this is just an AWS workspace, it should be fairly easy for the team to provision a new instance for you (again, last resort). Work through those steps one by one and find where the first failure is and go from there. – Metro Smurf Aug 22 '22 at 12:50
0

Have you checked this? https://learn.microsoft.com/en-us/dotnet/core/tools/sdk-errors/netsdk1005

If you still have the issue after and you are using some package from your company just check you Azure credentials are refreshed, because even if your are running the project locally you still need to fetch data when you do a restore.

Max
  • 794
  • 3
  • 7