How to debug code in NuGet package created by me - c#

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.

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.
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.

For Visual Studio 2017 and your nuget package source code hosted on GitHub or BitBucket:
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.
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.
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.

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

There is a much simpler solution:
Simply embed the debug symbols in the dll. Update your nupkg, et voila!

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):
Create "*.symbols.nupkg".
Deploy symbol package to SymbolSource server.
Configure IDE, Package consumers can add https://nuget.smbsrc.net/ to your symbol sources in Visual Studio.
Add required Library to project using NuGet (from our SymbolSource server).
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)

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?
As others here said. the .pdb file.
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.

I got this working by packing the package in debug mode and installing it from my local NuGet source. Then when you debug you can step into your library. You can add your NuGet local source that points to your local folder in Tools -> Options -> Nuget Package Manager -> Package Sources

Just try to use this on a function called from nuget
Picture
It Works for me

Related

How to make my nuget package able to "step in"?

Im releasing a nuget package and I'd like that whoever is using it could step into it as if it was his own code.
In order to do that, in the RELEASE configuration (which is the one I use for distribution), I try set the "Debug Information:" to Embedded and Portable but none did the trick! When I install the package, the pub files are never there...
What am I doing wrong?
The answer from memory is:
Visual Studio has a config setting for a Symbol Server which must point to source code with the same pdb file produced with the compile used for creating the nuget package.
There are different ways to host the symbol server. The easiest is to simply clone the repo from same version the nuget package was created from. Compile that code to generate the pdb. Then point the symbol server config to the bin folder containing the pdb of that compile.
Reboot visual studio.
Now try to single step into that code.
The real symbol server is produced as a website. Plenty of doc. On how to do that.

Visual Studio C# app won't build with debugging info

My C# WPF App won't load the symbols file using Visual Studio 2017. I've looked online and checked for the common issues, but can't debug my app. This is new behavior. It had been working (at least a few months ago) as part of a multi-project solution. I have no idea what has changed.
I have a breakpoint set after Main() and it shows solid red until I attempt to run in Debug configuration. As a breakpoint didn't stop the execution, I explicitly break.
[STAThread]
static int Main(string[] args)
{
Application.EnableVisualStyles(); // breakpoint on this line
Application.SetCompatibleTextRenderingDefault(false);
Debugger.Break();
Now when running, it pops up with "Symbol file not loaded" and it says "Binary was not built with debug information".
Checking the "Active (Debug)" configuration project properties, in the Build pane, I see
[x] Define DEBUG constant
[x] Define TRACE constant
[ ] Optimize code
and in the Advanced Build Settings dialog
Debugging Information: Full
Running Build->Clean and then Build->Rebuild and I see both the app.exe and app.pdb files newly created in bin/Debug/ directory. When deleting the bin/ and obj/ directories and rebuilding, I still get the same error when attempting to debug the app.
When I look at the Debug output, along with various loaded Windows DLLs, symbols loaded, I see my application with "Modules was built without symbols."
From the Debug->Windows->Modules window I see my app with "Optimized: No" and "Symbol Status: Binary was not built with debug information." In this same window, I right-clicked my application, selected "Load Symbols" and selected the bin/Debug/app.pdb file. It said, "A matching symbol file was not found in this folder".
I'm going mad trying to figure this one out. It seems it should be built with debug information, but it isn't. Can anyone point me in the right direction?
I'm going mad trying to figure this one out. It seems it should be
built with debug information, but it isn't. Can anyone point me in the
right direction?
It is quite strange issue. I think you could try these suggestions
Suggestion
1) please check whether Assembly Name(Right-click on project-->Application) has whitespace, if so, you should delete the whitespace and rebuild again to test it.
2) enable Define DEBUG constant and Define TRACE constant by right-click on project-->Build.
3) enable option Enable Just My Code and Use Managed Compatibility Mode under Tools-->Options-->Debugging-->General
4) check whether your xxx.csproj file has <DebugType>full</DebugType> node. If not ,you could add it in xxx.csproj:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>Full</DebugType>
</PropertyGroup>
5) Close VS Instance, delete .vs hidden folder under the solution folder, bin and obj folder. Then restart your project to test again.
6) If you install several nuget packages, you could run this command under Tools-->Nuget Package Manager-->Nuget Package Console:
update-package -reinstall
7) disable any third party extensions under Tools-->Extensions and Updates in case some extensions cause this behavior. And do not forget to restart VS.
8) if your VS has any latest update, please update it.
In addition,
If the new created wpf VS2017 project can be debugged successfully and your wpf project is an old project from the old VS version, I suggest you could create a new wpf project in VS2017 and then migrate the old into the new one to test whet.
You can copy the packages.config file from the old project into the new project's root directory and then run update-package -reinstall to reference these packages to your project automatically.
Any feedback will be expected.

How can I get Visual Studio to use Embedded Source code whilst debugging?

I have embedded the C# source code into a portable PDB file using the <EmbedAllSources> element in the csproj file, and I have embedded the pdb into the assembly using the <DebugType>embedded<DebugType> as described in Embedding C# sources in PDB with new csproj
My assembly looks like this:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<EmbedAllSources>true</EmbedAllSources>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
</PropertyGroup>
I can confirm that the pdb is type portable, and contains my source code:
According to the release notes for Visual Studio 15.5 the debugger should be able to use these files during debugging:
https://learn.microsoft.com/en-us/visualstudio/releasenotes/vs2017-relnotes-v15.5#EmbeddedSource
I'm using Visual Studio 2017 (version number 15.8.8)
However, when I F11 into the code from my assembly it asks for the sourcecode:
Whilst googling around there's a comment from ctaggart in the discussion https://github.com/ctaggart/SourceLink/issues/192 which says running sourcelink print-urls will print "embed" but mine doesn't, perhaps this is a hint:
Has anyone got this to work successfully? If so can you tell what I'm doing wrong?
Easiest way is to go to:
Debug -> Windows -> Modules
Inside this list, you should be able to find your assembly (dll/exe).
Right click it -> Load Symbols
It will ask for where to load the symbols from.
Navigate to the directory where your matching .pdb file is.
It should auto load. Stepping into, or having uncaught errors in this referenced assembly should trigger a break in that code, or ask you to navigate to the code file that corresponds to the file that threw the error.
In our process, we generally have to navigate to the nuget cache to find the pdbs for our installed nuget packages. (%UserProfile%/nuget/packages/YourPackage/VersionNumber/../..)
We also package up the source with those packages and navigate to those /nuget/packages .cs files as well, though, any corresponding .cs file should allow it to load.

How to debug into my nuget package deployed from TeamCity?

I have put a library that my team uses into a nuget package that is deployed from TeamCity into a network folder. I cannot debug into this code though! SymbolSource is one solution I have read about but I would much rather find some way to have access to the .pdb/source files directly from TeamCity. Does anyone know how to do this?
Edit. When I check 'Include Symbols and Source' in the Nuget Pack build step, TeamCity creates a .Symbol.nupkg in addition to the .nupkg file in the network folder. The .Symbol.nupkg contains the src and the .pdb file.
Edit. I unchecked 'Include Symbols and Source' on TeamCity and added the following to my nuspec file:
<files>
<file src="..\MyLibrary\bin\release\MyLibrary.dll" target="lib\net40" />
<file src="..\MyLibrary\bin\release\MyLibrary.pdb" target="lib\net40" />
<file src="..\MyLibrary\*.cs" target="src" />
<file src="..\MyLibrary\**\*.cs" target="src" />
</files>
This added the dll, the pdb, and the source files for my library in the nuget package and didn't generate a .Symbols file which I think is only needed for symbol servers.
Traditional method
Put the pdb in the NuGet package alongside the dll.
Add the source code to the Debug Source Files for the solution that references the package.
This means you'll be able to step through code and view exceptions, but you might have to find a file on disk and open it before you can set a breakpoint. Obviously you need to be careful that the source is at the right revision.
More detail on step
If you're currently packaging without a Nuspec, you'll need to create a Nuspec, then add the pdb to the list of files in the lib folder "NuGet spec" may be a useful command for generating the initial spec as defined in NuGet docs. Then ensure the Team City Nuget Pack step is referencing your new nuspec.
More detail on step 2
When you have a solution open, right click on Solution, select Properties...Common Properties...Debug Source Files, and add the root source directory for the relevant binary reference. Or see MSDN.
Note, you can't open the solution properties while debugging.
Still not hitting breakpoints?
Try disabling this from Tools->Options:
Modern way for public or private repos
To ensure the exact version of the source is available, embed it at build time.
From Visual Studio 2017 15.5+ you can add the EmbedAllSources property:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EmbedAllSources>true</EmbedAllSources>
Modern way for public repos
To keep your nuget and library size small, you can use the sourcelink package.
It generates a pdb that directs the debugger to the correct version of the file from your VCS provider (e.g. GitHub, BitBucket).
The latest version of dotPeek (free!) can act as a symbol server and generate pdb files on the fly. This has allowed me to debug into the dlls that are served via teamcity.
Download it here:
http://blog.jetbrains.com/dotnet/2014/04/09/introducing-dotpeek-1-2-early-access-program/
Instructions on how to set it up here.
https://web.archive.org/web/20160220163146/http://confluence.jetbrains.com/display/NETCOM/dotPeek+Symbol+Server+and+PDB+Generation
You could of course set-up & configure your own symbol server, but it's probably easiest to...
download and install Inedo's ProGet
enable symbol serving on the target feed
publish packages from TeamCity to the ProGet feed
use ProGet as your primary feed source (as it can aggregate multiple feeds including nuget.org)
All of this can be done with the free edition of ProGet.
disclaimer -- my day job is at Inedo
In your .nuspec (directly under <package>):
<files>
<file src="bin\$configuration$\$id$.pdb" target="lib\net451\" />
</files>
(change net451 to the platform you're compiling for)
If you have the source code for the package, then the foolproof (but possibly laborious) method is:
Add the source code for the package to your solution (right click Solution -> Add Existing Project)
Go through all of your projects in the solution and remove the NuGet reference to the library (i.e. open the References folder under each project and delete the reference to the package.) Then, add a reference to the NuGet package project in your solution. (i.e. Right click References, add Reference, choose Projects and tick the box for the project)
I had to do it this way when I the method I wanted to debug inside the NuGet package was called by the framework and not by my code, thus I couldn't step into it. (In my case, the method was an ASP.NET DelegatingHandler).
Once you're done you'll want to undo all your changes via source control so that the NuGet package is referenced correctly.
I've found a super simple way to do this, which I have blogged about here:
https://mattfrear.com/2017/11/29/speed-up-development-in-a-nuget-package-centric-solution/
This only works if you're using the new .NET Core style .csproj with <PackageReference> (on either .NET Core or .NET Framework).
This again assumes you have access to the source code of the NuGet package.
Build and compile the NuGet package on your local machine
Copy the .dll you've just compiled into your local NuGet packages feed folder (On my machine, this is C:\Users\matt\.nuget\packages\), overwriting the existing NuGet package .dll.
That's it! You should be able to step into the package while debugging. No messing around with .pdbs or source servers. This has greatly sped up my development cycle.
Since this question was originally posted, Jetbrains have written an entire blog post on how to accomplish this. The steps can be summarised as:
Install Debugging Tools for Windows on the agents.
Install & Enable the Symbol Server plugin.
Add Symbol Files Indexer build feature to your build configurations.
Ensure PDB files are output as an artefact.
Configure Visual Studio to use TeamCity as source server.
If you are using Nuget Package build steps, you can check 'Include Symbols and Source' to output a .symbol.nupkg which contains the PDBs. Depending on whether the Symbol Files Indexer is smart enough to look inside this file or not, you may need to change the file extension for things to work.
The full details are given here:
https://blog.jetbrains.com/teamcity/2015/02/setting-up-teamcity-as-symbol-and-source-server/
This is what I have found to work, but all the steps are probably not required...
Note: this doesn't allow you to debug both, only either the nuget
package or the solution in which it is installed.
Run Visual Studio as Administrator
Open and Start the host application (the one in which you installed the Nuget package) without debugging (Ctrl + F5)
In the Nuget package solution, ensure that Tools > Options > Debugging > General > "Require source files to exactly match the original version" is NOT checked.
Ensure that "Enable just my code" is NOT checked
Add a new folder in Tools > Options > Debugging > Symbols pointing to the source directory of the Nuget package. (You literally enter the folder path , see image below)
Click Debug > Attach to Process...
Find iisexpress (there may be multiple, it won't do any harm attaching to all)
If your code is in a public Git repository, or, at least in your network, is accessible without authentication, then GitLink would be an option:
https://github.com/GitTools/GitLink
GitLink makes symbol servers obsolete by changing the PDB to point to the Git server. But, as said before, this makes it necessary for the Git repository to be public - until now there's no "proper" way to authenticate when accessing a private repository.
Microsoft has now integrated the SourceLink NuGet package at https://github.com/dotnet/sourcelink/ which allows source code to be downloaded on demand while debugging if the publisher of the NuGet package sets it up.

a matching symbol file was not found in this folder

I want to use debug symbols, but I am receiving the following error:
a matching symbol file was not found in this folder
What is this problem, and how to solve it?
One of the things I've ran into with was because debug was off on the project referenced where the code lives. In my case, I made a new configuration called "Developer" and by default debug was turned off.
Right click the project in question
Properties
Build
Advanced (right bottom corner)
Set Debug Info to full
Recompile
I had the same problem as #DmainEvent. Apparently the dll that I was using was not the same version as the pdb that I had just compiled, so I got the error message.
If you have this problem, try using the dll and pdb from the same compilation run.
The error I got was "a matching symbol file was not found in this folder" in the Debug => Modules window even after both the DLL and PDB were available and built together, so I was unable to debug into the target DLL referenced by my main project.
Posting this here in case it helps someone browsing with "Mixed Platform" build for target DLL. I did two things to get past this:
In the solution using the target DLL, Uncheck "Just My Code" in Tools => Options => Debugging => General => Enable Just My Code (JMC).
Check "Enable native code debugging" in target DLL solution in relevant Project Properties => Debug.
I tried all the possible solutions, finally it worked when I disabled the option Enable native code debugging under the Debugger engines of Properties > Debug.
I ran into this problem and the answer was simple.
Visual studio has two project level settings that can create .pdb files.
Linker: Configuration Properties -> Linker -> Debugging -> Generate
Program Database File = "xxxx.pdb"
Compiler: Configuration Properties -> C/C++ -> Output Files -> Program Database File Name =
"yyyy.pdb"
You want #1 for debugging. Forget about #2.
Give file #2 a different name than file #1 to solve this error.
I don't know why microsoft specifies #2 as a .pdb file. That is just confusing.
I have fixed my debug symbols, forcing them to match using this tool:
chkmatch tool
edit: the website is down now. the wayback machine helps: https://web.archive.org/web/20210205095232/https://www.debuginfo.com/tools/chkmatch.html
So, my problem was I was trying to debug my project and the debugger couldn't step-in to the in-house nugets sources. I had the source files of the nuget project. Still the visual studio didn't accept the pdb files I was trying to show it to.
Showing exact same error:
a matching symbol file was not found in this folder
So, what I did was I added this to the .proj file of the nugets project:
<DebugType>full</DebugType>
And created the dll and pdb file again using the rebuild option.
In the command line I ran:
.\ChkMatch.exe -m name_of_your.dll name_of_your.pdb
It said this:
Writing to the debug information file...
Result: Success.
Great success!
So, next, I referenced this dll instead to the proj I was trying to debug. I worked when I tried to load the symbol again.
Hope it helps.
Without more details as to what you're doing, it's difficult to go beyond "the debugger is looking for a symbol file which matches the compiled code, and couldn't find one in the folder where the compiled code lives."
Some things to think about:
Are you creating symbols as part of your compilation? (check the project properties)
Are you using a symbol server (if so, does it point to the right place)
Is this compiled code from a third party? In which case, as you apparently have the source, compile it yourself.
Consider clarifying your question if you want a better answer. Especially what do you mean by "I want use of Symbols".
For BizTalk (and other) projects, it could be because there's a version of the assembly you're trying to debug already in the GAC. When you run a unit test or hit F5 to debug, a new version is compiled locally. However, the version in the GAC is being used, and the newly created PDB doesn't match the DLL in the GAC.
One way around this is to deselect a build for everything except your unit test project using the Configuration Manager, as shown below:
Well, the solution depends on your specific problem. I tried everything that could be possibly found on Stackoverflow and other sites. One of the thread that I followed is this. That did not help too.
The problem was at once resolved when I noticed that my executable project did not contain a reference to the library that I wanted to debug. So I just added the reference to that project.
**PS: ** This problem might also arise because the assembly referenced by the executable assembly might not match that in the references. So in that case, you just remove the already existing reference and add the new one.
Hope this helps!
The same happen to me because the .pdb file of the project have not been copied to the debug\Bin folder, so the symbols could not be loaded from the .pdb file.
You must rebuild your project and manually copy the symbols (.pdb file) to the debug\Bin folder of executable project.
I was trying to load symbols for a installed nuget package published on our local dev server. I had to uninstall and add a normal reference built from the code instead. This worked for me. Just remember install the original nuget package again once finished debugging.
If it works for you, try to embed debug symbols in the dll itself, so the symbols are loaded automatically. This worked for me in netcoreapp3.1 and net5.0:
<DebugType>Embedded</DebugType>
<EmbedAllSources>True</EmbedAllSources>
Beware that you may find this in documentation:
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
but it does not work.
I ran into this with Visual Studio 2022, tried the most of answers here. Fixed by switching back to Visual Studio 2019, seems like a bug in 2022.
My issue was a bit simpler to resolve, but still the issue the question asked. At first, I was not publishing the pdb file with the nuget package another project was using. Once I confirmed that, I removed the nuget package from my project and readded it from our network nuget source. That still didn't let Visual Studio pick identify the PDB location.
Then I noticed that if you select one of your nuget packages, ( Project --> Dependencies --> Packages --> Choose nuget package), there is a Path property. I checked that location and it pointed to %USERPROFILE%.nuget\packages. The pdb was not at this location and the Date Modified was older than the latest package I published. Once I deleted the folder for the given package version, removed it from my project, and re-added it, the latest .dll and .pdb file were added to this location.
After that, I was able to step into the code of my nuget package and had no further issues.
To get the nuget project to produce the pdb file in the first place, I added <IncludeSymbols>true</IncludeSymbols> inside of a PropertyGroup within the csproj file as other answers had directed.
Once I rebuilt that nuget project, it generated 2 *.nupkg files:
Namespace.x.x.x.x.nupkg
Namespace.x.x.x.x.symbols.nupkg
I found this was because the Properties => Debug => Start Action was set to Start external program instead of the Project. So the newly generated pdb file didn't match, because the actual exe was the wrong one.
I have had this problem recently as well.
Was able to fix it by selecting MyProject->Properties->Linker->Debugging->Generate Debug Info->"Optimize for debugging (/DEBUG)".

Categories

Resources