Topshelf:成功安装服务后,安装命令不会返回

Sud*_*hra 5 .net c# topshelf

注意:我没有执行类似于Topshelf安装程序要求我按Enter两次的任何操作-为什么?

服务等级(有趣的部分):

public class ServiceCore
{
    public ServiceCore(ServiceRuntimeConfiguration serviceRuntimeConfiguration)
    {
        _runningTasks = new List<Task>();
    }

        public bool Start(HostControl hostControl)
        {
            _hostControl = hostControl;
            _messageProcessor.Start(); // Starts a System.Threading.Tasks.Task
            StartListener(); // starts a System.Threading.Tasks.Task
            return true;
        }
}
Run Code Online (Sandbox Code Playgroud)

Program.cs:

Host host = HostFactory.New(configurator =>
{

configurator.UseNLog();

// Configure core service
configurator.Service<ServiceCore>(svc =>
{
    svc.ConstructUsing(theService => new ServiceCore(_serviceRuntimeConfiguration));
    svc.WhenStarted((svc, hostControl) => svc.Start(hostControl));
    svc.WhenStopped((svc, hostControl) => svc.Stop(hostControl));
});

// Configure recovery params
configurator.EnableServiceRecovery(recoveryConfigurator =>
{
    recoveryConfigurator.RestartService(0);
    recoveryConfigurator.OnCrashOnly();
    recoveryConfigurator.SetResetPeriod(1);
});

// Execute HostConfigurator
host.Run();
}
Run Code Online (Sandbox Code Playgroud)

问题

当我这样做时:

MyService.exe install --manual --localsystem
Run Code Online (Sandbox Code Playgroud)

该服务可以正常安装,但是该命令永远不会返回:

运行事务处理的安装。

开始安装的安装阶段。正在安装服务NotificationEngine.Main ...已成功安装服务NotificationEngine.Main。

安装阶段已成功完成,并且提交阶段已开始。

提交阶段已成功完成。

事务处理安装已完成。

^ C(我必须按CTRL + C)

我应该怎么做才能完成install命令然后返回?

注意如果我运行帮助,则可以观察到相同的行为(即显示帮助,但命令不返回):

MyService.exe help
Run Code Online (Sandbox Code Playgroud)

Tra*_*vis 2

一般来说,这意味着您没有释放对某些资源的控制,并且进程无法干净地退出。不过,这个东西很复杂,所以很难说清楚。

我会尝试的一些事情

  • 安装/CTRL+C 后执行时会发生什么MyService start?我假设它也会阻止,因为 help 会阻止。
  • 检查日志记录,您是否启用了任何日志记录?是否存在文件争用或权限问题?
  • Main()你的入口点还有什么作用?之后是在做某事host.Run()吗?上面的代码看起来像是您从该对象的构造中调用它,但我认为这是不好的剪切粘贴。
  • ConstructUsing确保在和When*回调被触发之前没有初始化资源。

之后,我会将其发送到我们的邮件列表:https://groups.google.com/forum/#!forum/ topshelf-discuss 。