Dru Sellers

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted Sun, Jan 11 2009 5:27 PM by drusellers

[Advertisement]

Comments

Sergio Pereira wrote re: Topshelf
on Sun, Jan 11 2009 10:08 PM

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 ( devlicio.us/.../creating-windows-services.aspx ) and integrating IoC is something I wish I had built it from the start.  

Dew Drop - January 12, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop - January 12, 2009 | Alvin Ashcraft's Morning Dew
on Mon, Jan 12 2009 11:23 AM

Pingback from  Dew Drop - January 12, 2009 | Alvin Ashcraft's Morning Dew

Reflective Perspective - Chris Alcock » The Morning Brew #263 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #263
on Tue, Jan 13 2009 1:55 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #263

igorbrejc.net » Fresh Catch For January 13th wrote igorbrejc.net &raquo; Fresh Catch For January 13th
on Tue, Jan 13 2009 8:03 AM

Pingback from  igorbrejc.net &raquo; Fresh Catch For January 13th

Brad Mead wrote re: Topshelf
on Thu, Feb 5 2009 8:15 PM

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.

Anthony Kilhoffer wrote re: Topshelf
on Thu, May 21 2009 3:10 PM

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

Meslgjqb wrote re: Topshelf
on Tue, Jul 14 2009 7:03 PM

sDyHvn

Kevin Miller wrote Running Multiple Instances of a Windows Service Using TopShelf
on Thu, Sep 24 2009 4:43 PM

Here is another one where I post about something Josh and team did that I borrowed and am using to great

geff zhang wrote 使用Topshelf创建Windows 服务
on Tue, Nov 24 2009 9:09 AM

Winndows Service 是一种可随 Windows 操作系统启动而启动的,在后台运行的,通常不和用户产生交互的程序。它无法通过双击来运行,类似于 Unix 守护进程(daemon proce...

Javed wrote re: Topshelf
on Tue, Jan 26 2010 6:33 PM

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?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?