UWP: MSBuild, NuGet 3 and project.json

Universal Windows app development (UWP) comes with NuGet 3.1, and this version brings a new package configuration file called project.json. No more packages.config, no more angle brackets, apparently curly brackets are the future. For a full introduction of NuGet 3.1 and UWP development, read the article Announcing NuGet 3.1 with Support for Universal Windows Platform.

When compiling your UWP project in Visual Studio 2015, a NuGet Packager Manager dialog appears briefly, telling you it's restoring the NuGet packages as defined in project.json. NuGet restore generates a project.lock.json file and stores it next to project.json. The project.lock.json file includes all the dependencies of the packages specified in project.json. More details on project.lock.json can be found in the NuGet documentation.

The problem with MSBuild

When building a Universal Windows project in Visual Studio 2015, all is good. If you however try to build the project with MSBuild (like you would do on a build server), the build will hopelessly fail with errors such as "CS0518: Predefined type is not defined or imported". When this error occurs, this means that the NuGet package restore did not run, and no project.lock.json file was generated. Since project.lock.json is typically not stored in source control, a build on a build server without a proper NuGet restore will fail.

The solution

You need to perform a NuGet restore build step on the project, before actually building it. I'm not sure yet why this does work automatically in Visual Studio 2015 and not with MSBuild, that question still needs to be answered.

You can use the commandline executable for NuGet (nuget.exe) and include it in your build process. A link to the commandline version of NuGet.exe is available in this article. And here's the direct link to the current version 3.1 beta: http://dist.nuget.org/win-x86-commandline/v3.1.0-beta/nuget.exe.

Execute 'nuget.exe restore' in the folder where your solution is located, and you should see output similar to the following;

Restoring packages for test.csproj...
Restoring packages for UAP,Version=v10.0...
Restoring packages for UAP,Version=v10.0 (win10-arm)...
Restoring packages for UAP,Version=v10.0 (win10-arm-aot)...
Restoring packages for UAP,Version=v10.0 (win10-x86)...
Restoring packages for UAP,Version=v10.0 (win10-x86-aot)...
Restoring packages for UAP,Version=v10.0 (win10-x64)...
Restoring packages for UAP,Version=v10.0 (win10-x64-aot)...
All packages are compatible with UAP,Version=v10.0.
All packages are compatible with UAP,Version=v10.0 (win10-arm).
All packages are compatible with UAP,Version=v10.0 (win10-arm-aot).
All packages are compatible with UAP,Version=v10.0 (win10-x86).
All packages are compatible with UAP,Version=v10.0 (win10-x86-aot).
All packages are compatible with UAP,Version=v10.0 (win10-x64).
All packages are compatible with UAP,Version=v10.0 (win10-x64-aot).

You can verify that the restore has succeeded, by checking whether a project.lock.json file exists next to project.json

Tips

The NuGet restore doesn't work as expected? Check the following;

First, double check you are using the commandline executable for NuGet 3.1 or up, NuGet 2.x does not understand project.json and will happily ignore your restore request.

And second; make sure there is no old packages.config in your project directory. NuGet will look at packages.config first, and if not found it will process project.json. If an old packages.config is there, remove it.



Latest News

RSS feed

Subscribe to the RSS feed and receive the latest news from First Floor Software in your favorite RSS reader.

Subscribe