How to debug code in NuGet package created by me

C#Visual StudioNugetNuget Package

C# Problem Overview


I have a NuGet package I created and installed in another solution but now I need to debug the code of the package when called from my new solution.

I tried referencing the solution of the package but it's not working.

I am using Visual Studio 2013.

C# Solutions


Solution 1 - C#

To debug any dll you need the symbol file of it (.pdb). If you build your project in the debug configuration you will see that those files are generated and put in the build output folder.

Visual studio loads those symbol files from different places as described here. 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.

enter image description here

The following quote from the Microsoft - Visual Studio Docs shows what counts as user and what as non-user code.

> User and non-user code > > To distinguish user code from non-user code, Just My Code looks at > symbol (.pdb) files and program optimizations. The debugger considers > code to be non-user code when the binary is optimized or when the .pdb > file is not available. > > Three attributes also affect what the debugger considers to be My > Code: > > - DebuggerNonUserCodeAttribute tells the debugger that the code it is applied to is not My Code. > - DebuggerHiddenAttribute hides the code from the debugger, even if Just My Code is turned off. > - DebuggerStepThroughAttribute tells the debugger to step through the code it is applied to, rather than step into the code. > > All other code is considered to be user code.

A more detailed answer can be found on my blog.

Solution 2 - C#

For Visual Studio 2017 and your nuget package source code hosted on GitHub or BitBucket:

  1. Enable full debug information in *.csproj file:

    <PropertyGroup Condition="'$(Configuration)'=='Debug'">
      <DebugType>full</DebugType>
      <DebugSymbols>true</DebugSymbols>
    </PropertyGroup>
    

    or right-click project properties, build, advanced, output debugging information - set to full.

  2. To enable automatic source download and stepping for your nuget package dll, add nuget package SourceLink.Create.CommandLine to your project, or add it manually into *.csproj file:

    <ItemGroup>
      <PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.2" PrivateAssets="All" /> 
    </ItemGroup>
    

    More info here.

  3. In tools - options - debugging, disable "Enable Just My Code", and enable "Suppress JIT optimization on module load (Managed Only)".

    After this, you should be able to step inside methods from your nuget package dll.

Solution 3 - C#

I got this working by building the project the nuget package originated from in debug mode, then just copying the pdb and dll from the debug directory to the location of the nuget dll within the project I wanted to debug it in.

e.g copy from

ExternalNugetPackage\bin\Debug\

to

ProjectDirectory\Packages\ExternalNugetPackage.1.0.0\lib\net4.5

Solution 4 - C#

> How to debug code in a nuget package created by me

Just as NtFreX answered, "To debug any dll you need the symbol file of it (.pdb). ". So you can create symbol packages which allow consumers to step into your package code in the Visual Studio debugger.

The way we do it (and works):

  1. Create "*.symbols.nupkg".
  2. Deploy symbol package to SymbolSource server.
  3. Configure IDE, Package consumers can add https://nuget.smbsrc.net/ to your symbol sources in Visual Studio.
  4. Add required Library to project using NuGet (from our SymbolSource server).
  5. Debug.

For the detail info, you can refer to Creating symbol packages.

If these packages are not suitable for publishing on NuGet Gallery/SymbolSource, you can put the *.nupkg and *.symbols.nupkg files on a local disk.

Note: Add the source code to the Debug Source Files for the solution that references the package(Right click on Solution, select Properties...Common Properties...Debug Source Files, and add the root source directory for the relevant binary reference)

Solution 5 - C#

There is a much simpler solution:

enter image description here

Simply embed the debug symbols in the dll. Update your nupkg, et voila!

Solution 6 - C#

As it might help someone else, here is an additional explanation of the problem in-hand.

What do I need to debug a pkg created by me?

  1. As others here said. the .pdb file.
  2. Also, the source code as expressed here.

Well, how can I include the source code of my nuget package?

  • I got to include the symbol packages. This answer here showed me how.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYatiacView Question on Stackoverflow
Solution 1 - C#NtFreXView Answer on Stackoverflow
Solution 2 - C#xhafanView Answer on Stackoverflow
Solution 3 - C#ds4940View Answer on Stackoverflow
Solution 4 - C#Leo Liu-MSFTView Answer on Stackoverflow
Solution 5 - C#FrankView Answer on Stackoverflow
Solution 6 - C#M. AbouzeidView Answer on Stackoverflow