Skip to content

Tutorial

Mike Schenk edited this page May 5, 2016 · 4 revisions

Using Visual Studio

  1. Create a Console Application.

.NET 3.5 and above is supported

  1. Install the EasyServiceHost package

Use Package Manager Console or

Install-Package EasyServiceHost
  1. Optionally, change Main to return int

  2. Create a bootstrap class

This class will become the entry point to your service.

The class can be named anything. It must have a public default (no-argument) constructor. It must implement the IManagedService interface:

	class Bootstrap : IManagedService
	{
		public void Start(IServiceHost host)
		{
			//TODO: start a new thread to do the work of your service.
		}

		public void Stop(TimeSpan timeLimit)
		{
			//TODO: stop the thread within the time specified in timeLimit.
		}
	}

The constructor and the Start method both run while the Windows Service Control Manager is waiting for a response to the start command. If you have initialization that could take longer than a couple of seconds, your should start a new thread to run it.

  1. Call ManagedHost.RunOne(args) from Main

This will handle and installation, uninstallation, and running your service depending on the command-line arguments supplied.

  1. Add configuration sections to App.config

Clone this wiki locally