Cinchoo framework simplifies the Windows service development. An application developed in this approach is ready to run as Console application as well as self install-able Service application. In this article, I’m going to walk over the steps of creating service development.
1. Create a new ‘Console Application‘ from VS.NET
2. Add reference to Cinchoo.Core.dll
3. Add namespace Cinchoo.Core
4. Create a class derived from ChoApplicationHost as below
[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
protected override void OnStart(string[] args)
{
//TODO: Application Startup code goes here
}
}
Make sure the type is decorated with ChoApplicationHostAttribute. And override OnStart method, where application start up code placed there. Implement any other service related (OnStop, OnShutdown etc) methods by overriding them.
5. In the main entry, do as below.
public class Program
{
static void Main(string[] args)
{
ChoApplication.Run(args);
}
}
That’s all, you application is self install able windows service application.
You can run it as console application or install it as Windows service also.
To Install as windows service, pass /@i command line argument
[AppExeName].exe /@i
To uninstall as windows service, pass /@u command line argument
[AppExeName].exe /@u
To start the service, pass /@s command line argument
[AppExeName].exe /@s
To stop the service, pass /@t command line argument
[AppExeName].exe /@t
To pause the service, pass /@p command line argument
[AppExeName].exe /@p
To continue the service, pass /@c command line argument
[AppExeName].exe /@c
To execute a command in the service, pass /@e:{command_id} command line argument
[AppExeName].exe /@e:2
To get the help on Windows service commands, pass /@?
C:\>TestServiceApp.exe /@?
TestServiceApp [Version 1.0.0.0]
Copyright c 2014
TESTSERVICEAPP [/@SN:<string>] [/@SD:<string>] [/@I] [/@U] [/@S] [/@T] [/@P]
[/@C] [/@E:<int>] [/@SP:<string>]
/@SN Service Name.
/@SD Service Description.
/@I Install Service.
/@U Uninstall Service.
/@S Start Service.
/@T Stop Service.
/@P Pause Service.
/@C Continue Service.
/@E Execute Command.
/@SP Command Line Parameters.
Controlling Service Installation
There are couple of settings classes which controls the service process behavior during installation. It can configured through configuration file or can be overridden programmatically. They are
- ChoServiceInstallerSettings – service process parameters used during installation of service.
- ChoServiceProcessInstallerSettings – service credential parameters used during installation of the service.
First, let take a look at the way configuring them through configuration file.
ChoServiceProcessInstallerSettings
These setting can be maintained in ChoServiceProcessInstallerSettings.xml file. If the file not exists, it will created by Cinchoo framework in application configuration directory. The file look as below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<serviceProcessInstallerSettings account="LocalSystem" userName="" password="">
<helpText />
</serviceProcessInstallerSettings>
</configuration>
If you want more information about each attribute/element in the above xml section, please visit ServiceProcessInstaller Class in MSDN.
ChoServiceInstallerSettings
These setting can be maintained in ChoServiceInstallerSettings.xml file. If the file not exists, it will created by Cinchoo framework in application configuration directory. The file look as below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<serviceInstallerSettings delayedAutoStart="false" displayName="ChoServiceHost.Test" serviceName="ChoServiceHost.Test" serviceStartMode="Automatic" />
</configuration>
If you want more information about each attribute/element in the above xml section, please visit ServiceInstaller Class in MSDN.
Next, let take a look at the way of overriding them programmatically.
Override ApplyServiceInstallParamsOverrides method in your ApplicationHost class, where you can changes service installation parameters.
[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
protected override void OnStart(string[] args)
{
Console.WriteLine("Application started...");
}
protected override void ApplyServiceInstallParamsOverrides(ChoServiceProcessInstallerSettings serviceProcessInstallerSettings, ChoServiceInstallerSettings serviceInstallerSettings)
{
serviceInstallerSettings.DisplayName = "Test";
serviceInstallerSettings.ServiceName = "Test";
}
}
In the above sample code, we are trying to override the ServiceName and DisplayName as ‘Test’. When you install the service, it will be created as ‘Test’ service.