Service & Scheduler: Using Topshelf, Quartz, & Other OSS Bits Part 1

This how-to entry will detail the steps for getting Topshelf installed, running, and a schedule integrated and timed appropriately using Quartz. Once that is complete I’ll go over how to get custom schedules added to the service.

Topshelf

To get Topshelf up and running open up Visual Studio, start a Windows Console Project, and then use Nuget (if you don’t have Nuget installed, I suggest doing that ASAP, you’ll need it to follow along with any reference additions).

Get Topshelf Through Nuget

Get Topshelf Through Nuget

Once you have Topshelf and associated references added via Nuget create a new class file called StatusCheckIn and add the following code.

using System;
using System.Timers;

namespace Sample.Services
{
    public class StatusCheckIn
    {
        readonly Timer _timer;

        public StatusCheckIn()
        {
            _timer = new Timer(5000) { AutoReset = true };
            _timer.Elapsed += TimeHappened;
        }

        static void TimeHappened(object sender, EventArgs eventArgs)
        {
            Console.WriteLine("Status Time: {0} checking in.",
                DateTime.Now);
            Console.WriteLine("Sender Type: {0}",
                sender.GetType());
            Console.WriteLine("Event Argument Type: {0}",
                eventArgs);
        }

        public void Start()
        {
            _timer.Start();
        }

        public void Stop()
        {
            _timer.Stop();
        }
    }
}

After adding the StatusCheckIn Class, open up the Program.cs file and add or change the following code.

using Topshelf;

namespace Sample.Services
{
    class Program
    {
        static void Main()
        {
            HostFactory.Run(hostConfigurator =>
            {
                hostConfigurator.Service<StatusCheckIn>(
                    serviceConfigurator =>
                {
                    serviceConfigurator.SetServiceName("TS");
                    serviceConfigurator.ConstructUsing(
                        name => new StatusCheckIn());
                    serviceConfigurator.WhenStarted(tc => tc.Start());
                    serviceConfigurator.WhenStopped(tc => tc.Stop());
                });

                hostConfigurator.RunAsLocalSystem();
                hostConfigurator.SetDescription("Sample  Host");
                hostConfigurator.SetDisplayName("Display Name");
                hostConfigurator.SetServiceName("Topshelf Service");
            });
        }
    }
}

If you run into an odd reference error, it is likely that VS2010 has determined you want the client profile of .NET 4.0 again. If anyone at MS on the VS2010/.NET 4.0 reads this, we DO NOT want this to be the default. It’s ridiculous that it gets set as default without some type of manual change. But I digress, open up the properties of the console/service project and set it under the dialog as shown.

Setting the appropriate .NET 4.0 Version in the Properties Dialog

Setting the appropriate .NET 4.0 Version in the Properties Dialog

NOTES: Working code available on Github under my Topshelf Repo.

3 comments
  1. Pingback: DotNetShoutout

  2. mhamrah said:

    +1 re: .NET Client Profile Default. So annoying!

    P.s. Nice WordPress theme.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Metal Cactus

Just another WordPress.com weblog

ChaniBlog

Code, cooking, and randomness.

plain old objects

the building blocks of software

midnight mystery ride

at midnight, we ride.

Riding on Roadways

Writing on Riding on Roadways

Not Rich Yet

It's going to happen. Gotta find something to do until then.

craftedincarhartt

Carhartt Women's Blog

heydev

For the love of code

Nathan Evans' Nemesis of the Moment

My nemesis of the moment

Open Source Bridge: Presentation Proposals

Snippets, software architecture, lean, agile, management, and leadership bits.

Captured Refractions

A collection of my latest adventures, past reflections and other photos.

for the love of Nike

for the love of Nike

The Cloud Dev

Developing {for/ on/ the} Cloud...

Project Manager in a Cloudy IT World

Thoughts, comments and ideas from experiences as a Project Manager in IT

iBikeuBike

If I can bike... So can you!

MAX FAQs

Portland Light Rail

UX Success

User Experience Design, Agile Development, Lean UX, Start Up

The lost outpost

a weblog by Andy Piper about technology, photography, and life

SaintGimp

Agile development, software craftsmanship, continuous improvement - Eric Lee's blog

Clang and Clamour

pardon the construction noises while we build the internet

Follow

Get every new post delivered to your Inbox.

Join 5,548 other followers

%d bloggers like this: