Click here to Skip to main content
Click here to Skip to main content
Go to top

Share Nuget packages between solutions: Part 1

, 4 Dec 2012
Rate this:
Please Sign up or sign in to vote.
How to share Nuget packages between solutions.

Many of you may use Nuget to add reference assemblies to your projects. Anyway sometimes we need to share a project between solutions. In this case if the relative path to this project from the different solutions is different you won't be able to use the default nuget packaging functionality. Let me explain this with samples.

  1. Let's create an empty Console application named First in folder C:\NugetConfig\First
  2. Add a dummy class library project to this solution in the same folder.
  3. Add reference in the console application to the class library. You should have the following structure now:
  4. Now add Newtonsoft.Json package to the solution:
  5. Install the package to the Dummy project:
  6. Add a dummy class to the dummy project which references Newton JSON:
  7. namespace Dummy
    {
        public class Dummy
        {
            public Newtonsoft.Json.Required Required { get; set; }
    
            public Dummy()
            {
                this.Required = Newtonsoft.Json.Required.Always;
            }
    
            public override string ToString()
            {
                return this.Required.ToString();
            }
        }
    }
  8. Add a call to the dummy project in Program.cs:
  9. using System;
    
    namespace First
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var dummy = new Dummy.Dummy();
                Console.WriteLine(dummy);
            }
        }
    }
  10. Now enable Nuget Package Restore on this solution:
  11. Now if we delete the Newtonsoft.Json folder from the packages folder and run the build the package is downloaded and the solution builds successfully

  12. Let's create a new project in another solution named Second in folder in folder C:\NugetConfig\Second.
  13. Add reference to the Dummy project 
  14. Add a call to the dummy project in Program.cs:
  15. using System;
    
    namespace Second
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var dummy = new Dummy.Dummy();
                Console.WriteLine(dummy);
            }
        }
    }
  16. We can build the second solution now as we have already downloaded the needed nuget package. But let's delete the package from C:\NugetConfig\First\packages folder. 
  17. Now we won't be able to build the solution. Let's enable Nuget Package Restore on this solution as well.
  18. Trying to build the solution leads to this error:

So here is the actual problem: In the Dummy project there is a reference to the NewtonJson.

<Reference Include="Newtonsoft.Json">
      <HintPath>..\packages\Newtonsoft.Json.4.5.10\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>

But our folder looks like this:

So we search for the Newtonsoft.Json.dll in the folder C:\NugetConfig\First\packages folder as the HintPath is relative from the Dummy.csproj file. But the build downloads the package to C:\NugetConfig\Second\packages.

You can find the code here NugetConfig-part-1.zip. To see how we can resolve this issue go to the part 2.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vasil Trifonov
Software Developer (Senior) Telerik
Bulgaria Bulgaria
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
Questionmake your own package instead PinmemberMike Lang7-Dec-12 11:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 4 Dec 2012
Article Copyright 2012 by Vasil Trifonov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid