Life as a developer

Debugging local nuget packages in Visual Studio

It is not uncommon to have projects where you include local libraries in the form of a nuget package instead of adding the library as a reference. There can be several reasons for this, maybe you have a library that you use for several projects or you want to publish it to nuget.org. You could also consider using Submodules, but that’s a topic for another post. For this post I will show you how you can debug your local packages with breakpoints and all.

There are both advantages and disadvantages with nuget packages over including projects as is references, but especially debugging can be a chore. A common scenario is to generate packages as a part of a build process. This can be done through an extension in Visual Studio Online Services.

This is something that works well for my team, but you need to wait for the package to build and to be generated before you can test and then debugging can be hard since you need to have the symbols. This can be done of course, read more about it here.

In order to cut the testing/debugging process a bit shorter I generate packages locally, and use those for debugging.

Nuget

You need nuget in your PATH, which you can download from https://dist.nuget.org and add it to your PATH.

Or you could install from Chocolatey that does the job for you.
Install chocolatey first:
Go to https://chocolatey.org and follow the instructions to install Chocolatey.
Then run this from the command line.

choco install nuget.commandline 

Note that at the time of this writing the chocolatey package are behind current version of nuget.

In order to generate the packages from command line, I have written a powershell script (packages.ps):


param([string]$version,
      [string]$buildConfig = "Debug",
      [string]$output = "c:\Nuget")

If ($version -eq "") {
    Write-Error "Must supply a version, i.e. package1.ps1 -version 1.0.0"
    return
}
.\nuget pack {yourfile}.nuspec -Build -Properties Configuration=$buildConfig -OutputDirectory $output -symbols -Version $version

Then to run the script, open up the command line and run this command from where your nuspec files are.

.\package.ps1 -version 1.0.0

This assumes that you have already created a nuspec file. If not, you can read a short how-to here.

I add an extra nuget source that points to the location where my packages ends up.



Visual Studio makes it easy to update several packages at once. Using the -symbols ensure that the symbols are generated too, which enables debugging.

Debugging

Open up two instances of Visual Studio, one for the library and one for the project that consumes the package.
You should uncheck Require source files to exactly match the original version, which you can find under: Tools > Options > Debugging > General. Enable just my code must also be unchecked.
Start the host application, run it without debugging (Browse with, Debug -> Start without debugging or Ctrl + F5).
Switch over to the library project, set all the appropriate breakpoints.
Then attach to process (Debug -> Attach to process or Ctrl + Alt + P).
Select iisexpress, click Attach and you are good to go.

goldnarms

Arnstein Johansen is a .NET Developer with a speciality in web development, but he also dabbles in mobile development and everything new and exciting. Arnstein works for Itema AS, a mid-size consultant agency in Trondheim. Itema has 25 consultants and recently won the prize for Norway's best workplace. He likes to share his knowledge by holding talks for others, either it is clients, colleagues or likeminded developers in user groups. He has hands-on experience from large projects, the latest from Urørt where he leads a team of developers creating a music service used by around 100000 users.

No Comments

Leave a reply