简体   繁体   English

以编程方式重新启动ac#TopShelf服务

[英]Restarting a c# TopShelf service programmatically

I find myself in a terrible situation where I have a topshelf service that uses a c++ library with memory issues. 我发现自己处于一个糟糕的境地,我有一个使用带有内存问题的c ++库的topshelf服务。 Because of this obination of a place I find myself in, I want to call TopShelf to restart the service every now and then during a pause in it's activities for no other reason but to "make the world right again". 由于我发现自己所处的这个地方,我想让TopShelf一次又一次地重新启动服务,暂停它的活动,除了“让世界再次正确”之外别无其他原因。

Are there any TopShelf APIs that allow for this? 是否有任何TopShelf API允许这样做? I cannot seem to find any in the documentation. 我似乎无法在文档中找到任何内容。

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

            });

I am sure Topshelf doesn't support this so you would have to do this from code yourself. 我确信Topshelf不支持这个,所以你必须自己从代码中做到这一点。

Take a look at the ServiceController class. 看一下ServiceController类。

Worst case you could have a second simple topshelf installer that manages your current service and restarts it? 最坏的情况是你可以有第二个简单的topshelf安装程序来管理你当前的服务并重新启动它? (bit dirty i know) (我知道有点脏)

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

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