简体   繁体   English

在Topshelf服务中重新启动self

[英]Restarting self in a Topshelf service

I'm using Topshelf to host a Windows service. 我正在使用Topshelf来托管Windows服务。 I am looking to get the hosted service to call to have itself restarted upon certain events. 我希望让托管服务调用以在某些事件时重新启动。 I was wondering how to achieve this? 我想知道如何实现这一目标?

Thanks, Ben 谢谢,本

You can use the service manager if you know the service name to call restart. 如果您知道要重新调用的服务名称,则可以使用服务管理器。 It may or may not work called from itself. 它本身可能会或可能不会起作用。 This isn't something Topshelf exposes, so you're on your own to do it. 这不是Topshelf所暴露的,所以你可以自己动手去做。

call Environment.Exit(1); 调用Environment.Exit(1); when u want restart service 当你想重启服务

then in HostFactory add Enable ServiceRecovery 然后在HostFactory中添加启用ServiceRecovery

HostFactory.Run(configure =>
            {
                configure.Service((ServiceConfigurator<Service> service) =>
                {

                    service.WhenStarted(s => s.Start());
                    service.WhenStopped(s => s.Stop());
                });

                //Setup Account that window service use to run.  
                configure.RunAsNetworkService();
                configure.SetServiceName("ServiceName");
                configure.SetDisplayName("ServiceName");
                configure.SetDescription("Description");
                configure.StartAutomaticallyDelayed();
                configure.EnableServiceRecovery(recoveryOption =>
                {
                    recoveryOption.RestartService(0);
                });

            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM