2013-03-28 51 views
2

我有一個類從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 /用戶名=用戶名/密碼= userPassword的路徑...

服務的安裝工作很好,有所需的用戶名和密碼。但是,我現在確實有一個卸載服務的問題。我正在使用InstallUtil.exe/u,但該服務仍然存在。

我讀here一個有用的技巧:

如果您需要安裝實例添加到安裝程序集合中的安裝方法時,一定要在卸載方法執行相同的添加到集合中。但是,如果將安裝程序實例添加到定製安裝程序的類構造器中的Installers集合中,則可以避免在兩種方法中維護集合。

我真的不明白什麼可以解決這個問題。

任何幫助將不勝感激。

埃拉德

回答

1

*解決方案*

這裏是我找到了解決辦法,確實是按照我所顯示上面的鏈接:

在我的安裝做同樣的像卸載()方法()方法(除了訂閱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 /用戶名=用戶名/密碼= userPassword的路徑...

祝你好運,

埃拉德

0

從你的鏈接

如果覆蓋在派生類中的方法安裝時,一定要首先調用基類的安裝方法,在派生方法。

您正在致電base.Install(...)最後。請按照文檔建議,在做任何其他工作之前嘗試調用它,看看是否可以緩解您的問題。

+0

感謝傑克。該文檔還說 - 「Install方法調用此實例的Installers屬性中包含的每個安裝程序的Install方法」。它看起來像在嘗試在base.Install(...)之後設置用戶名和密碼太晚。我會嘗試這個並更新。 – 2013-03-28 00:56:02

相關問題