微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。