接收“Install”方法中使用的参数的Installer类

我有一个inheritance自System.Configuration.Install.Installer类的类,用于安装Windows服务。 它看起来像这样:

[RunInstaller(true)] public class HostInstaller : Installer { private const string _serviceName = "My service name"; private ServiceProcessInstaller _process; private ServiceInstaller _service; public HostInstaller() { _process = new ServiceProcessInstaller(); _process.Account = ServiceAccount.User; _process.Username = "My user name"; // Hard coded _process.Password = "My password"; // Hard coded _service = new ServiceInstaller(); _service.ServiceName = _serviceName; _service.Description = "My service description"; _service.StartType = ServiceStartMode.Automatic; Installers.Add(_process); Installers.Add(_service); } } 

我使用InstallUtil.exe实用程序来安装和卸载此服务,一切正常。

然后我必须接收用户名和密码作为参数(而不是硬编码),所以我更改了类并重写了’Install’方法,并从构造函数中移动了上面提到的代码部分。

 public override void Install(System.Collections.IDictionary stateSaver) { string userName = this.Context.Parameters["UserName"]; if (userName == null) { throw new InstallException("Missing parameter 'UserName'"); } string password = this.Context.Parameters["Password"]; if (password == null) { throw new InstallException("Missing parameter 'Password'"); } _process = new ServiceProcessInstaller(); _process.Account = ServiceAccount.User; _process.Username = userName; _process.Password = password; _service = new ServiceInstaller(); _service.ServiceName = _serviceName; _service.Description = "My service description"; _service.StartType = ServiceStartMode.Automatic; Installers.Add(_process); Installers.Add(_service); base.Install(stateSaver); } 

现在,我正在使用以下方法再次安装该服务:

InstallUtil.exe / UserName = UserName / Password = UserPassword路径…

使用所需的用户名和密码,服务的安装非常有效。 但是,我现在有一个卸载服务的问题。 我正在使用InstallUtil.exe / u,但该服务仍在那里。

我在这里读到一个有用的提示:

如果需要在Install方法中将安装程序实例添加到Installers集合,请确保在卸载方法中对集合执行相同的添加。 但是,如果将安装程序实例添加到自定义安装程序的类构造函数中的安装程序集合中,则可以避免在两种方法中维护集合。

我无法真正理解什么可以解决这个问题。

任何帮助将非常感谢。

埃拉德

*解决方案*

这是我找到的解决方案,确实根据我上面显示的链接:

在Uninstall()方法中,我在Install()方法中执行完全相同的操作(除了订阅AfterInstall事件),然后调用base.Uninstall()方法。

该方法如下所示:

 public override void Uninstall(System.Collections.IDictionary stateSaver) { string userName = this.Context.Parameters["UserName"]; if (userName == null) { throw new InstallException("Missing parameter 'UserName'"); } string password = this.Context.Parameters["Password"]; if (password == null) { throw new InstallException("Missing parameter 'Password'"); } _process = new ServiceProcessInstaller(); _process.Account = ServiceAccount.User; _process.Username = userName; _process.Password = password; _service = new ServiceInstaller(); _service.ServiceName = _serviceName; _service.Description = "My service description"; _service.StartType = ServiceStartMode.Automatic; Installers.Add(_process); Installers.Add(_service); base.Uninstall(stateSaver); } 

当然,这两个方法的通用代码应该包含在一个私有方法中。

现在,为了卸载服务,您应该使用用户名和密码调用InstallUtil.exe,如下所示:

InstallUtil.exe / u / UserName = UserName / Password = UserPassword Path …

祝好运,

埃拉德

从你的链接

如果在派生类中重写Install方法,请确保首先在派生方法中调用基类的Install方法。

你最后调用base.Install(...) 。 尝试在执行任何其他工作之前调用它,如文档所示,并查看是否可以缓解您的问题。