Thoughts, Tips and Tricks on what I'm currently do for a living. Currently most of my spare time is spent on contributing to Akka.NET.

Friday, September 23, 2011

Using the Topshelf Shelving Bootstrapper in a Console App

Note! This was written for Topshelf and things might have changed in newer versions.


Topshelf ( is a framework for building Windows services. The service can either be standalone or one that is hosted by Topshelf. The latter style is called shelving (and the service you write is called a shelf) and it enables xcopy deploy of your service, instead of stop service, uninstall, copy, install, start.

Debugging a shelf

The problem with using the shelving style for your service is debugging. The documentation explains how to do it (, but it’s not that neat as it involves modifying where your build outputs the dlls.

An easier way, at least for me, would be to debug the shelf service by starting it from a console app, i.e. as a standalone service, without any need to modify where the output from the build goes. Ideally the console app would be as simple as possible, just reusing the code from the shelf.

A shelf service is initiated and configured by a bootstrapper (a class that inherits the interface Bootstrapper<T>) but unfortunately, if you compare the configuration for a standalone service with the shelf bootstrapper, they are not configured the same way, so reusing the configuration from the shelf bootstrapper in a standalone service seems not possible, but it’s actually quite easy.

Reuse the bootstrapper

Instead of implementing Bootstrapper<T> derive from this class instead:

public abstract class BootstrapperBase<T> : Bootstrapper<T> where T : class
  void Bootstrapper<T>.InitializeHostedService(IServiceConfigurator<T> cfg)

  public abstract void InitializeHostedService(ServiceConfigurator<T> cfg);

Instead of implementing
void InitializeHostedService(IServiceConfigurator<T> cfg)
you implement
void InitializeHostedService(ServiceConfigurator<T> cfg)

The bootstrapper in the example at becomes (changes are highlighted)

public class AShelvedClockBootstrapper :
  public override void InitializeHostedService(ServiceConfigurator<TheClock> cfg)
    cfg.ConstructUsing(n => new TheClock());
    cfg.WhenStarted(s =>
      XmlConfigurator.Configure(new FileInfo(Path.Combine(
    cfg.WhenStopped(s => s.Stop());

In the console app you start the service like this:

static void Main(string[] args)
  XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));
  HostFactory.Run(x =>
        s => new AShelvedClockBootstrapper().InitializeHostedService(s)); x.RunAsLocalSystem();   x.SetServiceName("TheClock"); }); }

No comments:

Post a Comment