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!

8 comments:

gfh said...

we provide a power leveling and free wow gold wow power leveling

حسين نجدي said...

thanks good way




http://www.guideofnet.com/

Dissertation Writing Help said...

your post is nice . . let me tell u one thing that Blogs have become latest and important source of quality free information on net people enjoying for hours together. . .nice blog keep sharing;)
Dissertation Writing Services

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

estilo said...

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

job_search 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

Osse said...

i really like your post .. i will like to bookmark your site for my future needs :)
custom dissertation writing | dissertation writing UK

saranya zinavo said...

Excellent site you have got here.. It’s difficult to find excellent writing like yours nowadays. I seriously appreciate people like you! Take care!!
website development company