接收“安装"方法中使用的参数的安装程序类 [英] Installer class that receives parameters used in the 'Install' method

查看:90
本文介绍了接收“安装"方法中使用的参数的安装程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从System.Configuration.Install.Installer类继承的类,用于安装Windows Service.看起来像这样:

I have a class inheriting from System.Configuration.Install.Installer class, and used to install a Windows Service. It looks like this:

[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实用工具来安装和卸载此服务,并且一切正常.

I have used the InstallUtil.exe utility for installing and uninstalling this service, and everything is working just fine.

然后我必须接收用户名和密码作为参数(而不是硬编码),因此我更改了类并覆盖了'Install'方法,并从构造函数中移走了上面提到的代码部分.

Then I has to receive the user name and password as parameters (rather than hard coded), so I have changed the class and overridden the 'Install' method, and moved code section mentioned above from the constructor.

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);
}

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

Now, I'm installing the service again, using this:

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

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

使用所需的用户名和密码,该服务的安装运行良好.但是,我现在确实存在卸载服务的问题.我正在使用InstallUtil.exe/u,但是该服务仍然存在.

The installation of the service is working great, with the desired user name and password. However, I do have now a problem with un-installing the service. I'm using the InstallUtil.exe /u, but the service is still there.

我在此处阅读了有用的提示:

I read here a useful tip:

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

If you need to add installer instances to the Installers collection in the Install method, be sure to perform the same additions to the collection in the Uninstall method. However, you can avoid maintaining the collection in both methods if you add installer instances to the Installers collection in the class constructor for your custom installer.

我真的不明白什么可以解决这个问题.

I can't really understand what can solve this problem.

我们将不胜感激.

没事

推荐答案

*解决方案*

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

Here is the solution I found, indeed according the link I have displayed above:

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

In the Uninstall() method I'm doing exactly the same like in the Install() method (besides subscribing the AfterInstall event), and then calling the base.Uninstall() method.

方法如下:

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);
}

当然,这两种方法的通用代码都应该包装在一个私有方法中.

Of course that the common code for both the methods should be wrapped into a single private method.

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

Now, in order to uninstall the service you should call InstallUtil.exe with the user name and the password, like this:

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

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

祝你好运

没事

这篇关于接收“安装"方法中使用的参数的安装程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆