How to Create Managed Windows Service
Microsoft Windows service, formerly known as NT service, enables you to create a long-running executable application that runs in a separate windows session or we can configure theservice to run in a user context other than alogged on user. Users can make windows services to run automatically without any user interaction. These features make windows service a powerful application which canbe hosted on servers and remote locations where minimum user interaction is required.
Prior to .NET environment, creating windows services was a tedious job. Earlier , windows services were created using unmanaged application like VC++ 6.0 or VB 6.0 . However after .NET, creating windows service is very easy and it hardly requires any technical knowledge. My article provides simple, easy to followinstructions on creating a simple windows service in .NET platform, which activates based on a timer value.
To understand more about Microsoft windows service , please go through the following msdn article, Ref: http://msdn.microsoft.com/en-us/library/d56de412(VS.80).aspx , which goes into more detailabout windows service. Also , at the bottom of this article , I am providing a couple of links which explainmore about windows service.
Creating a Windows Service which activates based on a timer.
I am planning to cover thefollowing details in this article.
1. Creating Windows Service Project Using VS Template and setting timer value.
2. Creating an Installer
3. Deploying Service.
4. Verifying whether service works
5. Un-installing service.
6. Additional Reference
I Creating Windows Service Project Using VS Template
Please follow the steps as given below to create a windows service.
1. Open Visual Studio 2005 IDE
2. On the File menu -> New, and then Project
3. Select Windows Service Template as shown in screen shot
4. Open 'Service1.vb' file and click 'click here to switch to code view'
5. Add following namespace at top of the Service1.vb file
6. Add following definition just below the class definition.
Dim oTimerCallback As TimerCallback
Dim StartTimer As System.Threading.Timer
7. Add following code under sub 'Protected Overrides Sub OnStart(..)
8. Create a new method call 'StartIt()' and add following code inside it.
Private Sub StartIt()
oTimerCallback = New TimerCallback(AddressOf TestCall)
StartTimer = New System.Threading.Timer(oTimerCallback, Nothing, 0, CType(60 * 1000, Long))
9. Create a new method 'TestCall' as given below in Service1.vb file.
Private Sub TestCall(ByVal state As Object)
'add code for required operation.
'This code will execute in every 60 second.
EventLog.WriteEntry("inside TestCall VS 2005")
II Creating an Installer
1. Open service1.vb file inside designer windows
2. Right click any were in Service1.vb file and select 'Add Installer' as given below
3. Once we select, 'Add Installer', Visual Studio IDE will create an installer with the default name 'ProjectInstaller.vb' . When we open ProjectInstaller.vb inside the designer window, we could see couple of controls like ServiceProcessInstaller1and ServiceInstaller1. By selecting properties for 'ServiceInstaller1' we could see an option 'StartType' . Were we could change the service startup type by selecting one of the given option as given below.
4. For changing Service account , right click on ServiceProcessInstaller1 and change the 'Account' Property to Local System as given below
5. After creating the installer we can compile the application to create a Windows Service EXE.
III Deploying Service.
For deploying service , either we can create a setup project (.msi) or we can use command line tool 'Installutil' which shipped with .NET Framework SDK. In this article , I am describing how to deploy our service using 'InstallUtil'. Please follow the steps as given below to deploy windows service using installutil.exe
1. Open Visual Studio Command prompt from 'Start -> Programs->Microsoft Visual Studio 2005 ->Visual Studio Tools -> Visual Studio 2005 Command Prompt'
2. Navigate to the folder where our 'Windows Service' application is compiled. Please find the screen shot
3. Type 'Installutil <<Service Name>>.exe as given below
Note: If we select the 'Account' type as 'User', application would prompt for username and password in which context service need to run.
IV Verifying whether service works
To verify whether our service works, follow the steps as given below.
1. Click 'Start ->Control Panel -> Administrative Tools' , and then click Services .
2. Select our installed service [by default service name should be 'Service1' as given below
3. Right-click on Service1 and then click start
4. To verify that an event is logged in the event log [Start->Control Panel -> Administrative Tools and then click 'Event Viewer'. In the left pane, click 'Application Log', in the right pane locate the event log for our service and we could see below event information.
"inside TestCall VS 2005"
V Un-installing service.
For un-installing service, enter following command inside Visual Studio command prompt by navigating to the folder where our .exe is created , ' Installutil <<Service Name>>.exe /u' as given below
1. 317421 How to create a Setup project for a Windows Service in Visual Basic .NET or in Visual Basic 2005
2. Introduction to Windows Service Applications
3. 820639 The Windows Forms Timer event is not raised in a Windows service
4. 821794 Descriptions of some best practices when you create Windows Services