Topshelf

So this is my quasi-new project. It is essentially the MassTransit.Host ripped out and in its own project. It is also the embodiment of my lessons learned about building services (daemons) on the windows platform.

Sample Code

var cfg = RunnerConfigurator.New(x =>
    {
       x.SetDisplayName("Timeout Service");
       x.SetServiceName("mt_timeout");
       x.SetDescription("Runs the MassTransit Timeout Service");
           
       x.ConfigureService<TestService>(c=>
           {
               c.WithName("my_service"); //this defaults to type name
               c.WhenStarted(s => s.Start());
               c.WhenStopped(s => s.Stop());
               c.WhenPaused(s => { });
               c.WhenContinued(s => { });
           });
       x.ConfigureService<TestService>(); //defaults (still working on this)

       //By default the service is set to start automatically
       x.DoNotStartAutomatically();

       //several different runas options
       x.RunAs("dru", "pass");
       x.RunAsLocalSystem();
       //x.RunAs(AppConfig("username"), AppConfig("password"));
       x.RunAsFromInteractive();

       //set service dependencies
       x.DependsOn("ServiceName");
       x.DependencyOnMsmq();
       x.DependencyOnMsSql();
   });

Runner.Host(cfg, args)

This code would be in a .Net console application and would be executed like:

timeoutservice.exe            #Runs as a console
timeoutservice.exe /install   #installs the service
timeoutservice.exe /uninstall #uninstalls the service
timeoutservice.exe /gui       #runs the default winform (must be in container)

So, here are some of the things it provides: start/stop individually hosted services; run the service in console, service, or winform hosts; self service installing; delayed container setup for faster install/uninstall; POCO and some things it will provide soon: services start/stop won’t effect other services, WMI integration.

I don’t see this as a game changing project, more than it is a place to harvest my education, and share with others. So if you write winservice code take a look and see if there is anything you can pass along yourself, if you don’t write winservice code but need to this can go a long way to helping that out.

Topshelf requires you to use an IoC container, which has made writing this project so much easier as I can focus on the service aspects and less on the object creation. I am using the Common Service Locator so you can use whatever container you want. I am also directly using the log4net framework at the moment and that is about all that their is for dependencies.

The big win for me is that I can now start my app as a console, test it, and then when happy run it as a service knowing its running in exactly the same way.

The project also provides hooks into some of the more esoteric error events so that we can log the more odd errors.

Its hosted at http://topshelf.googlecode.com/

I also want to take a minute to throw out some props to Chris Patterson my partner in crime and Jeremy Miller for the mad FI skills I keep ripping off from StructureMap.

-d

About Dru Sellers

Sr. Software Engineer at Dovetail Software.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • cheap wedding dresses

     
    Interesting post and thanks for sharing. Some things in here I have not thought about before.Thanks for making such a cool post which is really very well written.will be referring a lot of friends about this.Keep blogging

  • Coach Tote Bags
  • Javed

    When I install and run as a windows service, the app exe is failing to signal that is has finished starting to the services console. Any ideas?

  • http://ornywynj.com/ Meslgjqb

    sDyHvn

  • Anthony Kilhoffer

    What about cases where you want to supply the service Type at runtime? I notice in your host example, the service type is supplied via a generic parameter. What about runtime specification of service type?

    Great idea, though. Thanks for sharing…

  • Brad Mead

    Awesome, Awesome, Awesome!

    Just looking to ditch the kludgy (Provider/Abstract Factory/Activator) configuration pluming in my WindowsService apps in favor of DI/IoC. There is not a lot of practical examples of this out there… But THIS is so freakin timely it’s scary – especially as I met you (and Sergio from above) at Alt Austin 07.

    Sergio your post is also one more perspective of dealing with the same problem. I NEED perspective.

    Thanks Dru.

    P.S. See you both in a couple of weeks.

  • http://devlicio.us/blogs/sergio_pereira/ Sergio Pereira

    I’ve been snooping around in Topshelf’s code for a few days. I like what you guys did there. I had talked about some windows service libraries before ( http://devlicio.us/blogs/sergio_pereira/archive/2008/03/31/creating-windows-services.aspx ) and integrating IoC is something I wish I had built it from the start.