Documentation is in the works, but here's a quick overview:
A Windows service created with Vici WinService has the following components:
You start out by creating a .NET console application.
Then create a class for your service:
class MyService : Service { public MyService() : base(new ServiceInfo("MyService", "My custom-built service")) { // MyServiceTask is a task that should be run by this service ServiceTasks.Add(new MyServiceTask()); } }
Every service needs one or more tasks to execute. These are created by deriving your task from one of the ServiceTask classes. In this case we'll derive from CyclicServiceTask, because we want our task to run every 30 seconds:
class MyServiceTask : CyclicServiceTask { public MyServiceTask() : base(TimeSpan.FromSeconds(30), false) { } protected override void RunTask() { // Do your stuff here } }
A cyclic service tasks runs at fixed intervals. The interval should be passed as the first parameter of the base class CyclicServiceTask. You can also run your task at scheduled times (daily, weekly, ...) by deriving from ScheduledServiceTask (ScheduledServiceTask is still in development).
That's basically it... Now you only have to write some simple plumbing code to install, uninstall and run the service:
The Service class has the following important methods:
| Install() | Installs the service in the Windows service registry | |
| UnInstall() | Removes the service from the Windows service registry | |
| Run() | Runs the service | |
| RunConsole() | Runs the service as a console application (for debugging) | |
Bringing this all together, we can write the following Main() method:
static class Program { static void Main(params string[] parameters) { MyService service = new MyService(); if (parameters.Length > 0) { string option = parameters[0].ToLower(); switch (option) { case "/console": service.RunConsole(); return; case "/install": service.Install(); return; case "/uninstall": service.UnInstall(); return; } } service.Run(); } }
Compile your application, and you are ready deploy and run your service:
To install: myservice /install
To remove: myservice /uninstall
To debug, or run from the console, specify the "/console" parameter.
Complete documentation will follow shortly...