-
Notifications
You must be signed in to change notification settings - Fork 10
Simplify.WindowsServices
Provides BasicServiceHandler, SingleTaskServiceHandler, MultitaskServiceHandler windows-services base classes, ServiceInstallerBase installation base class.
Allows you to simply create and use windows services. Every user class instance will be instantiated using Simplify.DI IOC container which provides DI by default for your windows services.
Available at NuGet as binary package
To use Simplify.WindowsServices, you should create a class with Run method in it and pass this class to handler as a type parameter.
Run method will be invoked by the handler class, in this method you should start your windows service processing logic.
Your class instance will be created using IOC container Simplify.DI, so you can registed any types from which your class depends via DIContainer.Current and they will be automatically resolved.
Run method can be Run() or Run(string ServiceName) if you want to get service name in it.
BasicServiceHandler class is best suitable for services which is working without timers, for example, TCP server or client.
public class MyClass
{
public void Run()
{
// Some task
}
}static void Main()
{
ServiceBase.Run(new BasicServiceHandler<MyClass>(true));
}The true parameter indicates what ServiceProcess class will be automatically registered in DIContainer.Current like DIContainer.Current.Register<MyClass>();
otherwise you should do it manually:
static void Main()
{
DIContainer.Current.Register<MyClass>();
ServiceBase.Run(new BasicServiceHandler<MyClass>());
}SingleTaskServiceHandler class is best suitable for single task operation which is working by timer.
MyClass instance will be instantiated by the timer every time and MyClass.Run method will be invoked.
You can specify timer intervals in seconds or just set a Ncrontab expression for timer.
This options should be set via App.config file. If no settings specifed in config file, then the timer will be executed once every minute.
If both timer interval and crontab expression specified in the config file, then the crontab expression will be used instead.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ServiceSettings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<ServiceSettings>
<!-- Specifying crontab expression -->
<add key="CrontabExpression" value="* * * * *" />
<!-- Or specifying timer interval in seconds -->
<add key="ProcessingInterval" value="30" />
</ServiceSettings>
</configuration>For crontab expression examples see
SingleTaskServiceHandler working example
public class MyClass
{
public void Run()
{
// Some task
}
}static void Main()
{
ServiceBase.Run(new SingleTaskServiceHandler<MyClass>(true));
}public interface IMyClass2
{
SomeMethod();
}
public class MyClass2 : IMyClass2
{
public SomeMethod()
{
}
}
public class MyClass
{
private IMyClass2 _myclass2;
public MyClass(IMyClass2 myclass2)
{
_myclass2 = myclass2;
}
public void Run()
{
_myclass2.SomeMethod();
}
}static void Main()
{
DIContainer.Current.Register<IMyClass2, MyClass2>();
DIContainer.Current.Register<MyClass>();
ServiceBase.Run(new SingleTaskServiceHandler<MyClass>());
}static void Main()
{
var handler = new SingleTaskServiceHandler<MyClass>();
handler.OnException += OnException;
ServiceBase.Run(handler);
}
static void OnException(ServiceExceptionArgs args)
{
Console.Write(args.ServiceName);
Console.Write(args.Exception.Message);
}MultitaskServiceHandler class is best suitable for multiple task services.
It is the same as SingleTaskServiceHandler but allows you to specify multiple working classes/methods.
You can add multiple jobs with different timer intervals/crontab expressions.
You can use single class with multiple methods, each specified method will be lauched by respective task (new class instance will be created), or just use multiple classes.
MultitaskServiceHandler working example
static void Main()
{
// Handler creation
var handler = new MultitaskServiceHandler();
// Jobs addition
// If configuration section is not specified then 'YourClassName + Settings' section name will be used
handler.AddJob<TaskProcessor1>(true);
// Manually specified section name and invoke method name
handler.AddJob<TaskProcessor1>("TaskProcessor1SecondTaskSettings", "RunTask2");
// Registering class manually
DIContainer.Current.Register<TaskProcessor2>();
handler.AddJob<TaskProcessor2>();
ServiceBase.Run(handler);
}public class TaskProcessor1
{
public void Run()
{
Debug.WriteLine("TaskProcessor1 Run executed");
}
public void RunTask2()
{
Debug.WriteLine("TaskProcessor1 RunTask2 executed");
}
}public class TaskProcessor2
{
public void Run()
{
Debug.WriteLine("TaskProcessor2 Run executed");
}
}<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="TaskProcessor1Settings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="TaskProcessor1SecondTaskSettings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="TaskProcessor2Settings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<TaskProcessor1Settings>
<add key="CrontabExpression" value="*/2 * * * *" />
</TaskProcessor1Settings>
<TaskProcessor1SecondTaskSettings>
<add key="ProcessingInterval" value="30" />
</TaskProcessor1SecondTaskSettings>
<TaskProcessor2Settings>
<add key="ProcessingInterval" value="45" />
</TaskProcessor2Settings>
</configuration>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"