A windows service exists as an executable file and may contain more than one service. The behavior of each of the service is defined by writing a class for each that inherits from the ServiceProcess.ServiceBase class and code can be added to handle various methods provided by this class. The service can provide code for OnStart, OnStop, OnPause, OnContinue, and OnShutdown methods called by the SCM as a user interacts with the service. None of these procedures are required, but they can be used to provide specific behavior in reaction to requests from the SCM’s user interface, or programmatically from other services.
The steps to be taken to create windows services under .NET:
1. Create a new project in .NET by choosing the ‘Windows Services’ template. The project can either be VB.NET or C#.
2. A class named Service1 will be created. Open the class in design view. This can be done either by pressing Shift+F7 or choose View | Designer menu item.
3. The properties could be set as necessary. The three Boolean properties – CanPauseAndContinue, CanShutDown, and CanStop- control the behavior of the service. It should look like this:
4. Add startup code. The service’s OnStart event can be used to set up the service and data structures used, and perhaps log information to the event log. Also code to handle the OnContinue, OnCustomCommand, OnPause, OnShutdown, and OnStop events.
Add an Installer. This can be done by going to the service class’s design view as done in step 2. Now, clicking on the ‘Add Installer’ will add the installer classes.
Once the Installer class has been added, Visual Studio .NET creates a new project file named ProjectInstaller.cs. This file contains two components: ServiceProcessInstaller1 and ServiceInstaller1. The properties of the ServiceInstaller1 class will show the service name in ServiceName property as Service1. The ServiceProcessInstaller1 has properties such as UserName and Password as shown in fig 2.0. They will be by default set to ‘Nothing.’ For the installer to be able to install the service, the account information should be supplied. For a simple service the account property of the ServiceProcessInstaller1 could be set to LocalSystem. The ServiceName property of the ServiceInstaller object should be the same name as that of the service class.
For a simple service all the default behavior could be accepted and the service will install fine. By default the service class’s StartType property is set to Manual as shown in fig 2.1. If the service needs to start automatically when installed, the StartType property of the ServiceInstaller1 component in ProjectInstaller to Automatic.