TopShelf [Failure] Stop不能为null

时间:2015-10-07 13:43:02

标签: c# topshelf

Topshelf v3.2.150.0 .Net Framework v4.0。

安装服务时失败;

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)

Program.cs看起来像这样;

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");
        });
    }
}

这是一个命令行程序,它被转换为作为服务运行。按照Main()中入口点的topshelf手动示例。尝试了搜索引擎,但他们只是从Git返回源代码以获取错误消息。该服务继承自ServiceControl。

在服务中,Start()方法已定义为;

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

PollProcess()使用Windows事件触发器来检测添加到目录的文件。

缺少什么配置?

1 个答案:

答案 0 :(得分:11)

当最后一个应该是whenStopped

时,你有两个whenStarted

 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");
    });
}