Search code examples
c#topshelf

TopShelf [Failure] Stop must not be null


Topshelf v3.2.150.0 .Net Framework v4.0.

On installing the service it fails;

Topshelf.HostFactory Information: 0 : Configuration Result:
[Success] Name ZipPack
[Success] Description 9 Angle Zip Refresh
[Success] ServiceName ZipPack
Topshelf.HostConfigurators.HostConfiguratorImpl Information: 0 : Topshelf v3.2.150.0, .NET Framework v4.0.30319.42000
Topshelf.HostFactory Error: 0 : An exception occurred creating the host, Topshelf.HostConfigurationException: The service was not properly configured: 
[Failure] Stop must not be null
   at Topshelf.Configurators.ValidateConfigurationResult.CompileResults(IEnumerable`1 results)
   at Topshelf.ServiceExtensions.<>c__DisplayClasse`1.<CreateServiceBuilderFactory>b__d(HostSettings x)
   at Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost()
   at Topshelf.HostFactory.New(Action`1 configureCallback)
Topshelf.HostFactory Error: 0 : The service terminated abnormally, Topshelf.HostConfigurationException: The service was not properly configured: 
[Failure] Stop must not be null
   at Topshelf.Configurators.ValidateConfigurationResult.CompileResults(IEnumerable`1 results)
   at Topshelf.ServiceExtensions.<>c__DisplayClasse`1.<CreateServiceBuilderFactory>b__d(HostSettings x)
   at Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost()
   at Topshelf.HostFactory.New(Action`1 configureCallback)
   at Topshelf.HostFactory.Run(Action`1 configureCallback)

The Program.cs looks like this;

public static class Program
{
    public static void Main(string[] args)
    {
        HostFactory.Run(x =>
        {
            x.Service<ZipPackService>(s =>
            {
                s.ConstructUsing(name => new ZipPackService(new ServiceRepository(new FileHelper())));
                s.WhenStarted((tc, hostControl) => tc.Start(hostControl));
                s.WhenStarted((tc, hostControl) => tc.Stop(hostControl));
            });
            x.RunAsLocalSystem();
            x.StartAutomaticallyDelayed();
            x.SetDescription("9 Angle Zip Refresh");
            x.SetDisplayName("ZipPack");
            x.SetServiceName("ZipPack");
        });
    }
}

This was a command line program which is being converted to run as a service. Following the topshelf manual example for the entry point in Main(). Have tried search engines but they are only returning the source from Git for the error message. The service inherits from ServiceControl.

Within the service, the Start() method has been defined as;

    public bool Start(HostControl hostControl)
    {
        PollProcess();
        return true;
    }

The PollProcess() uses windows event triggers to detect files being added to a directory.

What configuration is missing?


Solution

  • You have two whenStarted when the last one should be whenStopped

    ie

     public static void Main(string[] args)
    {
        HostFactory.Run(x =>
        {
            x.Service<ZipPackService>(s =>
            {
                s.ConstructUsing(name => new ZipPackService(new ServiceRepository(new FileHelper())));
                s.WhenStarted((tc, hostControl) => tc.Start(hostControl));
                s.WhenStopped((tc, hostControl) => tc.Stop(hostControl));
            });
            x.RunAsLocalSystem();
            x.StartAutomaticallyDelayed();
            x.SetDescription("9 Angle Zip Refresh");
            x.SetDisplayName("ZipPack");
            x.SetServiceName("ZipPack");
        });
    }