Just wanted to share my experiences regarding the super cool mini framework, Topshelf – a bi-product from the development of MassTransit. When Dru and the other guys were working on MassTransit, they needed to create Windows services a lot, which led to the separation of the Windows service stuff from the other stuff – and now they call it Topshelf.
You use Topshelf by creating a console application. In the Main method, you create a configuration, which you hand over to Topshelf. Then, everything just works as expected.
Topshelf requires that you to use an IoC container hidden behind the common service locator interface, which is neat because then it’s up to you to decide which container to use. In this example, I am using Windsor.
Creating a service is as simple as this:
namespace SomeService { public class Program { [STAThread] public static void Main(string[] args) { const string serviceName = "some_service"; var cfg = RunnerConfigurator.New( x => { x.BeforeStart( s => { Console.WriteLine("Starting {0}...", serviceName); ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(CreateWindsorContainer())); }); x.AfterStop( s => { Console.WriteLine("Stopping {0}...", serviceName); ((WindsorServiceLocator) ServiceLocator.Current).Dispose(); }); x.ConfigureService<SomeService>( c => { c.WithName(serviceName); c.WhenStarted(s => s.Start()); c.WhenStopped(s => s.Stop()); c.WhenPaused(s => { }); c.WhenContinued(s => { }); }); x.SetDisplayName("My funky service"); x.SetDescription("This is a funky service that depends on MSMQ"); x.SetServiceName(serviceName); x.DependencyOnMsmq(); }); Runner.Host(cfg, args); } }
Everything in the snippet above works as expected. Note how I create my Windsor container and store it behind WindsorServiceLocator, which is my trivial implementation of the IServiceLocator interface. This way, Topshelf will pull the specified service type(s) (SomeService in the sample) from the container, and call the specified methods to start/stop (and possibly pause/resume) the service.
Note also how I specify that my service has a dependency on message queueing – neat!
Running and debugging the service is as easy as executing the .exe.
Installing/uninstalling can be done with
SomeService.exe /install SomeService.exe /uninstall
Nifty!
Sorry, the comment form is closed at this time.