简体   繁体   中英

Debugging NuGet packages using Visual Studio 2017

In Visual Studio 2015 and .NET Core development we could debug NuGet packages by retrieving the source code from a source (eg 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.

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 eg release builds, however, be aware that changing configuration requires that you reload your solution AFTER the change!

I've struggled with this for a long time. Here's the easiest way that I found that works repeatably:

  • Acquire the source code and build a nuget package locally. You'll need to increment to package version. Hopefully the author has provided a .nuspec to make building easier.

Create a local nuget source:

  • Place the .nuget file that you just created in C:\\Nuget (for example)

  • In Visual Studio, select Tools / Options / Nuget Package Manager / Package Sources

  • Press the green (+) icon. Add the path to the local nuget package that you created above (C:\\Nuget) and move the package source to the top of the list.

  • In your solution, for each project, remove the previous reference to the external nuget package and add the new nuget package from your local nuget source.

  • Start debugging and you should be able to step into the code for the nuget package.

I'm able to set a breakpoint in the source code from where I build the nuget package and have it break when I run the code.

All of the steps above are a little fidgity and you may need to play with it a few times to get it to work.

Let me know in the comments if this works for you. And if so, I'd appreciate an upvote! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM