Tuesday, January 13, 2009

How to create Windows Service Using C#

To create a new Window Service, pick Windows Service option from your Visual C# Projects, give your service a name, and click OK.
This action will add WinService1.cs class to your project.
Set your ServiceName to your own name so it would be easier to recognize your service during testing OR you can set this property programmatically using this line this.ServiceName = "MyWinService";
Step 2. Add functionality to your service
The default code of WebService1.cs added will have two overridden functions OnStart and OnStop. The OnStart function executes when you start your service and the OnStop function gets execute when you stop a service. I write some text to a text file when you start and stop the service.
   1:  protected override void OnStart(string[] args) 
   2:  { 
   3:  FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt" , 
   4:  FileMode.OpenOrCreate, FileAccess.Write); 
   5:  StreamWriter m_streamWriter = new StreamWriter(fs); 
   6:  m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); 
   7:  m_streamWriter.WriteLine(" mcWindowsService: Service Started \n"); 
   8:  m_streamWriter.Flush();
   9:  m_streamWriter.Close(); 
  10:  } 
  11:  /// <summary> 
  12:  /// Stop this service. 
  13:  /// </summary> 
  14:  protected override void OnStop() 
  15:  { 
  16:  FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt" , 
  17:  FileMode.OpenOrCreate, FileAccess.Write); 
  18:  StreamWriter m_streamWriter = new StreamWriter(fs); 
  19:  m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); 
  20:  m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n"); m_streamWriter.Flush();
  21:  m_streamWriter.Close(); 
  22:  } 

Step 2.5.
a) Return to Design view for WinService1.
b) Click the background of the designer to select the service itself, rather than any of its contents.
c) Right click on the Designed Windows and in the Properties window, click the Add Installer link in the gray area beneath the list of properties.
d) By default, a component class containing two installers is added to your project. The component is named ProjectInstaller, and the installers it contains are the installer for your service and the installer for the service's associated process.
e) Access Design view for ProjectInstaller, and click ServiceInstaller1. In the Properties window, set the ServiceName property to MyNewService. Set the StartType property to Automatic.
f) In the designer, select ServiceProcessInstaller1 (for a Visual Basic project), or serviceProcessInstaller1 (for a Visual C# project). Set the Account property to LocalService. This will cause the service to be installed and to run on a local service account.
After the above procedure, try registering using InstallUtil utilities.
Step 3: Install and Run the Service
Build of this application makes one exe, MyWinService.exe. You need to call installutil to
egister this service from command line.
installutil C:\mcWebService\bin\Debug\mcWebService.exe
You use /u option to uninstall the service.
installutil /u C:\mcWebService\bin\Debug\mcWebService.exe
Run the application
Step 4: Start and Stop the Service
You need to go to the Computer Management to start and stop the service. You can use Manage menu item by right clicking on My Computer. 
Under Services and Applications, you will see the service MyWinService. Start and Stop menu item starts and stops the service.
Go to your temp directory and see if text file is there with contents or not.
That's it.

No comments: