Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching Visual Studio debugger to Windows service -- "attach" greyed out

I am trying to attach to a windows service using Visual Studio 2010 → DebugAttach to process command. When I scroll through the list of processes my Windows service is greyed out and the attach button is also greyed out.

I have tried changing the service account to local service, my account, etc., but it remains greyed out. Is there a way to troubleshoot this?

like image 334
Kevin McPhail Avatar asked Sep 09 '10 20:09

Kevin McPhail


People also ask

How do I connect to a debugger in Windows service?

In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button. The Processes dialog box appears. Select the Show processes from all users check box. In the Available Processes section, choose the process for your service, and then choose Attach.

How do you attach a debugger to a process in Visual Studio?

You can attach the Visual Studio debugger to a running process on a local or remote computer. After the process is running, select Debug > Attach to Process or press Ctrl+Alt+p in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.

What does it mean to attach a debugger?

That means to attach a debugger (i.e visual studio's integrated debugger) to the process so you can pause it and inspect variables at runtime. This happens when you hit F5 automatically, or can be done manually using the debug menu.


2 Answers

I usually have the same issue and I take care of it by adding a boolean to my configuration that triggers a debug launch. You can launch a Visual Studio debugger instance that attaches to your Windows service process by calling this:

System.Diagnostics.Debugger.Launch(); 

What's nice is that you can call it wherever you wish in your code.

like image 72
Jeff LaFay Avatar answered Sep 23 '22 00:09

Jeff LaFay


In Visual Studio 2010, on the 'Attach to Process' dialog, check the two check boxes at the bottom that say 'Show processes from all users' and 'Show processes in all sessions'.

On doing that I found that I had my process show up twice, one running as System and one running under my local Windows login. I am not sure why (any input on this would be great!), but I was able to attach to the not-grayed-out one that showed up and was also able to step through my code this way.

The second one was the servicename.vshost.exe process as Ozz pointed out in the comments.

If you still do not see it, make sure your service is still running. Sometimes you might have a failing service that started and automatically stopped and therefore doesn't show up in the attach to options.

like image 27
3 revs, 2 users 60% Avatar answered Sep 24 '22 00:09

3 revs, 2 users 60%