Windows Services in .NET
 
Published: 18 May 2004
Unedited - Community Contributed
Abstract
Ever wondered how to keep your program up and running as soon as the system is booted? Windows services are processes that run in the background and independent of whether a user has logged in or not. This article explains what exactly a windows service is, why we need one and how it is built in the Microsoft .NET platform.
by Kavitha Pradeep
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 58649/ 67

Overview of .NET

The .NET framework is an integrated platform helping you to build varied category of applications. Today applications built using this are mostly web applications, windows applications, web service applications or components for the business tier. .NET extends support to also write windows service applications. The framework is rich in it class libraries. The block architecture of the .NET framework is as shown in fig 1.0

 

 

                                                      Fig 1.0

All .NET applications use the framework class library to develop applications and it relies on the CLR (Common Language Runtime) for execution .The CLR communicates with the host operating system for native code generation and execution .Thus .NET applications are sealed from running directly on the operating system. Applications are first compiled to get an assembly and then are JIT compiled to native form. Let us now look into what are windows services and why they are needed

Windows Services Defined

Every time a user logs on the NT machine, a desktop is immediately assigned to him. Any process or thread started by the user is attached to this desktop. Multiple desktops can exist, but only one desktop can be active at a time. These desktops each have threads assigned to them. Windows can be created in each of these threads, but only those windows in threads assigned to the active desktop are visible.

 

Windows services are processes that exist without any desktops and run in the background (similar to daemon processes in UNIX). They can be started before any user logs into the system. After the operating system is initially loaded, it immediately begins loading the services that are configured to automatically start. The user can log on to the system before all automatic services are loaded, but the user is not presented with a desktop until all the automatic services have finished loading. After logging on, the user can start and stop the services. When the user logs off, the desktop is closed and all the processes assigned to his desktop are also closed. But any services that exist when the user logs off are not closed and remain alive and running. They are often loaded at boot up, before any user logs in, and are often independent of any specific user logged on at the time. In fact, a service has its own login session. A service can also be launched manually by a user. Although it’s possible, Windows Service typically has no user interface.

The Need for Windows Services

The fact that services can exist without desktops is the advantage that makes services desirable. The various situations during which a process is installed as a service are:

·        When a process is needed that starts when the computer starts and stops only when computer stops

·        Hardware drivers, or applications controlling or monitoring hardware attached to a system; e.g. monitoring for plug and play devices

·        A process is required that operates even when no users are logged on to the computer; e.g. Anti Virus programs, Disk Monitoring programs etc.

·        Parts of Operating System itself; e.g. RPC (Remote Procedure Call) server, DCOM (Distributed Component Object Model) server, etc.

·        Software which handles communication from users on other machines, who have logged on to this machine; e.g. FTP Server, Web Server etc.

 

Service Control Manager (SCM)

Due to the fact that services have no desktops, they usually do not interact with the users. But there has to be a way for controlling and managing these services. Service Control Manager does just that.

The SCM, a part of the windows kernel, is the one that controls the lifetime of the service. Any process that needs to be started and stopped like a service has to register itself with the SCM. The SCM then takes care of when to launch the service and when to stop it. The SCM holds a list of all the installed services, when they should be started, whether they should be allowed to interact with the desktop, etc.

 

The administrator can control services by sending control requests to them (e.g. "start service", "stop service", etc.) via the SCM. This is done using the Services applet of the control panel; double-clicking on the icon brings up the Services window, which lists all the services in the SCM database. Another utility to view the services running is WinMsd.exe. Combining the functionalities available in these two utilities, with an additional capability to control and configure the services on remote computers is Service Controller (SC.exe). This utility is not pre-installed with windows and has to be downloaded to every computer that needs it. All these utilities use the SCM to communicate with the services.

 

The SCM maintains its database of services installed in the system in the registry. Each service has an entry in the following key:

     

  HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<Service Name>

 

Here, the Service Name is the name of the service used by the SCM. The service name seen in the Services applet is different from this one. This is a unique name used internally by the SCM. The corresponding name of each service which is displayed in the applet is stored in the key Display Name inside the Service Name key.

 

Each service can run by logging on as either a Local System or particular user account. Also the services can be enabled or disabled for specific hardware profile. Each service could either run independently or it will depend on some other service(s). The list of services each service depends on will be maintained by the SCM. All these information could be seen from the Services applet by looking at the properties of each service. Other than the list of services a particular service depends on, a list of services that depend on this service can also be seen.

 

When a service fails, the SCM can decide what action to take. These can be specified directly through the Service applet. The possible actions are:

·        Restart the service

·        Run a particular file

·        Reboot the computer

·        Take no action

By default the response for a failure is to take no action. If the service fails for the second time too, one of these actions can be set. For subsequent failures, another action could be assigned.

 

Building a service is a little complicated in the sense that a certain steps have to be followed to successfully build and install a service. Prior to .NET, the only efficient way to build a .NET was by building it in C++/ VC++. It was complicated and had various issues to be considered while doing it. But .NET has changed the way the services were build. With its Framework Base Classes available as a part of the .NET Framework, building services were made easy. By taking advantage of the object oriented capability of .NET, various tasks needed to build a service has been greatly simplified by encapsulating them inside various classes and including them as a part of the .NET Framework Base Classes. These base classes in the .NET Framework take care of the system-level interfacing.

.NET Approach to Windows Services

When .NET is used to create services, the steps involved in doing it are reduced. Much of the functionalities and system level interactions are wrapped inside classes and available as a part of the Framework Base Classes. Any .NET language can be used to create Windows Services. Two base classes needed for creating the Windows Services are

  • System.ServiceProcess.ServiceBase
  • System.Configuration.Install.Installer

 

System.ServiceProcess.ServiceBase provides the base class for the service itself. The class that inherits from this class contains the logic that runs in the service. System.Configuration.Install.Installer provides the interface necessary to get the service installed under Windows 2000 and NT. It isn't typically necessary to put any logic in the installer class. There can be more than one service in a project. The installer class creates one ServiceProcessInstaller object (which knows how to install the service with the SCM), and a ServiceInstaller object. The ServiceInstaller object writes information to the registry which is needed for installation. A subkey is added under

 

   HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\

 

In VB.NET, any project created as a service will automatically create a class that inherits from the System.ServiceProcess.ServiceBase class. But the installer class has to be included manually. A Windows Service has a special installation procedure that handles all the operating-system communications necessary to execute a service. A special command-line program in .NET named InstallUtil.exe installs and uninstalls Windows Services produced in .NET.

In VB.NET, the steps that are followed to develop a Windows Service are:

1.      Create Windows Service project in Visual Basic.NET

2.      Add installer to project

3.      Add/change logic and build the executable for the service

4.      Install the service using InstallUtil.exe

5.      Start the service using the Service applet in the Control Panel

 

The same applet can be used to stop the service. The InstallUtil.exe can be used again to uninstall the service. This is done by passing the ‘/u’ attribute.

Building Windows Services

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.

                                                            Fig 2.0

                                                            Fig 2.1

Installing Windows Services

Once the steps needed to build the service is over, it should be installed. Following are the steps to do it.

1.      Save the project.

2.      Build the service’s executable file. Build | Build menu item.

3.      Open the Visual Studio .NET Command Prompt item from the start menu. It will be installed as a subitem of the Visual Studio .NET Tools item. This shortcut sets all the path necessary before going to the DOS prompt. Change the folder to the location where the executable is located. It will be found in the bin folder within the folder where the project is stored.

4.      Install the service. This can be done by using the InstallUtil tool that comes with the Visual Studio. Use the following command line:

InstallUtil Service1.exe

5.      Start the service. This can be done by running the Windows Service Manager. In the services list, the service now installed will be listed as Service1. Right click and start the service.

6.      Now, any Startup events can be viewed by opening the Event Viewer. Select the Application log, and any entries send to the log can be seen there.

 

Removing and Debugging Windows Services

Removing

Removing the service can be done by running the same InstallUtil tool but with a /u parameter. This will uninstall the service. The entry in the Windows Service Manager will be removed.

Debugging

Because a service has to be compiled and installed before running it, debugging becomes a little more complex than the normal applications. To debug a service, we have to run it and debug the running process. The steps to follow:

1.      Install the service and run it.

2.      In Visual Studio, select Debug | Processes menu item. Processes dialog box is displayed.

3.      Select the Show system processes check box to include the running services.

4.      Select the Service1 entry and click Attach. Click OK to accept. Close Processes dialog box.

5.      Set the breakpoint in the code at the location to be tested.

6.      Trigger the breakpoint by taking necessary action. For example, if the breakpoint is in the OnStop procedure, stop the processes to invoke it. Now single stepping can be done. To continue F5 should be pressed.

7.      Then after the debugging is finished, detach the process by using the Debug | Processes menu. Select the service and click Detach.

Use the Debug | Stop Debugging menu item to stop debugging session.

Example

As a simple example, the following code can be used to test a service. Once the steps are followed to create a service, procedures for handling the start and stop event of the service is automatically created. These events can be tested by putting a log entry. The code might look like this:

For C# project:

 

protected override void OnStart(string[] args)

{

    EventLog.WriteEntry( String.Format("Process Starting…") );
}

 

For Visual Basic project:

 

Protected Overrides Sub OnStart(ByVal args() As String)

 

    EventLog.WriteEntry( String.Format("Process Starting…"))

 

End Sub

 

The  logged entry can be viewed from the Event Viewer applet.

Various Classes Available in .NET to Deal with Windows Services

The System.ServiceProcess namespace provides classes that allow you to implement, install, and control Windows service applications. The various classes available within this namespace are:

Classes

Class

Description

ServiceBase

Provides a base class for a service that will exist as part of a service application. ServiceBase must be derived when creating a new service class.

ServiceController

Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.

ServiceControllerPermission

Allows control of code access security permissions for service controllers.

ServiceControllerPermissionAttribute

Allows declarative service controller permission checks.

ServiceControllerPermissionEntry

Defines the smallest unit of a code access security permission that is set for a ServiceController.

ServiceControllerPermissionEntryCollection

Contains a strongly-typed collection of ServiceControllerPermissionEntry objects.

ServiceInstaller

Installs a class that extends ServiceBase to implement a service. This class is called by the install utility when installing a service application.

ServiceProcessDescriptionAttribute

Specifies a description for a property or event.

ServiceProcessInstaller

Installs an executable containing classes that extend ServiceBase. This class is called by installation utilities, such as InstallUtil.exe, when installing a service application.

TimeoutException

The exception that is thrown when a specified timeout has expired.

Enumerations

Enumeration

Description

PowerBroadcastStatus

Indicates the system's power status.

ServiceAccount

Specifies a service's security context, which defines its logon type.

ServiceControllerPermissionAccess

Defines access levels used by ServiceController permission classes.

ServiceControllerStatus

Indicates the current state of the service.

ServiceStartMode

Indicates the start mode of the service.

ServiceType

Represents the type of the service.

Conclusion

By taking advantage of the Framework Base Classes, the complexity needed to create a service is greatly reduced. This lets the developer to concentrate more on the functionality of the service instead of spending time in creating it.



User Comments

Title: Windows Services   
Name: S.B.krishna Reddy
Date: 2010-12-31 4:29:52 AM
Comment:
The images u have presented in the web page are not proper and not showing the clear view
Title: Windows Service in .NET 2   
Name: sbanik
Date: 2009-10-14 6:24:58 AM
Comment:
\
Title: Windows Services in .NET   
Name: Nanthakumar
Date: 2008-10-16 6:53:15 AM
Comment:
Hi

This is Very Useful my project..

thanks
Title: Extremely helpful, even after 4 yrs~   
Name: Rajeev
Date: 2008-06-12 4:40:13 AM
Comment:
Thanks a lot for this article. It's been 4 yrs since it was authored, but yet it is so useful for beginners.

Thanks a ton.
Title: Learned Windows services   
Name: Debasmit Samal
Date: 2008-01-23 1:16:47 AM
Comment:
Hi

Thanks a lot and lot for the article.
Really i got basic concepts for the windows services.

Thanks again for such a nice article.
Title: Windows Service   
Name: Rohini S
Date: 2008-01-22 3:28:06 AM
Comment:
It is an excellent tutorial. It covers basics of Windows service. It is a good for beginners
Title: Crystal report autogeneration using windows services   
Name: Rajesh m
Date: 2007-08-09 9:03:43 AM
Comment:
Hi All,
I developed windows service which will daily at 12:00pm generate crystalreports and mail is sent to the mail with crystal report attached. Here my problem is it works fine in my system..but when i put in testing server it is not working...
Title: windows service   
Name: pawar megha p.
Date: 2007-05-03 5:00:26 AM
Comment:
Hellow!
very good article for windows services.i have problem for writting the Installutil.exe in command propmt of vs.it gives no file found,so what may be the problem?


thanks..
Title: Windows Service   
Name: M.Mansoor AliKhan
Date: 2006-11-13 2:32:37 AM
Comment:
This is really a knowledge providing article.

Thanks ..
Title: Running Windows Service from Web Service   
Name: Shantanu Karmakar
Date: 2006-08-18 6:19:41 AM
Comment:
Hi,
I'm having a problem to start and stop a windows service from my web service. It's giving an exception InvalidIOException - Couldn't strat service. Can any one help in this regard? I think the problem is with the security.
Title: How do we programmatically configure a Windows Service to interact with the desktop?   
Name: Shashi Kumar
Date: 2006-07-29 9:15:19 AM
Comment:
Hello,
Can u please let me know how do I make the windows service interact with the desktop since the windows service has to invoke a windows application upon its successful start?
Please let me know where exactly a property called "AllowServiceToInteractWithDesktop" exists and how do I use it?

I have been struck for quite sometime now... and any help would be greatly appreciated.

Feel free to mail me at shashi.satya@gmail.com

Thanks in advance.
Title: Webservice   
Name: Mahendra
Date: 2006-07-19 1:58:51 AM
Comment:
this is really a knowledge providing article.
thanks
Title: User Interface   
Name: Uli
Date: 2006-06-20 8:14:30 PM
Comment:
"Although it’s possible, Windows Service typically has no user interface."

Hi, great article, how can I find more information about creating a windows service with windows interface, so far everytime that my windows service timer goes off and executes some code, all the functionality will work as needed but because it won't show the form that I developed (This is actually my biggest problem), it will limit my application's functionality.

Any ideas???

If you guys have something please let me know: ulisesr@mvrmc.org

THANKS!
Title: windows services   
Name: shan
Date: 2006-05-08 10:45:38 AM
Comment:
hi
this explaination is good but is this possible to install the windows services without the use of installutl i mean calling just the function install() , i tried it but its giving "no object reference " problem i think the problem is with the hashtable may be its not having the proper context?
i need ur help
Title: Windows Service Behaviour   
Name: Deepak
Date: 2006-04-27 7:28:58 AM
Comment:
Hello, I have problem with Windows Service that I have created in VB.Net its behaviour is really strange i.e.Sometimes after Istallation and Start it runs but sometimes it just stucks somewhere.Then again this service needs to be re-installed(not just restart) then it runs perfectly very well.
This service does some Asynchronous task using delegates.
I could not understood this behaviour,let me know how can I understand it or what could be reasons.

--Deepak.
Title: windows services   
Name: shravan
Date: 2006-04-03 4:24:51 AM
Comment:
this is ok
now whether this is possible to install this service without installutil.exe i mean just adding entries in the registry by urself
well i am a newbie






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-04-24 11:14:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search