Disabling NuGet Package Restore

Yes, this is counterproductive and a step backwards, but if you are working in an environment where your build/proxy server doesn't allow NuGet Package restore then you will need to disable it. This may sound straightforward and something that should be easily supported by NuGet, but I'm afraid you are out of luck. To disable NuGet package restores, you will need to perform some manual changes to your .csproj files.

To fully remove package restore just follow the steps below:

  • CLOSE THE SOLUTION

    Before performing any project file changes, you will need to close down the solution in Visual Studio. Visual Studio is sometimes notorious as it seems to cache .csproj files and your changes will be overwritten, unless the solution is shut down.

  • REMOVE THE .nuget FOLDER

    When you enable package restore at solution level, a .nuget folder is added at the root level of the solution. Delete this folder.

  • EDIT ALL .csproj FILES THAT USE NUGET

    Open each .csproj file in the editor of your choice and remove the following settings:

<RestorePackages>true</RestorePackages>  
<Import Project="$(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>  

Now you can re-open the solution again and perform a build to ensure that everything is working as expected.

NOTE: Remember to check-in/commit your changes to the version control server as well.

Happy coding... :)

Sajeel

Read more posts by this author.

Subscribe to The Magics

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!