Developing Windows Services
by Wei-Meng Lee08/18/2003
A Windows Service is an application that does not have a user interface. It commonly runs without human intervention and can be automatically started when the computer starts up. Examples of Windows Services are the Internet Information Server (IIS), Telnet, and FTP. Windows Services were formerly known as NT Services. In this article, I will illustrate how to create a Windows Service using Visual Studio .NET 2003.
Developing the TimeService Windows Service
In this article, I will develop a simple Windows Service that returns the current date and time. I will use the network example as illustrated in my earlier article. When this Windows Service is started, the server will start listening for all incoming network connections at port 500. Regardless of what the client sends, the Windows Service will return the current date and time.
|
Related Reading Programming .NET Web Services |
Let's now build our Windows Service:
1. Launch Visual Studio .NET 2003 and create a New Project.
2. Select the Project Types (Visual Basic Projects) and the templates
(Windows Service).
3. Name the project TimeService (see Figure 1).
|
| Figure 1. Creating a new Windows Service project |
4. Right-click on the design surface and select Properties.
5. Change the ServiceName property to TimeService. Also, set the CanPauseAndContinue
property to True (see Figure 2). You want to be able
to pause and resume the Windows Service after you have started it.
|
| Figure 2. Naming the Windows Service |
6. Right-click on the design surface and select Add Installer (see Figure
3).
|
| Figure 3. Adding the Installer |
7. Two installers will be added: ServiceProcessInstaller1
and ServiceInstaller1 (see Figure 4). These two controls
are used by the InstallUtil.exe utility when installing the Windows Service (more on this in the next section).
|
| Figure 4. Adding the two Installer controls |
8. Right-click on ServiceProcessInstaller1 and select Properties.
Set the Account property to LocalSystem.
9. Right-click on ServiceInstaller1 and select Properties.
Set the StartType to Manual.
10. Finally, double-click on the design surface to reveal the two default
methods, OnStart() and OnStop() (see Figure
5).
|
Figure 5. The default OnStart() and OnStop() methods |
Now for the coding. First, import the following namespaces:
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
You have also need to declare the following global variables:
Dim t1 As New Thread(AddressOf Listen)
Dim canStopListening As Boolean = False
Dim pause As Boolean = False
Dim log As New System.Diagnostics.EventLog
I have a subroutine named Listen() that repeatedly listens for incoming
network requests. When a client is connected, it will send the current
date and time. The global variables canStopListening and pause control
the status of the loop.
Private Sub Listen()
Const portNo As Integer = 500
Dim localAdd As System.Net.IPAddress = _
IPAddress.Parse("127.0.0.1")
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()
Do
If Not pause Then
Dim tcpClient As TcpClient = listener.AcceptTcpClient()
Dim NWStream As NetworkStream = tcpClient.GetStream
Dim bytesToRead(tcpClient.ReceiveBufferSize) As Byte
'---read incoming stream
Dim numBytesRead As Integer = _
NWStream.Read(bytesToRead, 0, _
CInt(tcpClient.ReceiveBufferSize))
'---write to event log
log.WriteEntry("Received :" & _
Encoding.ASCII.GetString(bytesToRead, _
0, numBytesRead) & " @ " & Now)
'---write time back to client
Dim time() As Byte = _
Encoding.ASCII.GetBytes(Now.ToString)
NWStream.Write(time, 0, time.Length)
tcpClient.Close()
End If
Loop Until canStopListening
listener.Stop()
End Sub


