Search code examples
c#topshelf

Topshelf not hitting OnStart Method


I'm trying to build my first topshelf based service. I'm trying to follow the pattern in the quick start http://docs.topshelf-project.com/en/latest/configuration/quickstart.html

    static void Main(string[] args)
    {
        var h =  HostFactory.Run(x =>
                            {
                                ConfHost(x);
                            });
    }

    private static void ConfHost (Topshelf.HostConfigurators.HostConfigurator x  )
    {
        x.Service<JobRunner>(s =>
        {
            ConfService(s);
        });
        x.RunAsLocalSystem();
        x.StartAutomatically();
        x.SetDescription("topshelf thing");
        x.SetDisplayName("displayname ");
        x.SetServiceName("svc name");
    }

    private static void ConfService(ServiceConfigurator<JobRunner> s)
    {
        s.ConstructUsing(name => new JobRunner());
        s.WhenStarted(bt => bt.OnStart());
        s.WhenStopped(bt => bt.OnStop());
    }

This code runs straight through and terminates without ever hitting the Onstart method on JobRunner even if I put a break point on the first line.


Solution

  • In the hope that someone else can learn from my mistake, the console output actually gives the reason you need to step through carefully or put a Console.Read() at the end to see it

    ConfigurationException: The service was not properly configured: [Failure] Name must not contain whitespace, '/' or '\' characters