How does one debug a Windows Service?

BigSam

New member
Joined
Jul 20, 2007
Messages
3
Programming Experience
10+
I created the logic for my Windows service in a vb.form, so that I could work the kinks out. I then copied & pasted to my new Windows Service & installed it. The new Service is started, does not report any errors, but isn't updating my database, like it did from the vb.form. How does one debug a Windows Service?

Thanks

Big Sam
 
Post moved, different topics.

Found this somewhere while searching "vb.net debug windows service":
If you want to debug your windows service, it isn't quite as straight forward as usual. Start your service, then go to Debug|Processes in the Visual Studio IDE. Find MyService.exe (you may need to check a box to display system processes), and attach the debugger. Then you'll be able to set breakpoints as usual - but not in the Start procedure.
 
I've got my service installed & started, but the service isn't doing anything - it should be logging data to a database. I've a timer that I enabled in the designer, but this doesn't seem to help. What next?

Thanks,

Big Sam
 
Its far easier to fake it out to be a windows app.


Open Project Properties (project Menu)
Application Tab
Startup Object = SUB MAIN
Project Menu >> Show All Files
Open Service1.Designer.vb (SERVICE1 is your service name)
Look for this code:

VB.NET:
    ' The main entry point for the process
    <MTAThread()> _
    <System.Diagnostics.DebuggerNonUserCode()> _
    Shared Sub Main()
        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 Service1}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    End Sub


change to be this code:
VB.NET:
[B]#IF DEBUG 
            ' The main entry point for the process
    <MTAThread()> _
    <System.Diagnostics.DebuggerNonUserCode()> _
    Shared Sub Main(args as string())
        
        Dim x as New Service1()
        x.OnStart(args)

        System.Windows.Forms.Application.Run(New System.Windows.Forms.Form())
    End Sub


#ELSE[/B]
    ' The main entry point for the process
    <MTAThread()> _
    <System.Diagnostics.DebuggerNonUserCode()> _
    Shared Sub Main()
        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 Service1}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    End Sub

[B]#END IF[/B]

Right click the toolbar area and SHOW the BUILD toolbar

Now you can select DEBUG or RELEASE build

If DEBUG is chosen, press PLAY and a blank windows form appears and your app code runs
When you compile for release this form code is NOT compiled into the exe

This because of #IF #ENDIF compiler directives
 
Back
Top