CarlDaniel
CarlDaniel

Reputation: 433

Nuget package not found after restore VS 2019 16.5.0

I've been fighting with nuget all morning, trying to get a solution that builds in the UI AND from the command line. Here's the latest problem, which I haven't made any headway on:

Things I've tried

What am I missing? this shouldn't be so hard..

UPDATE: The solution consists of 14 projects: 9 C# class libraries, 2 c# applications, 1 reporting services project and 2 WiX installer projects. All C# projects target Net472, NOT Core. The key part of the solution structure appears to be:

During build, project B fails to compile due to the lack of a reference to Newtonsoft.Json. Project A and all of the other nuget packages are supplied to the compier as references. Again, all nuget packages are in fact restored - Project A finds Newtonsoft.Json, project B does not.

In the detailed msbuild log output, this is the only mention of Newtonsoft.Json in the build of project 10 (Project B above):

10>  Dependency "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed".
10>      Resolved file path is "...ProjectA\bin\Release\Newtonsoft.Json.dll".
10>      Reference found at search path location "...ProjectA\bin\Release".
10>          For SearchPath "...ProjectA\bin\Release".
10>          Considered "...ProjectA\bin\Release\Newtonsoft.Json.winmd", but it didn't exist.
10>      Required by "...ProjectA\bin\Release\ProjectA.dll".
10>      Required by "C:\...ProjectA2\bin\Release\ProjectA2.dll".
10>      Found related file "...ProjectA\bin\Release\Newtonsoft.Json.xml".
10>      The ImageRuntimeVersion for this reference is "v4.0.30319".

(Folder and project names have been obscured)

Upvotes: 3

Views: 4996

Answers (3)

CarlDaniel
CarlDaniel

Reputation: 433

A couple things going on here, finally got a solution that works. Why this built in the IDE is anyone's guess - it's adding some extra secret sauce to make things work (more than just the automatic nuget restore).

  • I tried changing all projects to use PackageRef instead of packages.config. That caused nuget restore to fail with an obscure msbuild error that I didn't try to diagnose.
  • I noticed that SOME of the nuget packages were referenced in the .csproj files with ordinary Reference elements, but some of them were not (specifically, Newtonsoft.Json in "Project B" - and some others that I hadn't noticed due to B failing).

To correct the situation:

  1. Remove ALL use of PackageRef elements - change back to packages.config in ALL projects
  2. Make sure the each of the nuget -provided DLLs is referenced in the .csproj files. You have to do this by editing the csproj file by hand - the IDE won't let you add the missing references.

I'm assuming that this is a temporary situation and that in the long run the solution will be to use PackageReference everywhere.

Upvotes: 3

Mr Qian
Mr Qian

Reputation: 23858

Nuget package not found after restore VS 2019 16.5.0

devenv /build command line does not have the job to restore nuget packages by default. However, there are such options in VS IDE so that it will restore packages first and then build. But these do not work in command line.See this similar issue.

enter image description here

But you still want to use devenv to build your project and since you use a framework project with packages.config, I suggest you could use nuget.exe.See this.You can try these:

1) download nuget.exe from this link and then configure its local address to PATH in the environment variable and make sure that you can call nuget from CMD.

2) open vs command prompt, cd the path of the solution and then type this first:

nuget restore

Then you can type your devenv command line and I am sure that this will execute without any errors.

devenv xxxx.sln /rebuild

Besides,you can add a custom target in any xxx.csproj file of your solution like this:

 <Target Name="restoresolution" BeforeTargets="BeforeBuild">
    <PropertyGroup>
      <nugetpath>C:\tools</nugetpath> /////the local path of the nuget.exe
    </PropertyGroup>
    <ItemGroup>
      <slns Include="$(MSBuildProjectDirectory)\..\**\*.sln" />
    </ItemGroup>
    <Exec command="$(nugetpath)\nuget restore %(slns.Identity)" />
  </Target>

Then you can run devenv xxxx.sln /rebuild directly.

Upvotes: 0

you already checked the files app.config and packages.config, and the dotnet framework version?

Upvotes: 0

Related Questions