Keywords: NuGet Package Restore | Visual Studio 2015 | Automatic Package Restore Migration | MSBuild Integration | .nuget Directory Cleanup
Abstract: This comprehensive guide explores the complete process of enabling NuGet package restore in Visual Studio 2015, focusing on migration from legacy MSBuild-integrated package restore to automatic package restore. Through detailed analysis of solution and project file modifications, with code examples illustrating removal of .nuget directory and NuGet.targets references, the article ensures proper functionality of package restore. It compares different restoration methods and provides practical configuration recommendations to help developers resolve package dependency management issues.
Overview of NuGet Package Restore Mechanism
NuGet package restore is a core functionality in modern .NET development, maintaining a clean development environment by automatically downloading project dependencies. In Visual Studio 2015, Microsoft introduced automatic package restore, replacing the older MSBuild-integrated approach. This transition aims to simplify configuration processes and improve build reliability.
Essential Steps for Migration to Automatic Package Restore
For solutions using legacy MSBuild-integrated package restore, systematic migration operations are required. First, completely remove the .nuget directory from the solution, which typically contains NuGet.exe, NuGet.targets, and NuGet.config files. In the solution file (.sln), all entries related to the .nuget project must be deleted.
Typical code segments that need removal from solution files include:
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F4AEBB8B-A367-424E-8B14-F611C9667A85}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project File Cleanup and Configuration
In project files (.csproj or .vbproj), all references to NuGet.targets must be thoroughly removed. This includes Import statements and related error-checking targets. Typical XML code that requires deletion includes:
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
Visual Studio Configuration Optimization
In Visual Studio 2015, automatic package restore requires enabling through option settings. Navigate to <span style="font-family: monospace;">Tools > Options > NuGet Package Manager > General</span> and ensure both <span style="font-family: monospace;">Allow NuGet to download missing packages</span> and <span style="font-family: monospace;">Automatically check for missing packages during build</span> are selected. These settings correspond to the packageRestore configuration section in NuGet.Config:
<configuration>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
</configuration>
Comparison of Alternative Restoration Methods
Beyond automatic restoration mechanisms, developers can use the NuGet Package Manager Console for manual restoration. The command <span style="font-family: monospace;">Update-Package –reinstall</span> reinstalls package dependencies for all projects. For specific projects, use <span style="font-family: monospace;">Update-Package –reinstall -ProjectName MyProject</span>.
Command-line tools provide another restoration approach:
- <span style="font-family: monospace;">nuget restore MySolution.sln</span> - Restore solution packages using NuGet CLI
- <span style="font-family: monospace;">dotnet restore</span> - Suitable for projects using PackageReference
- <span style="font-family: monospace;">msbuild -t:restore</span> - MSBuild-integrated restoration method
Package Management Format Selection and Compatibility
Visual Studio 2015 supports two package management formats: PackageReference and packages.config. PackageReference stores dependencies directly in project files, while packages.config uses separate configuration files. New projects default to PackageReference format, offering better performance and dependency resolution.
Configuration examples demonstrate package storage path setup:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositorypath" value="$\..\..\..\..\Packages" />
</config>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
Troubleshooting and Best Practices
After migration completion, delete the entire packages folder and rebuild the solution to verify automatic package restore functionality. If package reference errors occur, uninstall and reinstall affected packages via the package manager. For complex project structures, consider using unified package storage locations to avoid duplicate downloads.
Environment variable configuration provides additional control: Setting the <span style="font-family: monospace;">EnableNuGetPackageRestore</span> environment variable can override package restore settings at a global level. Cache management commands like <span style="font-family: monospace;">nuget locals all -clear</span> help resolve package version conflicts.