To get started, you'll need to create a Visual Studio .NET Project for your new Service. Select File | New Project from Visual Studio, then select Windows Service from the appropriate templates window (Note: in VB Standard edition this template is not available. However, the code listed in this article will still compile) – the name of your project will become the name of your executable but not necessarily the service. Once the project is started, right-click anywhere in the designer and select “Add Installer”. This adds a Project Installer file to your project with two components called during Service installation to configure Registry keys that affect your Windows Service. You should set all of the following properties of the ServiceInstaller and ServiceProcessInstaller components:
This is the registered name of the service. This name is used to start and stop the service from the command line (e.g. NET START EmptyService).
This is the long name of the service that will appear in the “Name” column of the Windows Service Manager dialog.
This is the default state of the service. Disabled means the service will not start, manual means that it can be started, and automatic means that it will start when the OS boots up. This can be changed administratively in the Service Manager dialog.
This is account under whose security context the Service will run. This can be changed administratively on the “Log On” tab of the Service Manager dialog. Setting this property to “User” will cause a prompt for username/password when the service is installed (Alternatively, ServiceProcessInstaller.Username and Password properties can be set from the code-behind for the Project Installer).
There are several other properties you may consider setting at this point.
This requires other services to be running before this service will start, and causes this service to stop if they stop.
This is only available with .NET Fx 2.0 (although a low-level hack is available), sets the full description displayed in the Service Manager dialog.
The following properties of the Service itself are available from the Designer as well:
When set to True, the service will automatically report Start, Stop, Pause and Continue commands to the Windows Application Event Log (custom commands are also logged).
Some OS services do not accept stop commands, and for these CanStop is False. In most cases, you want to accept a Stop command. If you do not implement OnStop method (see below) the method of the base class will be used.
If you want to enable pausing for your service, you must set this property True and implement the OnPause/OnContinue methods (see below). If this property is False, these methods do not get called. Pausing should allow processing to stop without full take-down and cleanup of Service resources.