Developing Windows Services
Pages: 1, 2, 3
The OnStart() event is invoked when the Windows Service is first started.
And in this event, I have spun off the Listen() subroutine as a separate
thread. I then log a message indicating that the service has started,
using the event log. Using the event log is a good way to help debug
your Windows Service. Debugging Windows Services is not a trivial task
-- Windows Services run in the background and within the context of Services
Control Manager (and not within Visual Studio .NET) -- you cannot
debug Windows Services within Visual Studio .NET. Thus, a good way to
debug Windows Services is to use the event log for logging events.
Protected Overrides Sub OnStart(ByVal args() As String)
t1.Start()
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
Note that you cannot have a infinite loop within the OnStart() event,
as control needs to be returned to operating system when the service
is started. Hence I have spun off a thread (which runs continuously) to perform the actual reading
of incoming data.
The rest of the events to service are straightforward:
Protected Overrides Sub OnStop()
' Invoked when the service is stopped
canStopListening = True
log.WriteEntry("Service Stopped")
End Sub
Protected Overrides Sub OnPause()
' Invoked when the service is paused
pause = True
log.WriteEntry("Service Paused")
End Sub
Protected Overrides Sub OnContinue()
' Invoked when the service is continued
pause = False
log.WriteEntry("Service Continued")
End Sub
Once the coding is done, you can build the Windows Service by clicking on Build->Build Solution.
Installing the Service
Once the project is built, you need to install the Windows Service.
The .NET framework provides the Installutil.exe utility. To install
the Windows Service that you have just built, launch the Visual Studio
.NET Command Prompt window:
1. Navigate to the directory containing the project files and type in the following:
C:\...\Windows Services\TimeService\bin>installutil TimeService.exe
2. If the installation is successful, you should see something like the following screen (Figure 6):
|
| Figure 6. Installing the TimeService Windows Service |
Note that if you need to make changes to your Windows Service after
it has been installed, you need to uninstall it first before you build
the new version. You would also need to stop the service, if it has
been started (more on this in the next section). To uninstall the installed
Windows Service, use the /u option, like this:
installutil /u TimeService.exe
Starting the Service
Once the service is successfully installed, you need to start the service. You can use the following commands in the Command Window:
C:\...\Windows Services\TimeService\bin>net start TimeService The TimeService
service is starting.
The TimeService service was started successfully.
Alternatively, you can also start the service using the Services utility found within the Administrative Tools grouping in Control Panel (see Figure 7).
|
| Figure 7. Locating the TimeService in the Services utility |
Testing the Service
To test the TimeService, you will use the client example illustrated in my previous article. I have made some modifications here:
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Module Module1
Sub Main()
Const portNo = 500
Const textToSend = "anything"
Dim tcpclient As New System.Net.Sockets.TcpClient
tcpclient.Connect("127.0.0.1", portNo)
Dim NWStream As NetworkStream = tcpclient.GetStream
Dim bytesToSend As Byte() = Encoding.ASCII.GetBytes(textToSend)
'---send the text
Console.WriteLine("Sending : " & textToSend)
NWStream.Write(bytesToSend, 0, bytesToSend.Length)
'---read back the text
Dim bytesToRead(tcpclient.ReceiveBufferSize) As Byte
Dim numBytesRead = NWStream.Read(bytesToRead, 0, _
tcpclient.ReceiveBufferSize)
Console.WriteLine("Received : " & _
Encoding.ASCII.GetString(bytesToRead, 0, _
numBytesRead))
Console.ReadLine()
tcpclient.Close()
End Sub
End Module
Basically, the client will send a message to the server hosting the TimeService Windows Service (which is the local host, anyway) and read the date and time returned by the service. If your Windows Service is installed and started correctly, you should see the result shown in Figure 8 when you run the client application.
|
| Figure 8. Running the client |
During the lifetime of the TimeService Windows Service, it will log all sorts of information into the event log (as evident in our code). You can view the event log by going to Control Panel->Administrative Tools->Event Viewer.
Look under the MyLogFile item and you should see a list of event messages (see Figure 9):
|
| Figure 9. Viewing the event log |

