Dovetail Bootstrap – Now With Service

A customer recently asked me how to best create a Windows service using our Dovetail SDK. To me this sounds like a task for Dovetail Bootstrap. We have already provided a nice example of using Bootstrap of to build a web application. We did not, until now, have an example of using Bootstrap and Dovetail SDK to create a Windows service. Bootstrap makes it easy to pull data out of your Clarify database and Topshelf makes it easy to roll your own Windows services . Let’s put them both together and build an example Windows service that monitors case creation in your Clarify database.


If you’d like to skip right to the example you can find it in our Dovetail Bootstrap source repository.





At Dovetail we are big fans of Topshelf and its ability to easily create Windows services from plain .Net console applications. Our Dovetail Seeker and Dovetail Carrier products both use Topshelf for this purpose. First let’s take a look at how Topshelf configuration of your Windows service is done when your application starts up.



The code above informs Topshelf on how to configure Windows when you install this application as  a Windows service.  As the comment notes. The installation of the Windows service is done using the command line. It is easy to have your deployment script or windows installer automate this step. The most important part here is the service registration. You are not limited to registering only one service. BootstrapService wires everything up so let’s take a look at that.  


Topshelf is using an interface here to ensure you have methods for starting and stopping the service. In this example the start method constructs the IoC container and uses it to create a CaseMonitor which will do all the work. The IoC container is there because Bootstrap uses it to bring a lot of value. Things like automatic population of settings objects, getting a ClarifySession for the current user, logging support, and much more. Additionally using IoC containers make writing tests easier. Lets move on and take a look at the CaseMonitor.



Polling The Database



CaseMonitor starts a timer that pulses every 15 seconds polling the database using a model map filtered by a timespan. It them simply writes to a logger what it finds.  The IoC container is in charge of providing the CaseMonitor’s constructor with instances of the types it is dependent on. The more interesting thing here is the IModelBuilder or RecentCaseModel. This builder uses a Model Map to know how to pull data from a Clarify database.    

Model Maps



At Dovetail we use Model Maps extensively in our applications when we need to pull data out of the database into models (Projection) which we’ll be using in our MVC based applications.



Wrap Up


You might notice how little data access code we actually wrote. That is the beauty of Model Map. To make the Windows service aspect of your application you only need to give Topshelf some details and implement a ServiceControl based class. The two Dovetail applications I mentioned use the basic pattern demonstrated here but push TopShelf harder registering many Services using the IoC container and utilizing a message bus architecture MassTransit from the same smart people Dru Sellers and Chris Patterson that brought you Topshelf. This extra messaging sauce which makes it easy to produce and consume a lot of work across many threads making the most of your server hardware.