安装程序类,它接收'Install'方法
本文关键字:Install 方法 程序 安装 | 更新日期: 2023-09-27 18:06:18
我有一个继承自System.Configuration.Install.Installer类的类,用于安装Windows Service。它看起来像这样:
[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方法中对集合执行相同的添加操作。但是,如果您在自定义安装程序的类构造函数中将安装程序实例添加到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路径…
祝你好运,
兰德
从你的链接
如果您在派生类中重写Install方法,请确保首先在派生方法中调用基类的Install方法。
您最后呼叫base.Install(...)
。在做文档建议的任何其他工作之前尝试调用它,看看是否可以缓解您的问题。