Search code examples
c#visual-studiowindows-servicestopshelf

How to integrate topshelf to an existing windows service project?


I want to be able to use the TopShelf debugging abilities of my service in Visual Studio.

A lot of the examples and documentation out there refer to creating a Windows Console project in Visual Studio first, and then adding TopShelf, OWIN, etc

However, in my case I already have a perfectly good and working Windows Service project called QShipsService.sln, etc... and it uses a simple Connected Service (admittedly to old SOAP legacy services).

Can someone please direct me or provide an example of how to use TopShelf, with an existing non-Console like project?


Solution

  • I found my own solution...

    The assumption I made was the default Windows Service project defaulting to wanting to register the program as a service and kick off the OnOpen() and OnClose() methods, once the service is running.

    In my case I wanted to re-use an existing service that was based on a Timer(), and it would kick in every 4 hours to call a SOAP call and return some data. What I didn't realise was the ServiceConfigurator was trying to call its own Open() and Close() methods.

    So I commented out the OnOpen and OnClose methods and allowed the configurator to call my worker process via Open() method instead, which is what I was meant to have done the first time!

    For the noobs out there like me, here is the code...

    //using System.ServiceProcess;
    using Topshelf;
    
    namespace QShipsService
    {
        static class Program
        {
            static void Main(string[] args)
            {
                HostFactory.Run(
                    configure =>
                    {
                        configure.Service<QShipsService.QshipsService>(
                            service =>
                            {
                                service.ConstructUsing(s => new QShipsService.QshipsService());
                                service.WhenStarted(s => s.QStart());
                                service.WhenStopped(s => s.QStop());
                            });
    
                        //Setup Account that window service use to run.
                        configure.RunAsLocalSystem();
    
                        //add details and names about the service
                        configure.SetServiceName("QshipsService");
                        configure.SetDisplayName("QshipsService");
                        configure.SetDescription("QshipsService Windows Service to extract data from the QSHIPS SOAP service. Data is recorded and maintained inside the SPOT's database in POT-DB.");
                    });
    
    
                //## USE THIS IF WE'RE NOT USING TOPSHELF !! ##
                //    //this loads and starts the QshipsService (see QshipsService.cs program)
                //    ServiceBase[] ServicesToRun;
                //    ServicesToRun = new ServiceBase[]
                //    {
                //        new QShipsService.QshipsService()
                //    };
                //    ServiceBase.Run(ServicesToRun);
            }
        }
    }