Share via


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: https://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

                                              clip_image001

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

           Imports System.Timers

           Imports System.Threading  

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(..)

           StartIt()

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))

   End Sub

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")

        End Sub

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

                                          clip_image002[1]

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.

                                           clip_image004[1]

4. For changing Service account , right click on ServiceProcessInstaller1 and change the 'Account' Property to Local System as given below

                                        clip_image006[1]

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

                                          clip_image007

3. Type 'Installutil <<Service Name>>.exe as given below

                                          clip_image008[1]

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

                                      clip_image009

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

                                      clip_image010[1]

Additional Reference

1. 317421 How to create a Setup project for a Windows Service in Visual Basic .NET or in Visual Basic 2005

     https://support.microsoft.com/default.aspx?scid=kb;EN-US;317421

2. Introduction to Windows Service Applications

      https://msdn.microsoft.com/en-us/library/aa983650(VS.71).aspx

3. 820639 The Windows Forms Timer event is not raised in a Windows service

      https://support.microsoft.com/default.aspx?scid=kb;EN-US;820639

4. 821794 Descriptions of some best practices when you create Windows Services

      https://support.microsoft.com/default.aspx?scid=kb;EN-US;821794

PrjWinService.zip

Comments

  • Anonymous
    December 31, 2008
    I was going to get around to blogging about this, eventually. I came across this read this morning which

  • Anonymous
    December 31, 2008
    Now tie this service to a WorkFlow that uses Communication Foundation...now your talking a post to blog about.

  • Anonymous
    December 31, 2008
    Another POV about using Services. //TODONT: Use a Windows Service just to run a scheduled process A common requirement in business application is a scheduled process - call a webservice, process the data, and FTP the results to a business partner, for instance. Developers kick around possible solutions - BizTalk's overkill, DTS won't handle it well, what to do? http://weblogs.asp.net/jgalloway/archive/2005/10/24/428303.aspx

  • Anonymous
    August 09, 2010
    Good sample, but I had a problem because I declared oTimerCallback As TimerCallback StartTimer As System.Threading.Timer inside the StartIt() method and they were garbaged by GC after some runs. After moving declaration in class definition everything was fine