VB.Net - Service

A Windows Service is a program that starts up and runs when Windows Starts, and then it continues to run until Windows is turned off, or, until you stop the service.

Actually, Creating a Windows Service in VB is quite easy.

  • Create a Project of type “Windows Service”
  • Create a Service class which inherits from System.ServiceProcess.ServiceBase
  • Create an Installer class which inherits from System.Configuration.Install.Installer
  • Build your service
  • Install it into windows using the InstallUtil program. (for example c:\windows\microsoft.net\framework\v4.0.30319\InstallUtil Service.exe)

Wouldn’t life be great if it were just that easy?

Here are the steps in a bit more detail

Create a Project of type “Windows Service”

Enough said about that. It was hard to find windows service. I had to use the search function.(Later, I found this under Visual Basic > Windows > Classic Desktop) (for example vs2015_Service.exe)

Create a Service class which inherits from System.ServiceProcess.ServiceBase

Not really! actually when you create a project it adds a file named Service1.vb. I renamed it to SasdDirWatchService because I dont like filling up registry entries with vauge names.

Here is he code in the default template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Public Class SasdDirWatchService : Inherits System.ServiceProcess.ServiceBase

Public Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New SasdDirWatchService}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub

End Class

Given this, you are left two write 2 programs. One is executed when the OnStart() method is executed. The other is executed when the OnStop method executes.

What’s odd is I still needed the main() program to launch the service. This may be a coding problem but it does appear to only run one instance of the service. Maybe, when I’m running it in development mode, it uses main(). But when it’s installed as a service, it just run’s on it’s own. More testing is needed.

Create an Installer class which inherits from System.Configuration.Install.Installer

This isn’t so hard. If you get to the design view of the service your left with rather plain window instructing you to drag components from the toolbox, or to click to code view. But while you are at the design view, the right click menu has an option ‘Add Installer’ which when clicked will create the installer class for you.

The installer class has 2 components already on the design view. Clicking the properties of these two controls is enough coding that is needed for this

Build your service

I found lots of websites talking about creating webservices. They seemed to have a common theme, they all led the user through the creation of a webservice which posted data into the event viewer.

I can understand this example (1 person wrote the code - everyone else copied ;^). Actually this is a good example, because you dont really have a way of knowing when the program is running.

My experiences

My service is designed to watch a folder and post messages into a file as files are being added and removed from the folder.

I started by creating a directory watch program which ran ok. But the customer requested it be installed on a server and run continiously. This request is what precipated the creation of the DirWatcherService.

All of the code I wrote is in the Service class. The installer class is pure propery management. I had a problem at first but the InstallUtil program provided enough assistence to work through installation problems.

After awhile I felt like the program was running, but I wasn’t getting any results in my output file.

It took reading the errors in the Event Viewer (it turns out starting and stopping services makes a record in the Event Viewer) to discover that the Startup Object setting in the project property sheet was not pointing to the Service Class. Once I fixed that my program started to operate the way I expected.

Install it into windows using the InstallUtil program.

for example

1
c:\windows\microsoft.net\framework\v4.0.30319\InstallUtil Service.exe

After the install utility added the service to the computer, I did have to configure that service as to the account information and whether it should run always, and automatically.

Here are some references