Debugging Windows Services
Pages: 1, 2
Now, create a new Windows application to test the DLL. In my Windows application, I need to add a reference to the DLL created above (see Figure 5).
|
| Figure 5: Creating a reference to the TimeService class |
Once it is done, add three buttons to the Windows form to Stop, Pause, and Continue the service.
I have the following global variables:
Dim timeservice As New TimeServiceClass.TimeService
Public Delegate Sub ServiceDelegate()
Note that I have created a delegate called ServiceDelegate() so that
I can invoke the timeservice object asynchronously in my form load event:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim async As New ServiceDelegate(AddressOf StartService)
async.BeginInvoke(Nothing, Nothing)
End Sub
The StartService() method simply starts the service by invoking the
StartService() method of the timeservice object:
Public Sub StartService()
timeservice.startService()
End Sub
Likewise, I do the same for the Stop, Pause, and Continue buttons:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' Stop the service
timeservice.stopService()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click
' Pause the service
timeservice.pauseService()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button3.Click
' Continue the service
timeservice.continueService()
End Sub
You can now test your time service by running the Windows application, checking to see if you can stop, pause, and continue, your service.
Once you are sure that the service logic is working correctly, you can
use the service class in your actual Windows service. Here is the
code for the Windows service that uses the TimeService class (remember
to add a reference to the class):
Imports System.ServiceProcess
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Dim timeservice As New TimeServiceClass.TimeService
Public Delegate Sub ServiceDelegate()
Dim log As New System.Diagnostics.EventLog
'---------------------------------------------------------------
Protected Overrides Sub OnStart(ByVal args() As String)
Dim async As New ServiceDelegate(AddressOf StartService)
async.BeginInvoke(Nothing, Nothing)
Me.AutoLog = False
If Not EventLog.SourceExists("Time Service Log") Then
EventLog.CreateEventSource("Time Service Log", "MyLogFile")
End If
log.Source = "Time Service Log"
log.WriteEntry("Service Started")
End Sub
'---------------------------------------------------------------
Protected Sub StartService()
timeservice.startService()
End Sub
'---------------------------------------------------------------
Protected Overrides Sub OnStop()
timeservice.stopService()
log.WriteEntry("Service Stopped")
End Sub
'---------------------------------------------------------------
Protected Overrides Sub OnPause()
timeservice.pauseService()
log.WriteEntry("Service Paused")
End Sub
'---------------------------------------------------------------
Protected Overrides Sub OnContinue()
timeservice.continueService()
log.WriteEntry("Service Continued")
End Sub
End Class
You can now build the Windows service, install it, start it, and it should work correctly!
Summary
Encapsulating Windows services logic within a class allows you to debug the service easily, without the hassles of attaching a debugger to a running service. It is the recommended way to build Windows services. Using a combination of the techniques--attaching a debugger, encapsulating the service logic, and using the event log--will make life as a Windows service developer much easier and more productive.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotnet.com
-
Managed Code
2008-01-17 16:32:59 agandhe [View]
-
diagram of windows service
2006-04-25 21:54:30 Shilpi.gupta [View]
-
How to OPEN service in the debugger?
2006-02-17 05:39:42 dekeli [View]
-
How to OPEN service in the debugger?
2006-02-17 05:39:36 dekeli [View]
-
How to OPEN service in the debugger?
2006-02-17 04:55:39 dekeli [View]
- Trackback from http://jasonhorner.com/blog/archive/2005/10/22/159.aspx
Developing Windows Services
2005-10-22 09:06:40 [View]
- Trackback from http://weblogs.asp.net/jmoon/archive/2004/03/16/90624.aspx
re: Debugging Windows Services
2004-03-16 11:53:15 [View]
- Trackback from http://weblogs.asp.net/jmoon/archive/2004/01/07/48413.aspx
Debugging Windows Services
2004-01-07 13:16:28 [View]
-
Yet another way
2003-10-11 19:10:02 blowery [View]
-
That should be
2003-11-21 09:15:25 anonymous2 [View]
-
Yet another way
2003-11-19 01:53:15 anonymous2 [View]
-
why bother with the DLL?
2003-09-29 10:00:32 anonymous2 [View]
-
why bother with the DLL?
2003-12-09 15:14:34 anonymous2 [View]
-
It's not either/or, it's both
2003-09-04 13:23:17 kez_higgins [View]
-
It's not either/or, it's both
2004-03-02 10:26:49 amatlock [View]

