gdogra
gdogra

Reputation: 130

how to setup with target framework correctly

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:

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:

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

Upvotes: 3

Views: 5059

Answers (2)

Metro Smurf
Metro Smurf

Reputation: 38335

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.

Upvotes: 1

Max
Max

Reputation: 862

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.

Upvotes: 0

Related Questions