AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1319&pId=-1
Introducing Windows Services
page
by Uday Denduluri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27743/ 88

Overview

A service is a long running executable program that does not support any user interface. A service program runs under the system account. The system account is used by the operating system and by services that run under Windows. The system account (Internal account) was designed since many services and processes within Windows need the capability to log-in internally. ServiceProcessInstaller class installs the windows service. While installing, it specifies the user account under which the service will run. The instance of the ServiceProcessInstaller takes a property of the type Account. Account is of the type "ServiceAccount" which is of the type enum. The values of ServiceAccount enum are shown below.

Enum Type

Description

LocalService 

Uses anonymous credentials

LocalSystem

High privilege

NetworkService

Extensive local privileges 

User

Very specific to user

ServiceBase Class

The base class for all the windows services is ServiceBase class. ServiceBase class has methods like OnStart, OnStop, OnPause and OnContinue which can be overridden by the derived classes. Let us see the ServiceBase class in detail and understand it before we create a windows service. When the service starts the OnStart method of the service runs. The Service Control manager is responsible for running the Service. All the events, like OnStart, OnStop, etc. are accessed by the Service control Manager. It is to be understood that this is not similar to normal executable file execution. The executable initially calls the derived class constructor of ServiceBase. After this the OnStart method takes on. It is to be understood that all the resources or objects that are needed to be cleared off in the Onstop event should be created in Onstart event and not the constructor.

The following are the methods or events of ServiceBase class. All these events can be overridden.

1.    OnStart – This method is fired when the service is started by the Service Control Manager. This method is used to specify the processing that occurs when a Start command is received.

2.    OnStop – This method is called when the service is stopped by the Service Control Manager. This is typically used to specify the processing that occurs when a Stop command is received by the Service Control Manager.

3.    OnPause – This method is called when the service is paused and typically contains the processing to pause the service.

4.    OnContinue – This method is called when the service is resumed after being paused. This method typically contains the necessary processing so as to enable a service to return to normal functioning after the same was paused earlier.

5.    OnShutdown – This method is called when the system is being shutdown and contains the necessary processing indicative of what should happen prior to the system shutting down.

6.    OnPowerEvent – This method is used to specify the necessary processing that should take place when the power status changes.

7.    OnCustomCommand – This method is used to specify a custom command, i.e., any command other than those discussed above.

Creating and Installing a Windows Service with Visual Studio 2005

Creating and installing a windows service is explained step by step.

·         Open Visual Studio 2005

·         Select new project Visual C# - Windows in Project Types and Windows service in the installed templates and click Ok.

·         Write the code for OnStart and OnStop. Build and see that the windows service does not have any errors.

·         Run InstallUtil /i WindowsService.exe in the Visual Studio command prompt. To uninstall we can use InstallUtil /u WindowsService.exe.

·         Open Services.msc and locate the service in the list of the services. Start the service.

References
Conclusion

This article briefly examines the installation and execution of Windows Services.


Product Spotlight
Product Spotlight 

©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-08-18 9:45:28 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search