Click here to Skip to main content
13,552,926 members
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 16 Feb 2004
Licenced CPOL

Developing a Windows SERVICE Application using .NET Framework with C#.

, 16 Feb 2004
Rate this:
Please Sign up or sign in to vote.
An Introduction to Windows SERVICE Application using .NET Framework
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Add the rest of your HTML here -->


This article will take you through the step-by-step procedure to develop a SERVICE to run windows based Operating System. Developing a SERVICE based application in .NET Framework is pretty straightforward which it does by confining to very few classes. Before we could move on to the development part of the application a bit of theory on Service application should be of use.<?xml:namespace prefix = o /><o:p>

          Windows Services is previously called as NT Service. The Idea of creating a windows service application is two fold one is to create a long running application and the other is service applications are the application that run directly in the windows session itself (This can be verified by accessing the current or home directory of the service application which default to .winnt\system32). One more advantage of the Windows Service Application, which makes it more useful, compared to other application is that, Service application can be made to run in the security context of a specific user account.

<o:p>There are basically two types of Services that can be created in .NET Framework. Services that are only service in a process are assigned the type Win32OwnProcess.Services that share a process with another service are assigned the type Win32ShareProcess.The type of the service can be queried. There are other types of services which are occasionally used they are mostly for hardware,Kernel,FileSystem.

          Lifecycle of the Service Application. In a typical application the Main method is the entry point and for what ever instantiation has to happen will happen here matters. In Case of Service, the Main method remains to the entry module by the actual functionality of the service application start only in the Main method by  triggering a overridden method called OnStart() which actually starts the SERVICE. Prior to OnStart() method another significant variance is compared to the other application is service should be  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 following which the main and Onstart is followed to fall in line.

            A Service application can be in any one of the states listed they are viz., Running, Paused or Stopped. The Services also has the capability to queue the commands and also remain in those states. Such statuses can be queried, some of the significant states are like Continue Pending, which means  continue command is in queue and yet to get executed.Similarily StartPending,StopPending,PausePending.

        Window SERVICE application development can be divided to two phases. One is the development of Service functionality and the last phases is about the development. The 3 Main classes involved in Service development are

  •        System.ServiceProcess.ServiceBase 

  •        System.ServiceProcess.ServiceProcessInstaller

  •        ServiceController

System.ServiceProcess.ServiceBase  ,is the class in which we override the methods for implementation ,Some the method which gives us the behaviour of this are as follows.

MethodOverride to
OnStartIndicate what actions should be taken when your service starts running. You must write code in this procedure for your service to perform useful work.
OnPauseIndicate what should happen when your service is paused.
OnStopIndicate what should happen when your service stops running.
OnContinueIndicate what should happen when your service resumes normal functioning after being paused.
OnShutDownIndicate what should happen just prior to your system shutting down, if your service is running at that time.
OnCustomCommandIndicate what should happen when your service receives a custom command. For more information on custom commands, see MSDN online.
OnPowerEventIndicate how the service should respond when a power management event is received, such as a low battery or suspended operation.
Run The main entry point for the service, u sally in the Main method
[Src :MSDN]

Some Of the properties of Interest are as follows






Structure Of a Service Program

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.Configuration.Install;

namespace WindowsService


/// <summary>

/// This is the class for my Service

/// </summary>

public class MyService : System.ServiceProcess.ServiceBase


public MyService()



// TODO: Add any further initialization code


private void InitializeComponents()


this.ServiceName = "MyService";



/// <summary>

/// This method starts the service.

/// </summary>

public static void Main()


System.ServiceProcess.ServiceBase.Run(new System.ServiceProcess.ServiceBase[] {

new MyService() // To run more than one service you have to add them here



/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose(bool disposing)


// TODO: Add cleanup code here (if required)



/// <summary>

/// Start this service.

/// </summary>

protected override void OnStart(string[] args)


// TODO: Add start code here (if required)

// to start your service.



/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()


// TODO: Add tear-down code here (if required)

// to stop your service.





public class ProjectInstaller : Installer


public ProjectInstaller()


ServiceProcessInstaller spi = new ServiceProcessInstaller();

spi.Account = ServiceAccount.LocalSystem;


ServiceInstaller si = new ServiceInstaller();

si.ServiceName = "Hello Service Template";

si.StartType = ServiceStartMode.Automatic;

Installers.AddRange(new Installer[] {spi, si});



[Note: This Code is generated using Sharp Develop Version 0.98,Build 1.1]


Service Installation Procedure:

To install your service manually

  1. Access the directory in which your project's compiled executable file is located.
  2. Run InstallUtil.exe from the command line with your project's output as a parameter. Enter the following code on the command line:
    <FONT face=Verdana size=2><SNIPPET>
    :- installutil myservices.exe</SNIPPET>

To uninstall your service manually

  • Run InstallUtil.exe from the command line with your project's output as a parameter. Enter the following code on the command line:
    <FONT face=Verdana size=2><SNIPPET>
    :- installutil /u myservices.exe</FONT>
    <FONT face=Verdana size=2>[Note: If you are using any messagebox,After Installing the service navigate to Log On Tab under properties  and Select the </FONT>
    <FONT face=Verdana size=2>"Allow service to interact with Desktop"].</FONT>







This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Malaysia Malaysia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
VVV525227-Feb-12 8:09
memberVVV525227-Feb-12 8:09 
GeneralMy vote of 3 Pin
Eason_Wu22-Nov-11 17:39
memberEason_Wu22-Nov-11 17:39 
GeneralMy vote of 1 Pin
Anton Lesnichenko15-Apr-10 22:06
memberAnton Lesnichenko15-Apr-10 22:06 
much worse than msdn example
Generalmake protected service Pin
ebi_sd18-Nov-09 2:15
memberebi_sd18-Nov-09 2:15 
GeneralSuggestion for improvment Pin
vincentli27-Jul-09 4:36
membervincentli27-Jul-09 4:36 
Questiondetect the computer shut down Pin
BMWABCD2-Jul-07 19:07
memberBMWABCD2-Jul-07 19:07 
GeneralNeeds more stuff Pin
spannring22-Jan-07 18:30
memberspannring22-Jan-07 18:30 
GeneralNice Article Pin
SathishNellai27-Sep-06 11:18
memberSathishNellai27-Sep-06 11:18 
GeneralService Description Pin
Komil18-Aug-05 5:14
memberKomil18-Aug-05 5:14 
Generalrediffmail.ccurrent user name Pin
sudip1gmail2-Jun-05 20:02
susssudip1gmail2-Jun-05 20:02 
GeneralNot new Pin
Juicy16-Feb-04 23:52
memberJuicy16-Feb-04 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180515.1 | Last Updated 17 Feb 2004
Article Copyright 2004 by G.Venkatasubramaniam
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid