Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Simplify.WindowsServices

Alexanderius edited this page Oct 16, 2014 · 33 revisions

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

Windows-service base classes

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

BasicServiceHandler class is best suitable for services which is working without timers, for example, TCP server or client.

Example

MyClass.cs
public class MyClass
{
	public void Run()
	{
		// Some task
	}
}
Program.cs
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

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.

Example

App.config
<?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

MyClass.cs
public class MyClass
{
	public void Run()
	{
		// Some task
	}
}
Program.cs
static void Main()
{
	ServiceBase.Run(new SingleTaskServiceHandler<MyClass>(true));
}

DI example

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();
	}
}
Program.cs
static void Main()
{
	DIContainer.Current.Register<IMyClass2, MyClass2>();
	DIContainer.Current.Register<MyClass>();

	ServiceBase.Run(new SingleTaskServiceHandler<MyClass>());
}

Exceptions catching which can be thrown when resolving instances by Simplify.DI in handler:

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

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.

Example

MultitaskServiceHandler working example

Program.cs
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);
}
TaskProcessor1.cs
public class TaskProcessor1
{
	public void Run()
	{
		Debug.WriteLine("TaskProcessor1 Run executed");
	}

	public void RunTask2()
	{
		Debug.WriteLine("TaskProcessor1 RunTask2 executed");
	}
}
TaskProcessor2.cs
public class TaskProcessor2
{
	public void Run()
	{
		Debug.WriteLine("TaskProcessor2 Run executed");
	}
}
App.config
<?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>

Windows-service installer base class

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.

  • Title will be used as a service ID
  • Description will be used as s service name and description (you will be able to see that information in Services.msc after service will be installed).

Default ServiceInstallerBase usage

[RunInstaller(true)]
public class ServiceInstaller : ServiceInstallerBase
{
	public ServiceInstaller()
		: base(Assembly.GetExecutingAssembly())
	{
	}
}

Specifying service "RunAs" user

You can specify via configuration file a user name under which a service will be run.

Installer example

[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)
	{
	}
}

configuration example

<?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"

Clone this wiki locally