【问题标题】:How do I delete a service with a custom action?如何使用自定义操作删除服务?
【发布时间】:2015-06-27 00:25:29
【问题描述】:

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

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

在 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 服务? (它们紧紧地捆绑在一起;如果一个不存在,另一个也不应该存在)。

【问题讨论】:

    标签: c# visual-studio-2013 windows-services custom-action setup-deployment


    【解决方案1】:

    那篇文章确实告诉您如何进行卸载 - 它潜伏在“注意主输出出现在安装、提交、回滚和卸载下。”这意味着不仅有安装自定义操作,还有卸载自定义动作。因此,如果您添加了所有自定义操作节点就足够了。

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

    使用 RemovePreviousVersions 升级来升级服务真的很困难,因为它们改变了 VS 2008 中的升级行为。这个线程讨论了问题和解决方案:

    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 Installer 和 MSI 文件已经内置了对它的支持(但 VS 设置没有)。更多信息在这里,向下滚动到使用 Visual Studio 安装服务:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      相关资源
      最近更新 更多