CodeSnips

Tuesday, September 21, 2010

TopShelf - Shelving

Played around with a nifty new feature for TopShelf 2.0 called "shelving" that allows you to create a service simply by dropping a .Net assembly dll into a folder.

Topshelf is a tool for easily creating and installing a Windows service in .Net that can run as both a console program, as well as a service.

The new feature is itself a Topshelf-created service called Topshelf.Host.exe. When installed, the service appears in the services control panel as "Topshelf.Host".

The documentation for this new feature is a little sparse. OK, it's
really sparse. So, I'm posting a few tips to get started with this new feature.

1. Start by getting the source code and building it.
-The current project is on GitHub Topshelf Repository.
-Run build.bat.
-This creates a folder:

{Your Project Root}\Topshelf\build_output\Topshelf

2. Install the
Topshelf.Host service.
- Copy the Topshelf folder under build_output to a location of your choice, for example: c:\tools\TopShelf
.
- Navigate to the folder in a command shell.
- Execute:
Topshelf
.Host install

3. Setup logging.
The default log4net.config file only has a consoleappender - not very useful when the host service is running as a service. There is a clock.log4net.config in the Topshelf folder which you can copy and rename to log4net.config - then edit it to change the logfile name from "..\..\clock.log" to "topshelf.log".

4. Install "StuffOnAShelf" sample service.
The docs here are not super clear. There is a sample project in the TopShelf solution; however, the output of build.bat doesn't emit the sample folders, and, if you build the project in Studio, you'll find the full set of required DLLs are not placed in the "obj" folder.

But, the build does put the binaries and sample
configs in the TopShelf directory. You just need to assemble everything yourself in a folder and drop in into the TopShelf\Services folder.

The easiest thing to do is create a folder under the Topshelf directory called "StuffOnAShelf", and copy the following items into it from the Topshelf directory:
clock.log4net.config
log4net.dll
Magnum.dll
TopShelf.dll
clock.config (rename this to StuffOnAShelf.config)
StuffOnAShelf
.dll


Then drag the entire "StuffOnAShelf" directory under the Topshelf\Services folder.

5. Examine the results.
If everything went well, you should find two log files in the TopShelf directory:
clock.log
topshelf.log (assuming you used the log4net config suggested above).

You should see the topshelf host messages showing the loading of the StuffOnAShelf service.
You should see the output of the service itself in the clock.log.

Summary

This is a pretty new feature. There is no way to directly stop the "sub-services" hosted by the Topshelf.Host service - short of removing their directory out from under the Topshelf\Services folder.

Also, there's no direct way to know if they are actually running - short of looking at their log files.

At some point. it's likely that a console could be developed to watch the "sub-services" and control them. Until then, if you want full control, you'll need to look at the other sample project - called "Stuff" - which let's you create a full service that shows up in the services control panel.