Working with Windows Services in .NET
page 5 of 8
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 42538/ 76

Implementing a Simple Windows Service Application in .NET

This section makes use of the concepts learned so far and shows how we can implement a simple Windows Service in .NET.

Creating the Windows Service

To create a new project in Visual Studio .NET 2003, select C# as the language of your choice and then select Windows Service as the project.  Specify the name of the project and save the same. Note that when implementing a Windows Service, you should have two classes, one is the Service class and the other is the Service Controller class.  Refer to the following listings that contain the source code for both the custom Service and the custom ServiceController classes.

Listing 1: The Sample Windows Service Class

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
 
namespace SampleWindowsService
{
  public class SampleWindowsService: System.ServiceProcess.ServiceBase
  {
    StreamWriter streamWriter;
    private System.ComponentModel.Container components = null;
    public SampleWindowsService()
    {
      InitializeComponent();
    }
 
    static void Main()
    {
      System.ServiceProcess.ServiceBase[]ServicesToRun;
      ServicesToRun = new System.ServiceProcess.ServiceBase[]
      {
        new SampleWindowsService()
      };
      System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
 
 
    private void InitializeComponent()
    {
      components = new System.ComponentModel.Container();
      this.ServiceName = "Sample Service";
    }
 
 
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose(disposing);
    }
 
    protected override void OnStart(string[]args)
    {
      streamWriter = new StreamWriter(new FileStream(
        "C:\\SampleWindowsServiceLogger.txt", System.IO.FileMode.Append));
      this.streamWriter.WriteLine("Starting Sample Windows Service at " +
        DateTime.Now.ToString());
      this.streamWriter.Flush();
      this.streamWriter.Close();
    }
 
    protected override void OnStop()
    {
      streamWriter = new StreamWriter(new FileStream(
        "C:\\SampleWindowsServiceLogger.txt", System.IO.FileMode.Append));
      this.streamWriter.WriteLine("Stopping Sample Windows Service at " +
        DateTime.Now.ToString());
      this.streamWriter.Flush();
      this.streamWriter.Close();
    }
  }
}

Listing 2: The Sample Windows Service Installer Class

using System;
using System.Collections;
using System.ComponentModel;
using System.ServiceProcess;
using System.Configuration.Install;
 
namespace SampleWindowsService
{
  [RunInstaller(true)]
  public class SampleWindowsServiceInstaller:
    System.Configuration.Install.Installer
  {
 
    private System.ComponentModel.Container components = null;
 
    public SampleWindowsServiceInstaller()
    {
      InitializeComponent();
      ServiceProcessInstaller spi = new ServiceProcessInstaller();
      ServiceInstaller si = new ServiceInstaller();
      si.DisplayName = "Sample Windows Service";
      si.ServiceName = "Sample Windows Service";
      si.StartType = ServiceStartMode.Automatic;
      this.Installers.Add(si);
 
      spi.Account = ServiceAccount.LocalSystem;
      spi.Username = null;
      spi.Password = null;
      this.Installers.Add(spi);
    }
 
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose(disposing);
    }
    private void InitializeComponent()
    {
      components = new System.ComponentModel.Container();
    }
  }
}

The Windows Service implemented in this article stores the time of starting, stopping or resuming the service in a file C:\\SampleWindowsServiceLogger.txt in the local file system.  Make changes to implement the service that can suit your needs.

Start, stop and resume the Windows Service

Go to Control Panel | Administrative Tools | Computer Management and select the Services and Applications tab.  Then locate your installed service and click on Start to start the service.  The rest is self explanatory.

Installing the Windows Service

In order to install the Windows Service implemented above, use the installutil .NET Framework command line utility.  Specify the following at the command prompt.

Installutil SampleWindowsServiceInstaller.exe

Un-Installing the Windows Service

In order to uninstall the Windows service, specify the following at the command prompt:

Installutil /u SampleWindowsServiceInstaller.exe

View Entire Article

User Comments

Title: Mr   
Name: Gautam Sharma
Date: 2007-01-02 5:48:04 AM
Comment:
This article is very nice.I run my first windows service and installed sucessfully.
Thanks
Title: Good article   
Name: Harinath
Date: 2006-12-20 12:50:50 AM
Comment:
I found this article really useful. thanks for the same
Title: .NETPhreak   
Name: Dhaval Patel
Date: 2006-12-07 1:51:32 AM
Comment:
It should be pointed out that the call to the OnStart() method should return back to the SCM (Service Control Manager) within 30 seconds (this value can be altered in the registry, but it is not always feasible to do so in a production environment). The best idea is to encapsulate all your functionality into a separate .dll within a method, say for example RequestStart(), that is spawned in a new thread. All that you would need then is to instantiate that .dll in OnStart() and call RequestStart() - since RequestStart() has all its functionality in a new thread, the call to OnStart() will return immediately to the SCM. Additionally, if you make this thread a background thread, every time you stop your service, the thread will also die; this way you may not need to provide any functionality in your OnStop() method.
Title: Great one...   
Name: Sandeep Acharya
Date: 2006-12-05 9:47:00 PM
Comment:
I had no hands on windows services till now and very soon I am going to start working on this for some urgent requirement. And I must say that this article will help me a lot. I could able to develope a dummy one by refering to this article.

Thanks for publishing such useful articles.
Title: Making it Easier   
Name: Ambrose
Date: 2006-12-05 3:44:22 PM
Comment:
Might want to also consider things that raise the abstraction level:
http://aspalliance.com/749_The_Perfect_Service__Part_1

Product Spotlight
Product Spotlight 





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


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