编程语言
首页 > 编程语言> > c# – Topshelf:成功安装服务后,install命令不会返回

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

作者:互联网

注意:我没有做类似于Topshelf installer requires me to press enter twice – why?的任何事情

服务类(有趣的部分):

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

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

问题

当我这样做:

MyService.exe install --manual --localsystem

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

Running a transacted installation.

Beginning the Install phase of the installation. Installing service
NotificationEngine.Main… Service NotificationEngine.Main has been
successfully installed.

The Install phase completed successfully, and the Commit phase is
beginning.

The Commit phase completed successfully.

The transacted install has completed.

^C (I have to press CTRL+C)

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

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

MyService.exe help

解决方法:

通常,这意味着您不释放某些资源的控制权,并且该流程无法彻底退出.然而,这些东西很复杂,所以很难说肯定.

我会尝试一些事情

>在安装/ CTRL C之后执行MyService启动时会发生什么?我假设它也会因为帮助而阻塞.
>检查日志记录,您是否已启用?是否存在文件争用或权限问题?
>您的Main()入口点还有什么作用?它是在host.Run()之后做的吗?上面的代码使得它看起来就像你在该对象的构造中调用它,但我认为它是糟糕的切割粘贴.
>确保在ConstructUsing和When *回调被触发之前没有初始化资源.

在此之后,我会把它带到https://groups.google.com/forum/#!forum/topshelf-discuss的邮件列表.

标签:c,net,topshelf
来源: https://codeday.me/bug/20190702/1356007.html