Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild can't find package for .NET Standard project in solution with other .NET Framework projects

I have a Visual Studio Solution with:

  • 2 .NET Standard 2.0 projects (.csproj)
    • proj A (class library): proj refs -> proj B, no Nuget packages
    • proj B (class library): no proj refs, no Nuget packages
  • 2 .NET Framework 4.7.2 projects (.csproj)
    • proj C (class library): proj refs -> proj B, several Nuget packages
    • proj D (test project): proj refs -> proj C, proj B & proj A, several Nuget packages

This solution built fine on my local machine running VS Community 2017 15.7.3 This solution also builds fine on my build machine which is using MSBuild from the same VS of the same version.

Problem

The problem occurs when I add a Nuget package (in this case Newtonsoft) to proj A. The solution builds fine on my local machine but doesn't work when using MSBuild. Also, if I load the solution on my build machine using the VS installed there, it builds fine.

The error getting raised is:

error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

I think I've narrowed down the issue to an issue where .NET Standard & .NET Framework projects don't play well together with Nuget when they're in the same .sln because if I completely remove proj C & proj D the build succeeds on my build machine. I can also confirm that the Newtonsoft package is in my C:/User/.nuget/packages folder, but is not in the ./packages folder of the solution directory (which I'm pretty sure if supposed to happen since it's a .NET Standard project?).

CI .yml File

nuget.exe restore $SOLUTION_FILE
MSBuild.exe '/p:Configuration=release' $SOLUTION_FILE

Notes

I've done a lot of reading trying to find issues similar to this, but the closest I get is issues that look a lot like this one. This issue is similar but is using the .NET Standard project itself as a Nuget package where as I'm trying to get Nuget packages into the .NET Standard project itself. Maybe I need to extract all of my .NET Standard projects into a Nuget package instead of using them in the same solution?

like image 311
thefoxrocks Avatar asked Jun 01 '18 17:06

thefoxrocks


1 Answers

As Leo Lieu pointed out, using msbuild /t:restore "YourSolutionFile.sln" was the trick, although I had to do it as well as running nuget.exe restore "YourSolutionFile.sln" which I find peculiar...

like image 108
thefoxrocks Avatar answered Nov 15 '22 06:11

thefoxrocks