41
votes

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?

9
Could it be because the service is in release mode?pdiddy
Good thought but i had already checked that is was in debug mode.Kevin McPhail

9 Answers

64
votes

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.

39
votes

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.

8
votes

Not only did I have to select show processes from all users and the show processes in all sessions check boxes; I also had to click the Select button and then choose the Managed code.

enter image description here

2
votes

Make sure you run Visual Studio as an administrator. It will require administrator rights to attach to a running service.

2
votes

After installing the Windows service, start it. Then in Visual Studio open the Attach to process window (Alt + Ctrl + P), find your process and select it and click Attach Button in the bottom of the window.

0
votes

You may not have permissions to attach to the service. Do you have admin privileges on the desktop? Many corporations do not allow this by default, supposedly to assist in securing their environment.

0
votes

I solved my problem by changing the way the setup was generated, from RELEASE to DEBUG. And then, I installed the service and attached as usual.

-1
votes

I ran into the same issue today with none of the previous solutions working. As it turns out, it is not possible to attach to a process if you have a ProcDump session monitoring that process.

-2
votes

Use menu ToolsAttach to Process. Simply check Show Processes from all users at the bottom left. Sort by Process and try looking for your Windows service.