Thursday, September 10, 2009

How to create a windows service in dot net framework

Windows service is an application which always runs in background. We can view all the installed windows services in ControlPanel/Administrativetools/Services. Sql server, Oracle, ... are the good examples of windows service.

Using visual studio it is very easy to create windows service. Please follow the below steps to create a windows service.
  1. Open visual studio and create a new project of template "Windows Service" as shown below
  2. After you click OK in the above step you will see a new solution named "WindowsService1" created as shown below.
  3. Right click on the above screen and click "Add Installer". This will create an installer component which helps us in installing this service.
  4. You can notice a new component "ProjectInstaller.cs" added and opened as shown below. It has 2 controls "ServiceProcessInstaller1" and "ServiceInstaller1".
  5. You can set whether this service is manual, automatic, or disabled by changing the StartType property of "ServiceInstaller1" (Right click -> Properties).
  6. Change the "ServiceProcessInstaller1"'s property "Account" to "LocalSystem"
  7. Right click on Service1.cs in solution explorer and click "View Code"
  8. In code you will see the service event handlers "OnStart", "OnStop".
  9. Write code that initiates a thread in OnStart event handler.
  10. Write code that stops execution of the thread in OnStop event handler.
  11. Please see below for sample code
  12. Build the solution. You have the windows service built ready.
  13. But until we install this service we cannot see it is running or not.
  14. To install this service you need to open the Visual Studio 2005/2008 Command prompt in Start->Visual Studio 2005/2008->Visual Studio Tools
  15. Run the command : InstallUtil.exe Windows_Service_EXE_Full_Path
  16. Here Windows_Service_EXE_Full_Path is the exe generated for the WindowsService1 project. Let's say the full path of the exe is C:/WindowsService1/Bin/Debug/WindowsService1.exe then our command to install it would be like this.
  17. InstallUtil.exe C:/WindowsService1/Bin/Debug/WindowsService1.exe
  18. If the above command is successful then you will notice a new service named "Service1" is shown in Control Panel->Administrative Tools/Services
  19. To uninstall a service, InstallUtil.exe -u C:/WindowsService1/Bin/Debug/WindowsService1.exe
  20. To make the life easier to install/uninstall windows services we can create a setup project which will let people easily install/uninstall with a wizard Please read this link.

kick it on
Shout it


Kiran said...

Your artikel is helpfull. But I'm not familiar with C#. I try it in vb.
In vb service class is so simple, there is no inherits class and sub New().

Is it ok to apply you code directly on OnStart method?

Kiran said...

Yes. Putting my code in OnStart method works in VB also. Please try it and let me know f there are any issues.

Dinesh Pulugundla said...

This article is very nice with step wise. It would be great if you would also add steps for how to debug the Service.