Topshelf

Monitor .Net Windows Service in 3 Easy Steps

knowventBanner

Topshelf makes creating and installing a windows service as easy as one command line. Monitor .Net Windows Service is another animal all together.

Now if you are not familiar with Topshelf Christoph De Baene wrote a great article on how to create a net windows service in 5 steps using Topshelf.

Now that you are a Topshelf expert and have a service running you probably what to keep an eye on it (preferably not with your eye) and get notified if the service fails. This is where Knowvent and the Knowvent Topshelf Extensions make it easy.

Below is a sample of a Topshelf service implementation without Knowvent’s Extensions used.

namespace KnowventTopShelf
{
    class Program
    {
        static int _heartbeatIntervalMin = 1;
        public class DailyPricebookExport
        {
            readonly Timer _timer;


            public DailyPricebookExport()
            {
                _timer = new Timer(60000) { AutoReset = true }; //every minute
                _timer.Elapsed += (sender, eventArgs) =>
                    {
                        Console.WriteLine("Lets imagine we exported and sent our customers our Price Book file :-)");

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

        }
        static void Main(string[] args)
        {
            string srvDisplayName = "Daily Pricebook Export";
            var h = HostFactory.Run(x =>                               
            {
                x.Service<DailyPricebookExport>(s =>                    
                {

                    s.ConstructUsing(name => new DailyPricebookExport());
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());         

                });
                x.RunAsLocalSystem();                     
                x.SetDescription("Process for sending out companies price list");
                x.SetDisplayName(srvDisplayName);                    
                x.SetServiceName("DailyPricebookExport");
            });  

        }

    }
}

3 Super Easy Steps to Start Monitoring your Topshelf Service

1. Install the Knowvent Topshelf Extensions via Nuget

Install-Package Knowvent.TopShelfExtensions 

Or install using Nuget Package Manager

Knowvent.TopShelfExtensions

2. Add The extensions using statement to your Topshelf Configuration file

using Knowvent.TopShelfExtensions;

3. Setup Topshelf ServiceConfigurator to use Knowvent Heartbeat Monitoring

                s.KnowventConfig(new KnowventConfig()
                {
                    ClientId = "00000000-0000-0000-0000-000000000000",//Your free ClientId from www.knowvent.com
                    ApiKey = "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx",//Your free API KEY from www.knowvent.com
                    HeartbeatIntervalMin = 10, //The frequency TopShelf will send a heartbeat and the frequency Knowvent will expect one.
                    ProcessIdentifier = new Guid("A81D2B96-8789-41E6-8F25-F529F9EC139C"), //a Unique Guid, you pick!
                    Name = srvDisplayName
                });
                s.WhenStartedForKnowventHeartbeat(tc => tc.Start());
                s.WhenStoppedForKnowventHeartbeat(tc => tc.Stop());     

NOTE: If you haven’t already, you can Sign Up for a free Knowvent Account to get your API key and ClientId.

Start the app and that is it!

Knowvent will automatically register the process, maintain the state of service and will maintain a line of communication to ensure the service is online. If it stops ungracefully for any reason then Knowvent will publish a notification informing you of the issue.

You can also monitor this service via the Knowvent Portal

Comment below and let me know how this worked out for you.

Posted in .NET, Programming and tagged , , , .