我有一個類從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集合中,則可以避免在兩種方法中維護集合。
我真的不明白什麼可以解決這個問題。
任何幫助將不勝感激。
埃拉德
感謝傑克。該文檔還說 - 「Install方法調用此實例的Installers屬性中包含的每個安裝程序的Install方法」。它看起來像在嘗試在base.Install(...)之後設置用戶名和密碼太晚。我會嘗試這個並更新。 – 2013-03-28 00:56:02