Yes, debugging Windows Service is hard. I use two approaches.
1) You can use "Attach Debugger" feature. You can run the Service in a regular way and later attach the debugger to existing process. See Debug menu under main Visual Studio menu. It's hard to debug the very start of the service (
OnStart
). One trick is to insert big delay using
System.Threading.Thread.Sleep
in the very beginning, some time needed to attach the debugger.
2) I developed dual-mode application (library) which can run as interactive application or as Windows Service. Here is the key: during run-time, you can read the property
System.Environment.UserInteractive
(
http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx[
^]) to act accordingly. In this way, most of the debugging can be done using interactive mode. The problem is: the mechanism of dual application can be well debugged in the very beginning. You can develop as a separate .NET library and debug it on some most simple service code.
Additional advice: use logging extensively. Use
System.EvenLog
to log in the Windows System Log. As I use dual-mode application, I also use dual-mode logging (second mode is logging into some UI control). However
EventLog
using System log is much more reliable.
See my past answers for useful code samples:
Custom logger:
MsBuild OutPut to the TextBox on the fly in Windows Application[
^].
Logging with System Log structure:
How to create event log under a folder[
^].
—SA