How to Enable Nuget Package Restore in Visual Studio

How do I enable NuGet Package Restore in Visual Studio?

It took far too long but I finally found this document on Migrating MSBuild-Integrated solutions to Automatic Package Restore and I was able to resolve the issue using the methods described here.

  1. Remove the '.nuget' solution directory along from the solution
  2. Remove all references to nuget.targets from your .csproj or .vbproj files. Though not officially supported, the document links to a PowerShell script if you have a lot of projects which need to be cleaned up. I manually edited mine by hand so I can't give any feedback regarding my experience with it.

When editing your files by hand, here's what you'll be looking for:

Solution File (.sln)

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 (.csproj / .vbproj)

  <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>

How do I disable restoring NuGet packages while starting the solution

The restore you are seeing there is Auto-Restore which will only run on SDK based projects.
https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore#automatic-restore-in-visual-studio

There is currently no way to disable auto-restore explicitly but you can disable restore itself.
Note that this will disable all restore (on build/rebuild & solution right click restore)

https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore#enabling-and-disabling-package-restore

Go to Tools -> Options -> NuGet Package Manager -> General -> Package Restore.
The first option disables restore itself, while the 2nd option disables on build restore.

NuGet tries to restore to make sure that the packages were not deleted from disk or that the assets file (which helps the intellisense) is not deleted. This is integral to the complete visual studio experience.
There are improvements in 15.3 (to be released within 2 weeks) that would improve the restore experience on start-up if restore was done at some point previously and if there are no changes to the package references, source etc.

Can't restore NuGet Packages

I just updated my Visual Studio 2017 from 15.6.4 to 15.7.2. It helped with packages, in my case.

NuGet package restore in builds and Visual Studio

The best practice is to make two tasks in the build pipeline - restore and build.

The option "Enable NuGet package restore" does not exist anymore from VS2015, so how the developer restore the packages? just right-click on the solution and "Restore NuGet Packages:

Sample Image

In fact, VS should do it automatically when you build, if not check this setting (under Tools - Options):

Sample Image

You need to add your private NuGet feed to the NuGet sources (Tools - Options):

Sample Image

Assets file project.assets.json not found. Run a NuGet package restore

To fix this error from Tools > NuGet Package Manager > Package Manager Console simply run:

dotnet restore

The error occurs because the dotnet cli does not create the all of the required files initially. Doing dotnet restore adds the required files.



Related Topics



Leave a reply



Submit