Saturday, December 16, 2006

Fundamentals of Windows Services


Windows services enable you to perform tasks that execute as different background processes. You can use Windows services to perform tasks, such as monitoring the usage of a database. A Windows service executes in its own process space until a user stops it, or the computer is shut down.
Windows services run as background processes. These applications do not have a user interface, which makes them ideal for tasks that do not require any user interaction. You can install a Windows service on any server or computer that is running Windows 2000, Windows XP, or Windows NT. You can also specify a Windows service to run in the security context of a specific user account that is different from the logged on user account or the default computer account. For example, you can create a Windows service to monitor performance counter data and react to threshold values in a database.


You create Windows services to perform tasks, such as managing network connections and monitoring resource access and utilization. You can also use Windows services to collect and analyze system usage data and log events in the system or custom event log. You can view the list of services running on a computer at any time by opening Administrative Tools from Control Panel and then opening Services. Figure 2.1 displays the Services window.
A Windows service is installed in the registry as an executable object. The Service Control Manager (SCM) manages all the Windows services. The Service Control Manager, which is a remote procedure call (RPC) server, supports the local or remote management of a service. You can create applications that control ?Windows services through the SCM by using Visual Studio .NET. The .NET Framework provides classes that enable you to create, install, and control Windows services easily.

The Windows service architecture consists of three components:

(a) Service application.
An application that consists of one or more services that provide the desired functionality

(b) Service controller application.
An application that enables you to control the behavior of a service

(c) Service Control Manager.
A utility that enables you to control the services that are installed on a computer

You can create Windows services by using any .NET language, such as Visual C# or Visual Basic .NET. The System.ServiceProcess namespace of the .NET ?Framework contains classes that enable you to create, install, implement, and ?control Windows services. You use the methods of the ServiceBase class to create a ?Windows service. After you create a Windows service application, you install it by registering the application in the registry of a computer. You use the ?ServiceInstaller and ServiceProcessInstaller classes to install Windows services. You can view all the registered service applications in the Windows registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. After you install a service, you need to start it. You use the ServiceController class and the SCM to start, stop, pause, and continue a Windows service. The ServiceController class also allows you to execute custom commands on a service.
After you start a Service application on a computer, it can then exist in a running, paused, or stopped state. In addition, a service can be in a pending state. A pending state indicates that a command, such as the command to pause a service, was issued but not completed.

Windows services are categorized based on the number of services that run in a process space. You can have one service running in one process space or multiple services sharing a process space. The services that use a single process space are called Win32OwnProcess services, whereas the services that share a process with other services are called Win32ShareProcess services.

Differences Between Service Applications and Other Visual Studio .NET Applications

Windows services are different from other Visual Studio .NET applications. The following sections discuss these differences.

Installing Windows Services

Unlike other Visual Studio projects, you need to install a service application before the service can run. To register and install the service, you first need to add installation components to a service application.

Debugging Windows Services

You cannot debug a service application by pressing the F5 or F11 key like you can with other types of Visual Studio .NET applications. To debug a service application, you need to install and start the service, and then attach a debugger to the process of the service. You will learn to debug a service application in Lesson 5.

Executing Windows Services

In a Windows service, the Run method loads a service into the SCM. You call the Run method from the Main method of a service application. Another difference between executing a Windows service and other Visual Studio .NET applications is that the dialog boxes raised from a Windows service are not visible to the user. In addition, the error messages generated from a Windows service are logged in the event logs. You can specify that Windows services run in their own security context. Therefore, a Windows service can start before a user logs on and continue to run even after the user logs off.

Programming Model of Windows Service Applications

You create Windows service applications by using the classes of the System.ServiceProcess namespace. You use the methods of the classes in this namespace to provide functionality to your service application


No comments: