Advanced .NET Framework (Windows Services): Creating a Windows Service
This sample shows some how a Windows Service is created and deployed.
Featured Highlights:
The code demonstrates how to build a Windows Service that includes methods that
respond when the service is started, stopped, paused, continued, and when the
service is stopped due to a system shut down. In addition, code is provided to
demonstrate how to us a deployment project to deploy the Windows Service.
Requirements:
-
Trial or Release version of Visual Studio .NET Professional (or greater).
-
A Windows Operating System that supports services.
(Windows NT Server 4.0,
Windows NT Workstation 4.0, Windows 2000,
Windows XP Home Edition, Windows XP Professional, or
Windows .NET Server family)
Running the Sample:
To run the sample:
-
Install the Windows Service Demo by double clicking on the How-To Windows
Service - Time Track.msi
file.
-
Doubleclick the How-To Creating a Windows Service.sln
file.
-
Explore the How-To Windows Service Demo
project for an example of creating a Windows Service in code.
-
Explore the How-To Windows Service - Time Track Install project
for an example of creating a deployment project for a Windows Service.
Notes:
Although completed sample code can be useful, it doesn't show the process for
creating a Windows Service on your own. There are three very important steps.
-
Create the Windows Service. Simply create a new Windows Service project
in Visual Basic. You are provided with a overrides for the OnStart and OnStop
methods. Add code to these methods, and implement any additional overrides
(such as for the OnContinue and OnPause methods). You must also set the proper
parameter. For instance, the CanPauseAndContinue must be set to True for
the service to be able to accept those events.
-
Add an Installer. You can add an installer by right clicking on the design
palette of the windows service, and selecting "Add Installer". The installer is
required to make the proper changes in the registry, and to get the service
hooked into the right event sources. You can then configure the installer. In
your ServiceProcessInstaller, you need to determine the account that will start
the service. Popular choices are LocalSystem and User. In the
WindowsServiceInstaller, you need to specifiy how the service will start.
Choose Manual if the service should be started by a user action. Select
Automatic if the service should be run on startup.
-
Create a deployment. Windows Services can be deployed by using the
InstallUtil.exe utility that comes with the .NET Framework. However, a more
convenient method is to use a deployment project to create an MSI file. This is
only slightly tricky for a Windows Service. First, add a deployment project to
the solution containing the service project. Then create a Custom Action for
the primary output of the Windows Service project. (To do this, right-click on
the deployment project name and select View -> Custom Actions. Then right
click on the Custom Actions and select Add Custom Action. When the dialog box
opens double click the Application folder and select the primary output from
your Windows Service project. It will add this custom action to all four
installer actions: Install, Commit, Rollback and Uninstall.) You can now Build
your solution, and the resulting MSI file will install the Windows Service.
See Also:
System.ServiceProcess
Namespace,
ServiceBase Class,
ServiceInstaller Class,
Deploying Applications,
Introduction to Packaging and Deployment