Search code examples
c#visual-studio-debuggingtopshelf

Debugging Topshelf Service


I am using Topshelf to create a Windows service which uses the chrome Selenium driver to manipulate and parse various webpages.

While the service code runs fine from a normal (NetCore2) console test application, there's a problem when it runs under the Win32 console app that Topshelf requires.

Unfortunately, I haven't been able to figure out how to debug into the underlying service code. The VS2017 debugger appears to be attached to the Topshelf console app, but won't let me step into the underlying service code, which is in a separate NetStandard2 class library. Specifically, when I come to this line in the Topshelf console app:

var scanEntry = _scanner.Run();

where _scanner is an instance of the object that does the actual scanning, I can't step into the code (i.e., F11 just steps over the line).

I tried inserting the following line into the class library scanning code:

System.Diagnostics.Debugger.Launch();

but it doesn't do anything; the code just keeps merrily executing along.

I suspect this is related to the fact that the scanning code is running in a separate process that Topshelf launches. But I haven't been able to figure out how to identify it through Visual Studio's Attach to Process mechanism.

Pointers on how to debug the underlying service code when it's running under Topshelf would be much appreciated.

Additional Info

When I tried stopping on that _scanner.Run() line, and using the context menu to single step into the class library code (rather than using F11), I got prompted to turn off Just My Code, which I did. The VS debugger then tried to step into the class library code, but complained that it couldn't find the library's symbol file (*.pdb).

Which is really weird, because a symbol file with that name exists in the bin folder for the Topshelf console app.

I tried manually opening the pdb file from the bin folder, but got an error message that "A matching symbol file was not found in this folder".

Is this related to trying to debug a NetStandard2 class library from within a plain old Net console app?


Solution

  • Okay, turns out the problem was related to debugging a mixed NetStandard and NetOriginal code base...

    NetStandard class libraries, by default, do not generate debug symbol information usable by NetOriginal apps. You have to go into Project Properties -> Build -> Advanced and change the type of debug information being generated from Portable to Full.

    Once I made this change I was able to step into the class library code as per usual.

    I found this at a matching symbol file was not found in this folder