Skip to content

NuGet Package Manager

1 NuGet Package Restore

If you use NuGet references in your Visual Studio solutions I think that you should NOT check in packages to source control. You save space in your repository and I don’t think that dependencies to other sources should be in your own repository. If you as a developer adds a Visual Studio solution to source control I think it is important that other developers should be able to open the solution from source control and that they should be able to build it. Even if another developer do not have NuGet Package Manager installed or if the developer have NuGet Package Manager configured differently than yourself the developer should be able to build the solution. So to use NuGet Package Restore to accomplish this you have to do some additional steps than just enable NuGet package restore.

1.1 Enable NuGet package restore

Enable NuGet package restore for the solution, http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages.

1.2 Configure restore of project-level packages

Edit the NuGet.targets file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        …
        <!–
            Modification
            ************
            Modify from true to false. True should be default when using NuGet Package Manager 2.0.30717.9005.
            If another developer opens your project from source control and hasn't NuGet Package Manager installed or
            hasn't checked "Allow NuGet to download missing packages during build" in Tools -> Options -> Package Manager -> General,
            the project will be able to build anyhow.
        –>
        <!– Determines if package restore consent is required to restore packages –>
        <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">false</RequireRestoreConsent>
        …
    </PropertyGroup>
    <ItemGroup Condition=" '$(PackageSources)' == '' ">
        <!–
            Modification
            ************
            Add a packagesource element for each NuGet repository that is used in the solution. If you use
            a local repository it is important to omit the trailing backslash, eg. "C:\Data\Local NuGet packages" not "C:\Data\Local NuGet packages\".
        –>
        <!– Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config –>
        <PackageSource Include="https://nuget.org/api/v2/" />
        <PackageSource Include="http://nuget.mycompany.com/packages/" />
        <PackageSource Include="C:\Data\Local NuGet packages" />
    </ItemGroup>
    …
</Project>

1.3 Configure restore of solution-level packages

For the moment you are not able to restore solution-level packages from different repositories through the Visual Studio UI, you get an error, http://nuget.codeplex.com/workitem/2618.

As a workaround you can add the following command as a pre-build event in any of the projects in the solution:

“$(SolutionDir).nuget\NuGet.exe” install “$(SolutionDir).nuget\packages.config” -s “https://nuget.org/api/v2/;RepositoryPath1;RepositoryPath2;etc&#8221; -o “$(SolutionDir)packages”

Where the “-s” argument is a semicolon separated list of nuget repositories you use. If you have local repositories it is important to not include the trailing backslash in the path, eg “C:\Data\SomePath” and not “C:\Data\SomePath\”.

1.4 PS

I am aware that using a local repository in a solution that should simplify team development is not what you want. But I thought it was important to have it in this sample to remaind myself of omitting the trailing backslash when working with local NuGet repositories.

4 Comments
  1. tom permalink

    It is interesting that I did not found definition / docs what is “NuGet Package Restore”. Restore packages to where? Why restore them? Who destroyed them, so they must be restored?

  2. tom permalink

    Yeap, I guessed what it is after lot of reading (as there’s no where definition what it is). That links did not help.
    But the problem comes from initially wrong concept: Put *binaries* in *source* control. As it’s name says – it’s about sources.

    So feature “restore packages” should be named “get (nuget) dependencies” and should not be feature, but core of the product.

    • I understand the concept of “Restore” as follows:

      • “Developer A” creates a Visual Studio solution, enables NuGet Package Restore and installs a nuget package in one of the projects of the solution. “Developer A” adds the solution to TFS without including the nuget package.
      • “Developer B” opens the solution from TFS, first the NuGet package is missing. But when “Developer B” builds the solution “NuGet” handles it and downloads/restore the package.

      Regards Hans

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: