Debugging referenced DLL in Visual Studio - c#

I have multiple projects (lets call one MyLibrary) in Visual Studio that contains a lot of nifty functions and classes I use in my other projects. I add a reference to MyLibrary.dll directly and not the project itself.
However, I now need to breakpoint my way through something in MyLibrary while I run one of my other projects. How can I debug my dll the way I've set up my projects? I know for a fact I've done it earlier with these projects, but since I didn't do anything special, I don't know what's missing.
I've turned off Just-my-code and followed some tips like that, but I haven't gotten into .pdb's yet. If I need one to debug another DLL, I would appreciate some information on how to export that from MyLibrary.

While debugging, check in Debug/Modules, if your dll symbols are loaded. If not, let them load. You may be asked to set the folder with pdb files. Pdb contains the symbol used for debugging. It has to be the same version as dll file you are using in your projects. After that you will be able to debug the library.

You might need to go to the properties of a DLL, and ensure that Copy Local is set to True. Otherwise, Modules won't be able to find the .pdb for your reference
If that doesn't help you can try to rebuild your external library using debug mode, then copy manually both .dll and the .pdb file to the bin of your app.

Related

How can I get my ServiceInstaller project to include PDB files?

I have a windows service, which when built, includes the PDB files in the output.
I have a Service Installer project, that generates an MSI for installing the service.
I'd like to make it also include PDB files in the output (at least for a particular build configuration, but for all is fine too).
For other project types, you can generally go to Properties and change the settings, however there isn't much on the properties on the ServiceInstaller.
edit:
After some fiddling I've found that I can add the debug symbol output of a project, if I apply it to the service, it seems to include the PDB of the service, which is great. However I was expecting it to include all the pdbs, for all the referenced projects too. Since the service is quite slim and calls into other libraries... Will keep at it...
In the end, I told it to add debug symbols from every project individually by adding them one by one.
This is not ideal, because if projects are added the person adding them will need to remember to add their output to this, whereas if I could get it to just do *.pdb, the target service automatically includes pdb files for all of its referenced projects so that wouldn't be necessary.
However it has done the job for now. I will not mark this answer as accepted in case someone knows of a better way.

How can I debug class libraries in VS2013 using breakpoints?

I am attempting to debug a class library in VS2013, however whenever I set a breakpoint in the library, it seems to be ignored when executing the code.
I am using a console project, which is located in the same solution to call the code of the class library. I've added the .dll, which has been generated by the class library, as a reference to the console project in order to connect the two projects.
How do I connect the two projects properly and stop VS2013 from ignoring the breakpoints?
Sounds like you need the PDB file(s) associated with the library's (DLL) files.
Without PDB files, it's possible to set a breakpoint on the library code, if you can see it, but the red breakpoint circle will say something like "Symbols could not be loaded...." and you won't land on any breakpoints.
So, if you can get the PDB files of the library, copy them to the bin folder of your project, add a reference to the library DLL, and hopefully that should make Visual Studio happy.
Also, when debugging the code in VS, if you go to Debug->Windows->Modules, that will open a new window in the IDE which shows loaded assemblies. That window will also tell you whether the symbols (PDB) have been loaded.
My solution had two class libraries, one application that needed them. Couldn't break anywhere in the class library code. All settings previously mentioned here were checked. I solved it by removing the class library dll from the references and then adding it back in as a reference. Voila. Previously ignored breakpoints in the class library became active.

Project's referenced DLL's path changes "by magic" when DLL is deleted

Hereis the situation:
I have a folder that contains library DLLs, which are not built as part of my solution - lets say it is .\libs.
I add references to these DLLs. I then build. Everything is fine.
If I then delete the libs folder and rebuild my solution, the compilation still succeeds! Weird - I would have expected compilation errors since the library dlls are not there!
But looking at the reference properties in Visual studio, I see that the reference path has been changed from .\libs\foo.dll to myproject\bin\Debug\foo.dll. So it is picking up the referenced DLL from its old build output.
If I open myproject.csproj in a text editor, I see that the HintPath of the reference is still .\libs\foo.dll. If I re-create the libs folder, visual studio still uses myproject\bin\Debug\foo.dll (it does not revert to the actual DLL I want!)
Is this expected behaviour?
Is there a way to stop this behaviour because it is causing me problems - especially when I want to rebuild myproject with different versions of the libs: half the time I find that I am using a different version than what I wanted.
This is not magic. Your DLL's Copy Local Property is probably set to true, that's all.
Setting it to false will get you the desired behaviour.

Visual Studio 2010 dll create error

I am new to C# programming and I have a problem with dll creation.
I opened a class library project and write public static methods in my classes. Clicked debug and copied dll and pdb files (under bin/debug/..) to my WPF application project.
I didn't get any reference problems also editor shows my methods normally, also when I use them it gives me no error or warning...
However, when I run my program, I saw that my methods calling dll methods are not working. In addition, debug mode also jumps my methods so I cant trace the code.
Where am I doing wrong? Is there any other way to create dll or am I missing a trick in here?
Thank you..
Rather than copying the DLLs into your WPF app's bin directory, you should either add a project reference to your class library from your WPF app, or add a reference to the output directory of the class library. Otherwise the build is probably copying over your hand-copied files. Basically, you should treat anything in bin as "controlled by Visual Studio" IMO - don't copy anything there manually. It helps if you use project references rather than referring to specific files, too - that way each build gets an appropriate configuration for its dependencies.

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