Thursday, May 24, 2007

How to create a Windows Service?

This tutorial walks you through how to and use your Windows Service. This Service writes some text to a text file when you start and stop the Windows Service.

1) To create a new Windows Service, pick Windows Service option from your Visual C#/Windows Projects. Name your Windows Service project as SampleWindowsService (you can write any nameJ) and click OK.


Figure 1: Windows Service Project

2) Name the Window Service as WixService in solution explorer, then go to the WixService.cs file to add functionalities to Windows Service.

In WixService.cs, there are two overridden functions OnStart and OnStop. The OnStart function executes when you start your Windows Service and the OnStop function gets execute when you stop your Windows Service. Here to check the Windows Service, you can write some text to a text file when you start and stop the service.



protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
FileStream fs = new FileStream(@"C:\WIX_Work\SampleWindowsService\WixService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WixService: Service Started \n");
sw.Flush();
sw.Close();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
FileStream fs = new FileStream(@"C:\WIX_Work\SampleWindowsService\WixService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WixService: Service Stopped \n");
sw.Flush();
sw.Close();
}


3) Now to add an installer to SampleWindowsService project. Go back to the design surface for WixService and right-click on it. Select Add Installer, which creates a new component called ProjectInstaller and adds it to the project.

4) Select the ServiceProcessInstaller1 control on the ProjectInstaller design surface. Change the Account property in its Properties window to LocalSystem. Then select the ServiceInstaller1 control. In its Properties window, change the ServiceName property to WixService. Now build the project. This creates SampleWindowsService.exe for the service.

5) Now you're ready to install the service. To install the Windows Service, open the Visual Studio 2005 Command Prompt. Run the installutil.exe by providing the path of SampleWindowsService.exe path.


Figure 2: Installation of Windows Service

6) After installing the Windows Service, open the Services. To open Services, use services.msc in Run command prompt. Services display the newly installed Windows Service i.e. WixService.


Figure 3: WixService in Services Window

7) To uninstall the Windows Service, use /u with installutill.exe in command prompt.


Figure 4: Uninstallation of WixService

8) To Start/Stop the Windows Service, select the service, click Start/Stop button present in the toolbar or select the appropriate menu by right-clicking on selected service.


Figure 5: WixService Start/Stop

9) When you Start/Stop the Windows Service, text entries occurs in the specified text file.


Figure 6: Text file that contains text, when Windows Service Start/Stop

Appendix

WixService.cs



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace SampleWindowsService
{
public partial class WixService : ServiceBase
{
public WixService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.

FileStream fs = new FileStream(
@"C:\WIX_Work\SampleWindowsService\WixService.txt",
FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WixService: Service Started \n");
sw.Flush();
sw.Close();
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down
//necessary to stop your service.

FileStream fs = new FileStream(
@"C:\WIX_Work\SampleWindowsService\WixService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WixService: Service Stopped \n");
sw.Flush();
sw.Close();
}
}
}





2 comments:

Unknown said...

nice work... keep it up.....

Unknown said...

Thanks Avinash...

Google