VB.NET - Creating a Windows Service Part II
VB.NET and inserting an Installer to the Project
Open the service1.vb design window, right click on it and select Add Installer option, which will add an installer project called ProjectInstaller.vb with two controls to our existing project. Select the ServiceInstaller1 control and open the property window. Change the Service Name property and Display Name property to My Service. Select the ServiceProcessInstaller1 control and open the property window. Change the Account property to Local System.
Here by you can build the application and create an executable. Select Build Solution from the Build menu to create an executable with installation instructions for the service.
Installation
We have to use the InstallUtil program, to install our service. It is a .NET utility to install Windows services. It can be viewed it in C:\ WINNT\ Microsoft.NET\ Framework\ v1.0.3705.
This directory might be different on your computer, depending upon the version of the .NET framework you are working with. Also, you could run a .NET Command Window by selecting Start -> Programs -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools >- Visual Studio .NET Command Prompt, which sets all of the required paths for you.
Just type the following command in the command window: InstallUtil C:\tgol\Myservice\Bin\Myservice.exe. This is the path for the executable of the service we just created. Remember that VB.NET created the executable in the Bin folder under the project folder, so make sure you change this to your executable path.
Our service is now started. Open the Event Viewer from Administrative Tools and click Application Log to see the logs created by the Service (MyService) every 10 seconds. If you don't see any logs click refresh .
Beginning the Service
To run a service and to start a service are different facts. On installing the service with InstallUtil you are running the service but start it.
To view and start the service, open Control Panel -> Administrative Tools. Now click Services, locate MyService, right click on it and select Start to start it.
The service is started now. Open the Event Viewer from Administrative Tools and click Application Log to see the logs created by the Service (MyService) every 10 seconds. If you don't see any logs click refresh (F5). You will have to keep refreshing to see the latest event logs.
Discontinuing the Service
This procedure is similar to installing the service but now we shall run the InstallUtil with the /U parameter, which will uninstall the service: InstallUtil /U C:\tgol\Myservice\Bin\Myservice.exe (or the executable path on your computer).
Take note of the message to confirm that the service was uninstalled properly. Some facts we have to take in to consideration here:
1. If you make any changes to the service application always uninstall/install.
2. Stop the service and close the service window before you install/uninstall the service.
3. Open ProjectInstaller.vb, select the ServiceInstaller1 control, and open the property window.
4. Change the Start Type property to automatic if you want to start the service automatically.
5. Try avoiding a user interface, inputs and message boxes in the service application.
Ending
Use a Windows service instead of a standard application when you need to monitor or administer something in the background. There are more positive aspects then negative when selecting a Windows service over an application running with Windows Scheduler. Windows services are often overlooked. The only disadvantage of using a service is the installation procedure.