Paul Ballard's WebLog

.NET All Day.... .NET All Night. The semi-coherent ramblings of a sleep deprived mind.

Debugging Windows Services

Mike Diehl had a recent blog post about debugging Windows Services.  As I've been doing quite a bit of this lately, I thought I might jump in with some more information on Windows Services and the fun that is debugging them.

Debugging Service Startup

When a Windows Service is installed into the Service Control Manager, it doesn't start running until it's either started manually or if the installer StartType property is set to "Automatic" then after the system reboots.  What this means to your debugging is that you can't simply install the service and attach the Visual Studio debugger to the process as there isn't one to attach to until after you start the service.  However, once you start the service if you have a bug such as an exception in the service's initialization you won't get the debugger attached to the process before it's too late.  So then, how do you debug the service class' initalization?  Using the System.Diagnostics.Debugger.Lauch() method.

// The main entry point for the process

static void Main()

{

#if DEBUG

      System.Diagnostics.Debugger.Launch();

#endif   ...

This method pops up the following screen asking you which instance of the debugger it should use to debug the application.



Note that I have placed the Debugger.Launch method call inside a "#if DEBUG" compiler directive.  DEBUG is a predefined directive that is automatically added when the application is compiled in Debug mode.  Therefore, while I am working on the application the Debugger.Launch method will be called, but when I switch to Release mode the C# compiler will skip that command.  This will keep you from having to remember to remove all of your Debug code (not that you shouldn't be doing that anyway).

Debugging After Startup

After the service has intialized and the OnStart override has been executed the service will begin its work.  If you've used the method listed above to start the Debugger when the service starts up you can then create breakpoints in Visual Studio or you can put Debugger.Break() methods into your code where you want the debugger to stop code execution. 

You can however choose to debug a service that is already running, by attaching the Visual Studio Debugger to the process.  Click on the Debug menu option and select "Processes".  This will bring up the following window listing the processes currently running on the machine. 



Select the process you want to debug and hit the 'Attach..." button.  The next window asks what types of programs you want to debug.  Make sure the "Common Language Runtime" option is checked and hit OK.  You are now debugging that process.  If the EXE is compiled in Debug mode and the PDB file is in the same directory as the EXE, you should see the source code for your service.  Just set your breakpoints and you're off and debugging.



Recompiling The Service

When the Service Control Manager tells the service to stop, the process for the service should finish up any work and end gracefully.  As long as this is happening and no threads are left running, you should be able to simply recompile your service in place without reinstalling it.  However, if you do leave any residue running Visual Studio will give a build error saying that the  EXE file cannot be copied because access is denied, file in use by another process.  This is a design issue so if you find that after stopping your service you still cannot access the EXE file, you should debug the application to make sure you have cleaned up all of the resources and threads.

Execution Permissions

I tested every version of  the Account property settings and no setting would prevent me from debugging the application.  Therefore, you should not need to add any users or groups to the "Debugger Users" group on your machine.  My testing on this was pretty quick though and so there could be some instances where this might be required. 

Hope this information helps in your Windows Service Debugging

Posted: Jul 12 2005, 10:31 PM by PaulBallard | with 9 comment(s)
Filed under:

Comments

Dean Harding said:

You need to be in the Debugger Users group if you're not an Administrator on the local machine and you want to attach to a process running under a different account.

Of course, you also need to be an Administrator to be able to start/stop services, so you'll run into problems there as well.

Generally, when I've developing services using a non-Admin account, I'll add a command-line option to my service process which bypasses the actual service stuff and just calls OnStart then waits for a keypress, then calls OnStop - essentially "faking" what the service control manager does.

Actually, I might put a little something about this on my blog later...
# July 12, 2005 11:53 PM

TrackBack said:

Dean
# July 13, 2005 7:38 AM

Karl said:

I am trying to debug a windows service that is running under a windows domain account. I am using the debugactiverprocess function to try and connect to the process. I have already changed the security token and am impersonating my own account (which the process is running under) but it will not bind to the process. If the account runs under the local system account then it will work ok but not under a domain account.
Do you know what the reason behind this is?

Karl
# July 18, 2005 1:23 AM

.NET (Never Explain Twice...or something like that) said:

I've been working on my "super duper mucho excellente" windows service project and the obvious questions

# September 29, 2006 7:02 PM

Anderson On... said:

If you are taking time out to read this post, you have probably experienced the pain of debugging windows

# June 17, 2007 5:06 PM

dave-smith.net » Windows Service Development said:

Pingback from  dave-smith.net » Windows Service Development

# December 5, 2007 12:13 PM

PLR Articles - Monthly Private Label Articles said:

PLR articles is another method of marketing your products through outsourcing

# April 13, 2008 6:44 PM

foreclosures said:

Most large rental truck fleets have offices in most major cities as well as larger size towns, something most of the large professional moving companies don t have. This makes it much easier to find, deal with, rent and finally return the rental truck

# August 3, 2008 11:24 AM

Design .Net said:

Lately I have been trying to find a good and simple way if debugging a Windows Service Project most ways

# August 26, 2008 5:29 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)