Debug Windows Service

Easier way to debug a Windows service

If I want to quickly debug the service, I just drop in a Debugger.Break() in there. When that line is reached, it will drop me back to VS. Don't forget to remove that line when you are done.

UPDATE: As an alternative to #if DEBUG pragmas, you can also use Conditional("DEBUG_SERVICE") attribute.

[Conditional("DEBUG_SERVICE")]
private static void DebugMode()
{
Debugger.Break();
}

On your OnStart, just call this method:

public override void OnStart()
{
DebugMode();
/* ... do the rest */
}

There, the code will only be enabled during Debug builds. While you're at it, it might be useful to create a separate Build Configuration for service debugging.

How do I debug Windows services in Visual Studio?

You can also try this.

  1. Create your Windows service and install and start…. That is, Windows services must be running in your system.
  2. While your service is running, go to the Debug menu, click on Attach Process (or process in old Visual Studio)
  3. Find your running service, and then make sure the Show process from all users and Show processes in all sessions is selected, if not then select it.

Sample Image


  1. Click the Attach button
  2. Click OK
  3. Click Close
  4. Set a break point to your desirable location and wait for execute. It will debug automatic whenever your code reaches to that point.
  5. Remember, put your breakpoint at reachable place, if it is onStart(), then stop and start the service again

(After a lot of googling, I found this in "How to debug the Windows Services in Visual Studio".)

How do you debug a Windows Service?

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>

Debug Windows Service

I recommend following pattern for debug:

 var ServiceToRun = new SomeService(); 
if (Environment.UserInteractive)
{
// This used to run the service as a console (development phase only)

ServiceToRun.Start();

Console.WriteLine("Press Enter to terminate ...");
Console.ReadLine();

ServiceToRun.DoStop();
}
else
{
ServiceBase.Run(ServiceToRun);
}

Edit: make sure that your target is Console Application, not Windows Application, otherwise it will not work.

Unable to debug windows service - The breakpoint will not be currently hit

Or you could specify the path to your Symbols for Visual Studio to find.

Tools > Options > Debugging > Symbols > Add in the directory of your pdb files.



Related Topics



Leave a reply



Submit