Search code examples
c#windows-servicestopshelf

Topshelf C# Windows Service - Cannot pass HostControl


I am trying to pass a HostControl instance to the start method of a topshelf service but I am getting the following compiler errors:

  • Not all code paths return a value in lambda expression of type
  • 'HostSettings' does not contain a definition for 'ConstructUsing'
  • 'HostSettings' does not contain a definition for 'WhenStarted' and no extension method 'WhenStarted' accepting a first argument of type 'HostSettings' could be found (are you missing a using directive or an assembly reference?)

I have implement the interface 'ServiceControl' for the EventBroker class.

using Topshelf;
namespace Sample
{
  class Program
  {
    static void Main(string[] args)
    {
        HostFactory.Run(x =>
        {
            x.Service<EventBroker>(s =>
            {
                s.ConstructUsing(name => new EventBroker());
                s.WhenStarted((tc, hostControl) => tc.Start(hostControl));
                s.WhenStopped(tc => tc.Stop());
            });
            x.RunAsLocalSystem();

Am I missing some an assembly reference or something else? I am using TopShelf v3.3.154.0. Without the 'hostControl' it is working fine.


Solution

  • After some trouble shooting, I found that the 'ServiceControl' wasn't implemented correctly. The Stop/Start methods of the EventBroker didn't have a return type (bool).

    Now, with the following Stop/Start Methods it is working.

        bool Start(HostControl hostControl)
        {
            return true;
        }
    
        bool Stop(HostControl hostControl)
        {
            return true;
        }