-
Notifications
You must be signed in to change notification settings - Fork 10
Simplify.WindowsServices
Provides BasicServiceBase, MultiTaskServiceBase, SingleTaskServiceBase windows-services base classes, ServiceInstallerBase installation base class.
Allows you to simply create and use windows services.
BasicServiceBase class is best suitable for services which is working without timers, for example, TCP server or client.
public class Service : BasicServiceBase
{
public Service()
{
Initialize += LoadSettings;
Run += OnRun;
}
private bool LoadSettings()
{
// Loading settings
return true;
}
public static bool OnRun()
{
// doing some action, for example, open a TCP listener in asynchronous way
// OnRun method will be called only once
return true;
}
}static void Main()
{
ServiceBase.Run(new Service());
}SingleTaskServiceBase class is best suitable for one-threaded operations which is working by timer.
In Initialize event you should call InitializeTimer method to set a timer points when the Work event should be raised.
OnWork method will be executed in a separated thread.
You can set a time points when the Work event will be raised or set a specified interval:
Settings time points:
InitializeTimer("12:00, 18:45");var timePoints = new List<DateTime>();
...
InitializeTimer(timePoints);or
settings interval (in milliseconds)
InitializeTimer(0, 1000);public class Service : SingleTaskServiceBase
{
public Service()
{
Initialize += LoadSettings;
Work += OnWork;
}
private bool LoadSettings()
{
InitializeTimer("12:00, 18:45");
return true;
}
private void OnWork(object state)
{
// doing some periodical work
}
}MultiTaskServiceBase class is the same as SingleTaskServiceBase but allows you to run multiple jobs at the same time (each job runs on a separated thread).
In Initialize event you should call AddJob method to add a service jobs.
public class Service : MultiTaskServiceBase
{
public Service()
{
Initialize += ServiceInitialize;
}
bool ServiceInitialize()
{
AddJob(new ServiceJob(OnWork1, "01:00"));
AddJob(new ServiceJob(OnWork2, 0, 60000));
return true;
}
private void OnWork1(object state)
{
}
private void OnWork2(object state)
{
}
}ServiceInstallerBase class is allows you to install a windows service with information from your assembly information fields, like Title, Description.
Description and Title fileds of specified assembly will be used as your service name and description.
-
Titlewill be used as a service ID -
Descriptionwill be used as s service name and description (you will be able to see that information inServices.mscafter service will be installed).
[RunInstaller(true)]
public class ServiceInstaller : ServiceInstallerBase
{
public ServiceInstaller()
: base(Assembly.GetExecutingAssembly())
{
}
}You can specify via configuration file a user name under which a service will be run.
[RunInstaller(true)]
public class ServiceInstaller : ServiceInstallerBase
{
public ServiceInstaller()
// Enabling service to run as a specified user name and password from a configuration file
: base(Assembly.GetExecutingAssembly(), true)
{
}
}<?xml version="1.0"?>
<configuration>
<configSections>
<section name="ServiceInstallerSettings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<ServiceInstallerSettings>
<add key="RunAsUserName" value="UserName"/>
<add key="RunAsUserPassword" value="UserPassword"/>
</ServiceInstallerSettings>
</configuration>Note: you can specify any user, for example: domain user as "YourDomain\UserName"
using System.IO;
using Simplify.WindowsServices;
namespace MyService
{
public class Service : SingleTaskServiceBase
{
public Service()
{
Initialize += LoadSettings;
Work += OnRun;
}
private bool LoadSettings()
{
InitializeTimer("12:00");
return true;
}
public static void OnRun(object state)
{
File.WriteAllText("FooFile.txt", "Hello world!!!");
}
}
}namespace MyService
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static void Main()
{
ServiceBase.Run(new Service());
}
}
}using System.ComponentModel;
using System.Reflection;
using Simplify.WindowsServices;
namespace MyService
{
[RunInstaller(true)]
public class ServiceInstaller : ServiceInstallerBase
{
public ServiceInstaller()
: base(Assembly.GetExecutingAssembly())
{
}
}
}@ECHO OFF
echo Installing MyService...
c:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /LogFile= /LogToConsole=true MyService.exe
pause @ECHO OFF
echo Uninstalling MyService...
c:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u /LogFile= /LogToConsole=true MyService.exe
pause