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 Mar 2, 2014 · 33 revisions

Provides BasicServiceBase, MultiTaskServiceBase, SingleTaskServiceBase windows-services base classes, ServiceInstallerBase installation base class.

Allows you to simply create and use windows services.

Windows-service base classes

BasicServiceBase

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

Example

Service.cs
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;
	}
}
Program.cs
static void Main()
{
	ServiceBase.Run(new Service());
}

SingleTaskServiceBase

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);

Example

Service.cs
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

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.

Example

Service.cs
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)
	{
	}
}

Windows-service 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"

Full example

Service.cs
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!!!");
		}
	}
}
Program.cs
namespace MyService
{
	internal static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		private static void Main()
		{
			ServiceBase.Run(new Service());
		}
	}
}
ServiceInstaller.cs
using System.ComponentModel;
using System.Reflection;
using Simplify.WindowsServices;

namespace MyService
{
	[RunInstaller(true)]
	public class ServiceInstaller : ServiceInstallerBase
	{
		public ServiceInstaller()
			: base(Assembly.GetExecutingAssembly())
		{
		}
	}
}
Install.bat
@ECHO OFF

echo Installing MyService...
c:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /LogFile= /LogToConsole=true MyService.exe
pause 
Uninstall.bat
@ECHO OFF

echo Uninstalling MyService...
c:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u /LogFile= /LogToConsole=true MyService.exe
pause 

Clone this wiki locally