TopShelf [Failure] Stop 不能为空

Posted

技术标签:

【中文标题】TopShelf [Failure] Stop 不能为空【英文标题】:TopShelf [Failure] Stop must not be null 【发布时间】:2015-10-07 13:43:02 【问题描述】:

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】:

当最后一个应该是 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");
    );

【讨论】:

以上是关于TopShelf [Failure] Stop 不能为空的主要内容,如果未能解决你的问题,请参考以下文章

Topshelf的使用

Quartz.NET总结如何根据自己需要配置Topshelf 服务

如何检测 Topshelf 是不是在控制台模式下运行

TopShelf+Quartz.net实现基于window服务的定时调度

使用Topshelf创建Windows服务

如何使用Topshelf管理Windows服务