views:

222

answers:

6

I have created a windows service and installed it manually. Later started the service from Services tool. Now I want to dubug the windows service application from Visual studio IDE. When I try to attach the process from Debug tab in the IDE, the windows service process is shown in the list, but it is not highlighted to be attached. Is there any other main process that I should attach to debug service application. Any relevant information posted is appreciated.

Thanks.

A: 

see how to debug in How to: Debug Windows Service Applications

lsalamon
A: 

If you have JIT debugging turned on, DebugBreak() at startup will allow you to launch into the debugger.

+4  A: 

The easiest way I have found to do this is to put a call to System.Diagnostics.Debugger.Break() in the Main() function, the constructor, or the OnStart() callback of your service. When you start the service from the SCM, you will be presented with a dialog that allows you to start a new instance of Visual Studio or attach to an existing instance. The service will stop at your programmatic breakpoint. You can debug from there.

Of course, all of this is predicated on the fact that you are actually able to debug a Windows service. The fact that you see the service in the "attach to" list, but it isn't highlighted indicates that you do not have sufficient privilege on your machine to debug it. Get with your system administrator to figure out what permissions are required. When I've done this, I've usually had to log off and log back on after the permissions were changed to get it to work.

EDIT:

I believe this echoes the infomation at the link @divo provided, but here is how I was able to get permission to debug my Windows service (straight from my system admin).

  1. Go to Start|Control Panel|Administrative Tools|Local Security Policy.
  2. Expand the local policies and click on User Rights Assignments.
  3. Look at the "Debug Programs" setting. Your username or primary group should be listed in this setting.

Note that if your system is a member of a domain, then this setting will be controlled by group policy and cannot be changed from the local machine level.

Matt Davis
+1  A: 

Debugging services requires certain permissions:

Setting Debug Permissions - Debugging a System Service

If you can't get the necessary permission another option to debug your service is via System.Diagnostics.Trace. You can monitor the messages being traced by your service with Sysinternal's DbgView.

0xA3
+4  A: 

This doesn't answer the exact question, but for what it's worth, I have found the easiest way to develop and debug a windows service is to put all of the logic into a class library and then call the logic from either a windows service (in production) or from a regular windows form (during development). The windows form would have a Start and Stop button which would simulate the start and stop behavior of the service.

To make it easy to switch between the two modes, I just use a command line parameter and handle that in the Main method like this:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    private static void Main(string[] args)
    {
        if (args.Length > 0 && args[0] == "/form")
        {
            var form = new MainForm();
            Application.Run(form);
            return;
        }

        var servicesToRun = new ServiceBase[]
        {
            new BackgroundService()
        };

        ServiceBase.Run(servicesToRun);
    }
}

Then in the "Command line arguments" field of the Visual Studio project properties, you can just add the "/form" parameter, and it will always pop up the form when debugging locally. This way you don't have to worry about attaching to a process or anything like that. You just click Debug as usual, and you're good to go.

jeremcc
This works nicely for most scenarios, but don't forget that there are also situations where running as a service is quite different from running in an interactive session (user account, permissions, session, desktop availability etc).
0xA3
True, it doesn't work in every scenario. But for debugging the core logic, or running random interactive tests, I have found it to be pretty handy.
jeremcc
Thanks for sharing, I used this method on a project and ended up writing a blog post to flesh it out a bit if anyone is interested.http://caffeinefueled.com/posts/debugging-windows-services-in-dot-net
CaffeineFueled
A: 

Check out How to: Launch the Debugger Automatically which shows a simple way to debug services.

Stephen Nutt