This article presents the guide to write windows service program and control it from other program.
Background
In many projects which I was part in, needed the program running as windows service. It can auto startup when Windows Restart and access Domain resource when it runs under special user. But sometimes, windows service program is not easy to control and config, for example: I need the windows service running once a day, but maybe I change my idea, need it runs every three hours, so how to config it or make the windows service more flexiable is a puzzle.
Solution
1. Make windows service running as it is, do some little modify to other program can control it;
2. Write a small application to control the windows service;
3. User Windows Schedular to run the application, config run time as you will
Steps
1. Write windows service program ( Here we start to program step by step)
Step 1. File -> Project -> Windows C# -> Windows -> Console Application
Step 2. Add Reference to this project
System.Configuration.Install
System.ServiceProcess
Step 3. Add New Class :SimpleServiceMain.cs ( you can rename the Program.cs to it, or delete Program.cs then Add a new )
public class SimpleServiceMain : ServiceBase
{
....
protected override void OnCustomCommand(int command)
{
OutputDebugString("[SimpleService]" + "SimpleService OnCustomerCommand(" + command.ToString() + ")");
base.OnCustomCommand(command);
}
....
}
Step 4. Add New Class :SimpleServiceInstall.cs
[RunInstaller(true)]
public class SimpleServiceInstall : Installer
{
public SimpleServiceInstall()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
serviceInstaller.DisplayName = "SimpleService";
serviceInstaller.Description = "SimpleService";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "SimpleService";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
Step 5. Install the windows service Here is a batch script to install/unstall the service
Install.bat
==============================================================
ECHO OFF
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i SimpleService.exe
sc start SimpleService
echo ---------------------------------------------------
echo Done
pause
==============================================================
Unstall.bat
==============================================================
@ECHO OFF
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
sc stop SimpleService
echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /u SimpleService.exe
echo ---------------------------------------------------
echo Done
pause
==============================================================
2. Write windows application to control windows service
Please make sure when the application run, the service have been installed to system
Step 1. File -> Project -> Windows C# -> Console Application
Step 2. Add Reference to this project
System.Configuration.Install
System.ServiceProcess
Step 3. Add these codes in Program.cs
...
static void Main(string[] args)
{
string aMachine = "." ;
string aServiceName = "";
if( args.Length == 1 ){
aServiceName = args[0];
}
else if (args.Length >= 2)
{
aMachine = args[0];
aServiceName = args[1];
}
else
{
Console.WriteLine("Error Parameters, the command line should be: ");
Console.WriteLine("SimpleServiceController ServiceName or");
Console.WriteLine("SimpleServiceController Machine Name ServiceName");
}
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController();
sc.MachineName = aMachine;
sc.ServiceName = aServiceName;
sc.ExecuteCommand(130);
}
...
3. How to use Windows Schedular to control your program running
Please read windows helps for more information
4. More
You can use Windows Batch(.bat) to control multi services by SimpleServiceController
5. About OutputDebugString, you can get the tools to view the informations