Topshelf 在安装前删除服务的现有版本

问题描述

我正在使用 TopShelf 启动 Windows 服务。如果设备上正在运行该服务,我想在安装最新版本的服务之前删除该服务的现有版本。似乎我可以使用 BeforeInstall 方法,但我不确定这样做的正确方法是什么。

 public static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                x.Service<RestService>(s =>
                {
                    s.ConstructUsing(() => new RestService());
                    s.WhenStarted(rs => rs.Start());
                    s.WhenStopped(rs => rs.Stop());
                    s.WhenShutdown(rs => rs.Stop());
                });
                x.RunAsLocalSystem();
                x.StartAutomatically();

                x.SetServiceName("CampaignAppApiService");
                x.SetdisplayName("CampaignAppApiService");
                x.SetDescription("This is a self-hosted web api rest service for Campaign App");
                x.BeforeInstall() //Remove the existing "CampaignAppApiService" service if there is one,so that the latest version get installed 
            });
        }

我的最终目标是通过运行包含以下命令的批处理脚本来简化安装和启动服务的过程。

.\RestApi.exe install
.\RestApi.exe start

我的另一种方法是使用 sc stop 和 sc delete 手动删除服务。但它并没有像我预期的那样工作。

sc stop CampaignAppApiService
sc delete CampaignAppApiService   //Returns Service marked as deleted
cd %~dp0
.\RestApi.exe install
.\RestApi.exe start //Return error message
//Error 1058 The Service Cannot Be Started Either Because it is disabled or Because it has No Enabled Devices Associated
pause

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)