Debug Windows Services

  • Thread starter Thread starter Guest
  • Start date Start date
Elioth,

You have a couple of options.

1) Start the service in the Service Control Manager and attach the VS
debugger to a running process via Debug | Processes. I don't like this
option because the application may have already performed a lot of work
before you can attach and set a breakpoint.

2) Setup the application to run either as a service or a console
application. It's much easier to debug a console application. The
following code demonstrates what I'm talking about. There are several
variations on this theme including the use of conditional compilation.

Shared Sub Main()

If Environment.UserInteractive Then

' Run as a console application.
Dim service As Service1 = New Service1
service.OnStart(Nothing)
Console.WriteLine("Press ENTER to quit...")
Console.ReadLine()
service.OnStop()

Else

' Assume the Service Control Manager invoked the application.
Dim ServicesToRun() As ServiceBase
ServicesToRun = New ServiceBase () {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End If

End Sub

Brian
 
Back
Top