如何使用自定义操作删除服务

本文关键字:删除 服务 操作 自定义 何使用 | 更新日期: 2023-09-27 17:59:55

根据我在这里的发现,我能够构建一个安装和部署项目,该项目可以正确安装我正在使用的服务。不幸的是,当我更新应用程序(可能还有它附带的服务)时,关于如何卸载服务的信息绝对为零。

如何将卸载程序添加到服务项目安装程序中?

在ProjectInstaller类中,我添加了以下代码:

[System.Security.Permissions.SecurityPermission( System.Security.Permissions.SecurityAction.Demand )]
public override void Uninstall( IDictionary savedState ) {
    base.Uninstall( savedState );
    try { this.TRSInstaller.Uninstall( savedState ); } catch { }
}

我不得不这样做,因为在没有try/catch的情况下尝试它会导致服务被正确删除,但出现了一个异常,不允许我删除整个程序,所以我也无法安装新版本。

但是,这不会删除该服务。

这是VS抛出的ProjectInstaller类的代码:

namespace TriviaRetriever {
    partial class ProjectInstaller {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose( bool disposing ) {
            if ( disposing && ( components != null ) ) {
                components.Dispose( );
            }
            base.Dispose( disposing );
        }
        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent( ) {
            this.TRProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.TRSInstaller = new System.ServiceProcess.ServiceInstaller();
            // 
            // TRProcessInstaller
            // 
            this.TRProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.TRProcessInstaller.Password = null;
            this.TRProcessInstaller.Username = null;
            // 
            // TRSInstaller
            // 
            this.TRSInstaller.Description = "Service for automatically downloading your trivia.";
            this.TRSInstaller.DisplayName = "Trivia Retriever";
            this.TRSInstaller.ServiceName = "TriviaRetriever";
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.TRProcessInstaller,
            this.TRSInstaller});
        }
        #endregion
        private System.ServiceProcess.ServiceProcessInstaller TRProcessInstaller;
        private System.ServiceProcess.ServiceInstaller TRSInstaller;
    }
}
namespace TriviaRetriever {
    [RunInstaller( true )]
    public partial class ProjectInstaller : System.Configuration.Install.Installer {
        public ProjectInstaller( ) {
            InitializeComponent( );
        }
        [System.Security.Permissions.SecurityPermission( System.Security.Permissions.SecurityAction.Demand )]
        public override void Uninstall( IDictionary savedState ) {
            base.Uninstall( savedState );
            try { this.TRSInstaller.Uninstall( savedState ); } catch { }
        }
    }
}

我需要对此做些什么才能在卸载应用程序时成功删除windows服务?(它们紧紧地捆在一起;如果其中一个不在场,另一个也不应该在场)。

如何使用自定义操作删除服务

这篇文章确实告诉了你如何进行卸载-它隐藏在短语"注意,主输出显示在安装、提交、回滚和卸载下"中。这意味着不仅有安装自定义操作,还有卸载自定义操作。因此,如果您已经添加了所有的自定义操作节点,就足够了。

此外,您不需要从现有的卸载中调用卸载,因为您使用的是基于哪个库的卸载方法。卸载将卸载服务。请注意,卸载不会尝试停止服务,因此可能需要重新启动才能完全卸载服务。

使用RemovePreviousVersions升级来升级服务确实很困难,因为它们在VS2008中更改了升级行为。这个线程通过问题和解决方案:

https://social.msdn.microsoft.com/Forums/en-US/b2d1bd22-8499-454e-9cec-1e42c03e2557/how-to-deal-with-error-1001-the-specified-service-already-exists-when-install-a-service-using?forum=winformssetup

最好不要使用代码来安装和卸载服务。这是完全没有必要的,因为Windows安装程序和MSI文件有内置的支持(但VS设置没有)。更多信息请点击此处,向下滚动至使用Visual Studio安装服务:

http://www.installsite.org/pages/en/msi/tips.htm