Monday, March 10, 2014

Visual Studio NuGet package restore

Visual Studio has two approaches to use package restore: Automatic Package Restore and MSBuild-Integrated package restore.  The 'MSBuild-Integrated Package Restore' restores packages DURING the building process that might cause issues in some scenarios. The 'Automatic Package Restore' is the recommended approach by the NuGet team (http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore).

There are several steps to to make 'Automatic Package Restore' work:
1. In Visual Studio, Tools -> Extensions and Updates,  Upgrade NuGet if there is a newer version (Version 2.7 or later)

2. If you use TFS, in your solution's .nuget folder, remove the NuGet.exe and NuGet.targes files. Then edit NuGet.Config to not check in NuGet packages:

<configuration>
    <solution>
        <add key="disableSourceControlIntegration" value="true" />
    </solution>
</configuration>

If you checked in packages folder to TFS, delete the folder and check in the deletion of package folder deletion.

If you don't use TFS, delete the .nuget folder.

3. In each project file (.csproj or .vbproj) in your solution, remove the <Import Project> that references NuGet.targets file. The reference looks like this:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

Remove this line in every project file in your solution.

4. In Visual Studio menu, either through

Tools -> Options -> Package Manager -> General  or
Tools -> NuGet Package Manager -> Package Manager Settings

 please enable the following two options
1) 'Allow NuGet to download missing packages'
2) 'Automatically check for missing packages during build in Visual Studio'

5. Test your package restore configuration by the following steps
1) Save your solution and close Visual Studio
2) Delete your solution's packages folder
3) Start Visual Studio, open your solution and rebuild it.

No comments:

Post a Comment