……………………………………………….Expertise in .NET Technologies

  • Categories

  • Advertisements

Introduction to Windows Service Applications

Posted by Ravi Varma Thumati on September 22, 2009

What is a Windows Services?

Windows Services formerly known as NT Services are applications that can be automatically started when the operating system boots. The core function of a Windows Service is to run an application in the background. Windows Services enable you to create long-running executable applications that run in its own Windows session, which then has the ability to start automatically when the computer boots and also can be manually paused, stopped or even restarted.

Windows Services don’t run on Windows 95, 98, or ME; the NT kernel is a requirement. Windows Services do run on Windows NT 4, Windows 2000, Windows XP, and Windows Server 2003.

 Examples of Services:

  • World Wide Publishing Service is the service of the Internet Information Server (IIS)
  • Event Log is a service to log messages to the event log system
  • Microsoft Search is a service that creates indexes of data on the disk

 To see all of the services on a system On a Windows 2000 Professional go to

Settings -> Control Panel -> Administrative Tools -> Services.

Windows Services1

You can easily create services by creating an application that is installed as a service.

You create your service as a Microsoft VS .Net Project, defining code within it that controls what commands can be send to the service and what commands should be taken when those commands are received. Commands that can be sent to a service includes starting, pausing, resuming and stopping the service, and executing custom commands.

After you create and build application, you can install in the running command line utility InstallUtil.exe and passing the path to the service’s executable file. You can then use the Services Control Manger to start, stop, pause, resume, and configure your service.

Service Applications VS others Visual Studio .Net Applications

Service applications function differently from many other VS.NET applications in several ways:

  • The complied executable file that a service application project creates must be installed on the server before project can function in a meaningful way. You cannot debug or run a service application by pressing F5 or F11. You cannot run immediately or step into its code. Instead, you must install and start your service and then attach a debugger to the service’s process.
  • You must create installations components to the service application. The installation components install and register the service on the server and create an entry point with the Windows Services Control Manger.
  • The Main method for your service application must issue the run command for the services your project contains. The Run command loads the services into the Service Control Manger on the appropriate server. If you use the windows services project templates, this method is written for you automatically.
  • Windows Service applications run in the different window station than the interactive station of the logged-on user. A window station is a securable object that is associated with a process, and contains a clipboard, an atom table, and a set of desktop objects. Because window station is not an interactive station, dialog boxes raised from within a windows service application will not be seen and may cause your program to stop responding. Similarly, error messages should be logged in the windows event log rather than raised in the user interface.

The Window Services classes supported by the .Net Framework do not support interaction with interactive station, that is, the logged-on user. The .Net framework also does not include classes that represent desktops and stations. If your window services must interactive with other stations, you need to access the unmanaged windows API.

  •  Windows service applications run in their own security context and are started before the user logs to the Windows computer on which they are installed. A service running under the system accounts have more permissions and privileges than a user account.

Service Lifetime

A service goes through several internal states in its lifetime. First, the service is installed onto the system on which it will run. This process executes the installers for the service project and loads the service into the Services Control Manager for that computer. The Services Control Manager is the central utility provided by Windows to administer services.

After the service has been loaded, it must be started. Starting the service allows it to begin functioning. You can start a service from the Services Control Manager, from Server Explorer, or from code by calling the ServiceController.Start method. The Start method passes processing to the application’s OnStart method and processes any code you defined there.

A running service can exist in this state indefinitely until it is either stopped or paused or until the computer shuts down. A service can exist in one of three basic states: Running, Paused, or Stopped. The service can also report the state of a pending command: ContinuePending, PausePending, StartPending, or StopPending. These statuses indicate that a command has been issued, such as a command to pause a running service, but has not been carried out yet. You can query the ServiceController.Status property to determine what state a service is in, or use the ServiceController.WaitForStatus method to carry out an action when any of these states occurs.       

You can pause, stop, or resume a service from the Services Control Manager, from Server Explorer, or by calling methods in code. Each of these actions can call an associated procedure in the service (OnStop, OnPause, or OnContinue), in which you can define additional processing to be performed when the service changes state.

Types of Services

Two types of services can create in Visual Studio .Net:

  1. Win32OwnProcess: Services that are the only one service in a process.
  2. Win32ShareProcess: Services that share a process with another service.

You can retrieve the service type by querying the ServiceController.serviceType property

ServiceController Component

      You can use the ServiceController component to interact with the Windows services on your local computer or on any computer to which you have access. The ServiceController component allows you to perform administrative tasks such as starting, stopping, and pausing services; to retrieve lists of available services; and to call custom commands on the service.

Deploying and Installing Services

Visual Studio .NET ships installation components that can install resources associated with your service applications. Installation components register an individual service on the system to which it is being installed and let the Services Control Manager know that the service exists.

After you add installers to your application, the next step is to create a setup project that will install the compiled project files and run the installers needed to install your service. To create a complete setup project, you must add the service project’s output to the setup project and then add a custom action to have your service installed.


  • Services must be created in a Windows Service Application project or another.     NET-enabled project that will create an .exe file when built and inherits from the ServiceBase Class.
  • Projects containing Windows services must have installation components for the project and its services. This can be easily accomplished from the Properties window.


Windows Services Architecture

            Three program types are necessary to operate a Windows Service:

  • A service program
  •  A service control program
  • A service configuration program

The service program itself provides the actual functionality. With a service control program, it’s possible to send control requests to a service, such as start, stop, pause, and continue. With a service configuration program, a service can be installed; it’s copied to the file system, written into the registry, and configured as a service.

Service Program:

The service program implements the functionality of the services. It needs three parts:

  • A main function
  • A service-main function
  • A handler

Service Control Manager (SCM):

The SCM plays an important role for services, sending requests to our service to start and to stop it. The SCM is the part of the OS that communicates with the service.

Fig illustrates how this communication works with a UML sequence diagram.

windows Services2

At boot time, each process for which a service is set to start automatically is started, and so the main function of this process gets called. The service has the responsibility to register the service-main function for each of its services. The main function is the entry point of the service program, and in here, the entry points for the service-main functions must be registered with the SCM.

   Main function, Service-main, and handlers:

         The main function of the service might register more than one service-main function. The service must register a service main function for each service it provides. A service program can provide a lot of services in a single program.

         The SCM now calls the service-main function for each service that should be started. The service-main function contains actual functionality of the service. One important task of the service main function is to register a handler with the SCM.

         The handler must respond to events from the SCM. Services can be stopped, suspended, and resumed, and the handler must react to these events.

Once the handler has been registered with the SCM, the service control program can post requests to the SCM to stop, suspend, and resume the service. The service control program is independent of the SCM and the service itself. We can write our own service control program; a good example of this is the SQL Server Service Manager shown below.

windows Services3

Service Control Program:

                     With a service control program we can control the service. For stopping, suspending, and resuming the service, you can send control codes to the service, and the handler should react to these events.

Service Configuration Program:

With a service configuration program, a service can be installed; it’s copied to the file system, written into the registry, and configured as a service. The installation program can use the service configuration program to configure the service, but this program can also be used at a later time to change service configuration parameters.

System.ServiceProcess Namespace

In .NET Framework, you can find service classes in the System.ServiceProcess namespace that implement the three parts of a service:

  • You have to inherit from the ServiceBase class to implement a service. The ServiceBase class is used to register the service and to answer start and stop request.
  • The ServiceController class is used to implement a service control program. With this class you can send requests to services.
  • The ServiceProcessInstaller and ServiceInstaller classes are, as their names suggest, classes to install and configure service programs.


[Visual Basic]
Public Class ServiceBase
   Inherits Component
public class ServiceBase : Component




Defining the Service Behavior’s

            In service class, you override base class function that determines what happens when the state of your service is changed in the Service Control Manager. The ServiceBase class exposes the following methods, which you can override to add custom behavior.


Override to 

OnStart Indicate what actions should be taken when your service starts running. You must write code in this procedure for your service to perform useful work.
OnPause Indicate what should happen when your service is paused.
OnStop Indicate what should happen when your service stops running.
OnContinue Indicate what should happen when your service resumes normal functioning after being paused.
OnShutDown Indicate what should happen just prior to your system shutting down, if your service is running at that time.
OnCustomCommand Indicate what should happen when your service receives a custom command.
OnPowerEvent Indicate how the service should respond when a power management event is received, such as a low battery or suspended operation.


            OnCustomCommand lets you specify additional functionality beyond starting, stopping, pausing and continuing services. The SCM does not examine the custom command to verify whether the service supports the command parameter passed in. It passes the custom command directly to the service. If the service does not recognize the command parameter, it does nothing. Custom commands are raised by an ExecuteCommand statement in a ServiceController component. Use a switch statement or if..then condition to handle the custom commands you define on your service. The only values for a custom command that you can define in your application or use in OnCustomCommand are those between 128 and 256. Integers below 128 correspond to system-reserved values.


            Use OnPowerEvent to specify the processing that occurs when the system event indicated in the PowerBroadcastStatus enumeration occurs–for example, when the computer is placed in suspended mode or indicates low battery power. OnPowerEvent is expected to be overridden when the CanHandlePowerEvent property is true.

A program, routine, or process that performs a specific system function to support other programs, particularly at a low (close to the hardware) level. When services are provided over a network, they can be published in Active Directory, facilitating service-centric administration and usage. Some examples of Windows 2000 services are Security Accounts Manager service, File Replication Service, and Routing and Remote Access Service.


  1. The Problems of Interactive Services
  2. Event Logging
  3. Performance Monitoring



2 Responses to “Introduction to Windows Service Applications”

  1. Thank you for another great post. Where else may anyone get that type of information in such an ideal means of writing? I have a presentation next week, and I’m at the look for such information.

  2. I have to confess that i typically get bored to read the entire thing however i think you’ll be able to add some value. Bravo !

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: