Articles → .NET WINDOWS SERVICE → Basic Example Of Windows Service In .Net

Basic Example Of Windows Service In .Net






Objective Of This Tutorial













Steps Involved




  1. Create a window service
  2. Modify OnStart() method of windows service(if required)
  3. Add the Service Installer to the windows service
  4. Use ‘installutil.exe’ in the command line to install the service.



Step 1: Create A Window Service




  1. First of all open visual studio and create a new project (New – Project – select ‘Windows Service’)
  2. A windows service application is created as shown below
Picture showing the code in visual studio for windows service application
Click to Enlarge


Step 2: Modify Onstart() Method Of Windows Service(If Required)






  1. Dispose - clean up any managed and unmanaged resources
  2. OnStart - control the service startup
  3. OnStop - control the service stoppage

Step 3: Add The Service Installer To The Windows Service




  1. Right click on ‘Service.cs’, click on ‘View Designer’ to open the designer view.
  2. In designer right click to open context menu.
  3. Now click on ‘Add Installer’.
  4. ‘ProjectInstaller.cs’ is added in solution explorer.
Picture showing how to add project installer in windows service application
Click to Enlarge




Step 4: Use ‘Installutil.Exe’ On The Command Line To Install The Service




  1. Open the command prompt of Visual Studio.
  2. Go to the directory where the ‘WindowsService1.exe’ is located. In this case it is ‘bin/debug’ folder of the project.
  3. Run ‘Installutil WindowsService1.exe’ command to install the service.
Picture showing the command line window for installing windows service using the installutil command
Click to Enlarge



  1. Go to ‘Start’.
  2. Click on ‘Run’(or you can use windows + R shortcut)
  3. Type ‘services.msc’
  4. Click ‘OK'.


Picture showing the window service inside the services.msc
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, February 15, 2010
 
Updated On  -  Wednesday, February 26, 2014

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250