Creating a Windows Service in VB.NET

Page 3 of 4
  1. Introduction
  2. What is Windows Service
  3. Adding an Installer to the Project
  4. Conclusion

Adding 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 -- ServiceProcessInstaller1 and ServiceInstaller1 -- to our existing project.

Select the ServiceInstaller1 control and open the property window. Change the ServiceName property and DisplayName property to MyService (this is the name you want to appear in the list of services in the services window). Select the ServiceProcessInstaller1 control and open the property window. Change the Account property to LocalSystem (this needs to be specified as we need to run the service on our local machine).

Now it's time for us to build the application and create an executable. Select Build Solution from the Build menu to create an executable with installation instructions for the service.

Installing the Service

To install our service we need to use the InstallUtil program, which is a .NET utility to install Windows services. You can find it in C:\WINNT\Microsoft.NET\Framework\v1.0.3705

Note: This directory might be different on your computer, depending upon the version of the .NET framework you are working with

Alternatively, 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.

Type the following command in the command window:

InstallUtil "d:\My documents\development\applications\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.

Starting the service

Running a service and starting a service are two different things -- when you install the service with InstallUtil you are running the service, but have yet have to 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:

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 (F5). You will have to keep refreshing to see the latest event logs:

Stopping 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 "d:\My documents\development\applications\MyService\bin\Myservice.exe"

(or the executable path on your computer).

Take note of the message to confirm that the service was uninstalled properly.

Tips

  • Stop the service and close the service window before you install/uninstall the service.
  • Always uninstall/install if you make any changes to the service application.
  • Try avoiding a user interface, inputs and message boxes in the service application.
  • Open ProjectInstaller.vb, select the ServiceInstaller1 control, and open the property window. Change the StartType property to automatic if you want to start the service automatically.
  • If you want to debug your windows service, it isn't quite as straight forward as usual. Start your service, then go to Debug|Processes in the Visual Studio IDE. Find MyService.exe (you may need to check a box to display system processes), and attach the debugger. Then you'll be able to set breakpoints as usual - but not in the Start procedure.

 

You might also like...

Comments

About the author

Jayesh Jain

Jayesh Jain New Zealand

Jayesh Jain is working as a Business Analyst in Auckland, New Zealand. He has several years of n-Tier development experience in developing interactive client solutions. He has a passion for Web ...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”