Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug NuGet packages using Visual Studio

In Visual Studio 2015 and .NET Core development we could debug NuGet packages by retrieving the source code from a source (e.g. GitHub) to a local disk, adding the source path to the downloaded source code in global.json and reference the NuGet package in our project. This caused a reference to the projects in the downloaded source code to automatically be visible in the current solution and thus made it possible to easily debug (More about this functionality can be read about in this article).

Does anyone know how to do the same using Visual Studio 2017? Since the global.json is gone I can't find any solution for this.

like image 470
Rune G Avatar asked Mar 13 '17 17:03

Rune G


People also ask

How do I debug a .NET core NuGet package?

The easiest way to debug your nuget packages is to put the . pdb files of the packages in the build output folder of the project you want to debug. If the code you are trying to debug is classified as non-user code you need to uncheck Just My Code in the debugging options.

How do I debug an external NuGet package?

In order to debug into NuGet package libraries, Visual Studio must be configured to use ProGet as a symbol server. To do this select Debug > Options, from the menu bar, then browse to Debugging > Symbols in the tree menu.


1 Answers

I see this has become a popular question, however, MS is (as for the most time nowadays in Visual Studio) absent in requests that actually can improve their product.

There are some posts around out there on how to use the reference library from Microsoft, but this doesn't apply to all projects plus you will debug optimized release bits which is limiting in both watch and step capabilities. I also feel that this way to do it even slows down a slow Visual Studio even more. This way to do it is described in this post.

However, lately I have found a way to work around this issue. It isn't always stable, but what can be done is to add the related project to your project as a project reference.

But here are the steps I have done that mostly works:

  1. Clone the repository of the nuget package from github (or other source)
  2. Try your best to find which commit that the nuget package was built from (most projects references with either tags or branches, but don't expect this, it may be better to compare dates on the nuget package and commits).
  3. Follow the instructions from the project on how to build it, some is simply building in Visual Studio other may require more steps like using some build scripts in command prompt.
  4. Add a reference to the project in the solution, some times you'll also need to add the project that the project references to, but not always. Haven't found exactly the rules here yet. It seems that newer Visual Studio updates doesn't need this.
  5. Add a reference to the project in ALL projects that references the nuget package within your solution. Failure to do so may cause conflicts which the compiler tries it best (not good enough) to solve.

Build and debug, in your output window check that the assembly located in your project's output folder is used. If it is, just hit breakpoints in the referenced projects and you'll have full debug functionality.

It is a bit of trying and failing to make this work, but it does work eventually.

It is possible to create conditions on the project references to ensure that they isn't built in e.g. release builds, however, be aware that changing configuration requires that you reload your solution AFTER the change!

like image 52
Rune G Avatar answered Oct 11 '22 10:10

Rune G