Thursday, March 27, 2014

Sharing library project across solutions and NuGet

Latest NuGet doesn't rely on 'Enable NuGet Package Restore’ feature:
So instead I recommend checking this:,category,DevelopmentNuGet.aspx

I have several applications using one library where communication to the backend API is implemented.
Here’s how the folder hierarchy for projects looks like
Each app solution (App1, App2) has Api.csproj added.
The main reason why Api project is added to app solutions is we want to debug and step into the code of Api library easily.
But this might not be a good idea because each app can depend on a specific version of the Api library. If something needs to be changed in the Api which affects all applications, all applications would need to be updated and sometimes the impact of the changes in the app is complex.
For this reason, we might want to have each app depending on a specific version of the Api, so instead of adding the project as reference, we could add the compiled library as reference.
We can solve the problem to step into the library source code by also having library.pdb files along with the .dll.
More info: (see comment)

However, suppose we still want to add the library as source code to app solutions.
There’s a problem with NuGet that it’s creating ‘packages’ folder per solution.
Here’s how to do it:
  1. delete existing ‘package’ folder in each solution folder
  2. for each solution, right-click on the solution file in the solution explorer > ‘Enable NuGet Package Restore’
  3. this will create a .nuget folder. open the NuGet.config
  4. add the following under <configuration> node:
      <add key="repositoryPath" value="../../packages" />
    </config> (NOTE: if you're using Xamarin, you MUST use forward slashes, otherwise on Mac you will get a folder with the name '..\..\packages' !)
    it instructs NuGet to use ‘packages’ folder located at the same level with Api and apps solution folders.
    note that this path is relative to the location of NuGet.config file
  5. go to each .csproj and make sure all paths which reference ‘packages’ folder are set to “..\..\packages’
  6. delete each ‘.suo’ file in solution folders
  7. reopen Visual Studio
  8. open each app solution and build it.
    for the first solution being built, NuGet brings the packages in the new ‘packages’ folder.
    all other solutions will reference the already downloaded packages


No comments: