Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#

Share Nuget Packages Between Solutions: Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Dec 2012CPOL2 min read 21K   28   5   1
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.

C#
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();
        }
    }
}
C#
using System;

namespace First
{
    class Program
    {
        public static void Main(string[] args)
        {
            var dummy = new Dummy.Dummy();
            Console.WriteLine(dummy);
        }
    }
}

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.

C#
using System;

namespace Second
{
    class Program
    {
        public static void Main(string[] args)
        {
            var dummy = new Dummy.Dummy();
            Console.WriteLine(dummy);
        }
    }
}

  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. Add a call to the dummy project in Program.cs:
  8. Now enable Nuget Package Restore on this solution:
  9. Let's create a new project in another solution named Second in folder in folder C:\NugetConfig\Second.
  10. Add reference to the Dummy project 
  11. Add a call to the dummy project in Program.cs:
  12. 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. 
  13. Now, we won't be able to build the solution. Let's enable Nuget Package Restore on this solution as well.
  14. 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.

XML
<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. To see how we can resolve this issue, go to part 2.

License

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


Written By
Software Developer (Senior) Telerik
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

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

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