0

Is it possible to simulate non-UserInteractive mode when debugging in Visual Studio, and if so, how?

I have a service that is running that I'd like to debug, but the behaviour is different depending on whether I'm debugging it or running the service.

I found this question:

How do I debug Windows services in Visual Studio?

The answer there doesn't quite do it for me because following that, when you debug it, the debugger runs the process in UserInteractive mode. What I want is the debugger to debug the process, but without UserInteractive mode.

For example, I had an error that was buried deep in the code because a library it uses was trying to display some sort of dialogue box (even though the dialogue box wasn't seen by the user). This would not be picked up in Debug because UserInteractive mode is used. I want to be able to do more debugging on these kinds of issues

colmde
  • 2,852
  • 1
  • 19
  • 42
  • 1
    You can use debug paratets and sepcify console there. If console is specified, the service can be ran as normal console application and your debugging should work. – Manoj Choudhari Mar 29 '19 at 11:08
  • 1
    Personally I would split the projects logic, db calls etc into separate projects then use a console app to debug. You can also use topshelf http://topshelf-project.com/ to install the same console app as a Windows Service. – Steve Mar 29 '19 at 11:10
  • I'm not sure what you hope to achieve by debugging it non-interactive. To take the example given, if you had this ability, you wouldn't have seen this dialog in debug either. Whereas I'm sure as it currently stands when you're debugging this code and suddenly a dialog appeared, it was obvious that there was a problem... – Damien_The_Unbeliever Mar 29 '19 at 11:41
  • @Damien_The_Unbeliever - I didn't want to see the dialog - however the attempt to display it was causing an exception while running as a service. In this particular case, the dialogue box does not appear on screen (it's a sort of progress indicator but displayed for such a short time that you don't see it unless there's an unexpected delay). – colmde Mar 29 '19 at 13:21
  • Hi, do you mean you want to debug the service without any UI action in vs? Just let the debugger run and break until get an error? – LoLance Apr 03 '19 at 10:09

2 Answers2

1

To debug a service you would need to

  1. Build your service in the Debug configuration

  2. Install your service to see how to do this go to this link https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-install-and-uninstall-services

  3. Start your service either from services control manager, server explorer or even from the code and if you want to know how to do this then go to this link https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-start-services

  4. Start visual studio as admin so that you can attach to system processes.

  5. Optional > on visual studio menu bar, choose Tools, Options. In the options dialog box choose debugging symbols and select microsoft symbol servers check box, and then choose the OK button

  6. On the menu bar choose attach to process from the debug or tools menu for the short key press CTRL+ALT+P

The process dialog box appears

  1. Then select the show process from all users check box

  2. In the available process section, choose the process for your service and then choose attach

Hope this helps

Tauheed
  • 11
  • 4
0

I hate attaching to a process from Visual Studio. It works, but it also seems to take forever to build the list of processes to choose from. Perhaps that's because our systems are locked down too tightly. It's entirely possible that in a different environment, this works just fine.

Still, I find it much easier just to trigger a programmatic breakpoint when the service is starting and jump in to debugging at the beginning. To do this, call the following in the OnStart() callback:

System.Diagnostics.Debugger.Break();

When you start your service, you should get a prompt indicating an unhandled exception has occurred.

enter image description here

Click the Yes option, answer Yes to the UAC prompt, select which instance of Visual Studio you want to use, and then debug normally once Visual Studio starts.

When you're finished debugging, just stop the service, and the debugger will quit automatically. However, don't close that instance of Visual Studio. Make whatever changes you need to make the service, and rebuild it. Then when you restart the service and you get to the point of selecting the Visual Studio instance to use, it'll include your original debug instance in the list. It's much faster to jump back into that one than creating a new instance each time.

HTH

Matt Davis
  • 43,149
  • 15
  • 89
  • 118