Tuesday, May 23, 2006

Debugging Windows Services in Visual Studio

Last week I started writing a Windows Service in Visual Studio for some build and test automation programs. What I have always found annoying (but understandable) about windows services is the debugging. Because your code is set up to run as a service when you select the Windows Service template in VS that means you have to install the service, start it, and then attach to the process in the debugger instead of being able to click the "start debugging" button on the debugger toolbar like I'm used to with other project types.

Previously, I followed the advice in this article by creating a setup program that easily allowed me to install and uninstall the service directly from the Solution Explorer in VS. However I still have to either call System.Diagnostics.Debugger.Break() in my code (and remember to remove it when I'm done!) or attach to the process in order to debug it.

There's an easier way. If you look in the Main entry point of the service you will see how the service is loaded. (In C# this is in the Program.cs file that's created for you and in VB.NET this code resides in the service's Designer.vb file.) You can modify this code by adding a DEBUG compiler directive to control how the process starts:
    <MTAThread()> _
    Shared Sub Main()
#If DEBUG Then
        ' Start the process as a non-service for debugging only.
        ' Stop the debugger to stop the process.

        Dim service As New MyService 
        service.Execute() 

        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
#Else
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        ' More than one NT Service may run within the same process. To add
        ' another service to this process, change the following line to
        ' create a second service object. For example,
        '
        '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
        '
        ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyService}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
#End If
    End Sub
Now when your configuration is set to debug, you can hit F5 to debug like normal. Stop the debugger to stop the process. Keep in mind that the OnStart and OnStop methods of your service will not run so you should break your functionality into a callable method on your service class (in this example, I called it "Execute()"). Have fun!

6 comments:

Hussein Nagdy said...

thanks good way




http://www.guideofnet.com/

Raffay said...

Wow Impressive! Your blog is very informative. However, it is pretty hard task but your post and experience serve and teach me how to handle and make it more simple and manageable.Thanks for the tips… Best regards.

whm reseller hosting | reseller web hosting

alestilo.nica at gmail dot com said...

Clean, clear and very elegant way to debug services. Excellent post. Thank you!!.

sattu said...

Hi! Nice post. But i am getting an error in the following line:

Dim service As New MyService
service.Execute()

the error is: Execute is not a member of Myservice.

So, can you please help me in solving this?

With regards,
satya

soukya reddy said...

This site is amazing and it is helps for students.Thanks for sharing
.Net Online Training

Janu said...

thanks for all this information. You are including better information regarding this topic in an effective way. T hank you so much.


Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery