How do you debug a Windows Service?

Armen Tsirunyan picture Armen Tsirunyan · Mar 1, 2011 · Viewed 25.7k times · Source

I read the MSDN article on the topic. To quote:

Because a service must be run from within the context of the Services Control Manager rather than from within Visual Studio, debugging a service is not as straightforward as debugging other Visual Studio application types. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug your application using all of the standard debugging functionality of Visual Studio.

Now my problem is that my service fails to start in the first place. First it crashes, and says:

An unhandled exception (System.Runtime.InteropServices.COMException) occurred in MyServiceName.exe[3596])

and suggests me to debug it (the debugger instance instantly crashes when I choose one). Then it says

Could not start the MyServiceName service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion

So, how can I investigate/debug the reason that my service won't start? The thing is I created a console application that does EXACTLY what the service does and it works fine. (I mean I just copied the OnStart() method's and the main loop's contents to main).

Any help would be appreciated.

The Service is written in C# with heavy use of interop. I am using VS2008

Answer

BrokenGlass picture BrokenGlass · Mar 1, 2011

You could use a parameter to let your application decide whether to start as service or regular app (i.e. in this case show a Form or start the service):

static void Main(string[] args)
{
    if ((1 == args.Length) && ("-runAsApp" == args[0]))
    {
        Application.Run(new application_form());
    }
    else
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyService() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}

Now if you pass the parameter "-runAsApp" you can debug the application normally - the SCM won't pass this parameter, so you can also use it as service w/o any code change (provided you derive from ServiceBase)

Edit:

The other difference with windows services is identity (this might be especially important with InterOp) - you want to make sure you are testing under the same identity in "app" mode as well as service mode.

To do so you can use impersonation (I can post a C# wrapper if it helps, but this can be easily googled) in app mode to use the same identity your windows service will be running under i.e. usually LocalService or NetworkService.

If another identity is required you can add settings to the app.config that allow you to decide whether to use credentials, and if so which user to impersonate - these settings would be active when running as app, but turned off for the windows service (since the service is already running under the desired identity):

  <appSettings>
    <add key="useCredentials" value="false"/>
    <add key="user" value="Foo"/>
    <add key="password" value="Bar"/>
  </appSettings>