Thursday, March 27, 2014

Sharing library project across solutions and NuGet


UPDATE:
Latest NuGet doesn't rely on 'Enable NuGet Package Restore’ feature: http://docs.nuget.org/docs/reference/package-restore
So instead I recommend checking this: http://www.damirscorner.com/CategoryView,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
Api
  Api.csproj
  Api.sln
App1
  App1.csproj
  App1.sln
App2
  App2.csproj
  App2.sln
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:
http://www.xavierdecoster.com/how-to-nuget-package-restore-when-sharing-projects-between-solutions
http://blog.stangroome.com/2013/11/26/nuget-reference-paths-for-projects-in-multiple-solutions/ (see comment)
http://blog.stangroome.com/2013/12/05/visual-studio-solutions-projects-and-shared-code/
http://www.xavierdecoster.com/post/2011/11/16/why-everyone-should-be-using-a-symbol-server
http://www.edsquared.com/2011/02/12/Source+Server+And+Symbol+Server+Support+In+TFS+2010.aspx

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.
http://stackoverflow.com/questions/18376313/setting-up-a-common-nuget-packages-folder-for-all-solutions-when-some-projects-a
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:
    <config>
      <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

References:
http://forums.xamarin.com/discussion/17783/add-manage-nuget-packages-per-solution
http://lastexitcode.com/blog/2014/08/10/NuGetSupportInXamarinStudio5-2/

No comments: