在Topshelf服务中重新启动self

时间:2013-11-06 13:16:20

标签: c# .net topshelf

我正在使用Topshelf来托管Windows服务。我希望让托管服务调用以在某些事件时重新启动。我想知道如何实现这个目标?

谢谢, 本

2 个答案:

答案 0 :(得分:1)

如果您知道要重新调用的服务名称,则可以使用服务管理器。它本身可能会或可能不会起作用。这不是Topshelf所暴露的,所以你可以自己动手去做。

答案 1 :(得分:1)

当您要重新启动服务时致电Environment.Exit(1);

然后在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);
                });

            });