2011-02-01 108 views
13

我正在使用InstallUtil安裝我的服務,我只是無法弄清楚如何爲它指定啓動參數!使用InstallUtil安裝具有啓動參數的Windows服務

這裏是我的安裝程序子類:

[RunInstaller(true)] 
public class ServerHostInstaller : Installer 
{ 
    private ServiceInstaller m_serviceInstaller; 
    private ServiceProcessInstaller m_serviceProcessInstaller; 
    private static string s_usage = "Usage:\ninstallutil /i /username=<user_name> /password=<user_password> NCStub.Server.Host.exe"; 

    public ServerHostInstaller() 
    { 
    m_serviceInstaller = new ServiceInstaller(); 
    m_serviceInstaller.ServiceName = Program.ServiceName; 
    m_serviceInstaller.DisplayName = Program.ServiceName; 
    m_serviceInstaller.StartType = ServiceStartMode.Automatic; 

    m_serviceProcessInstaller = new ServiceProcessInstaller(); 
    m_serviceProcessInstaller.Account = ServiceAccount.User; 

    Installers.Add(m_serviceInstaller); 
    Installers.Add(m_serviceProcessInstaller); 
    } 

    public override void Install(IDictionary stateSaver) 
    { 
    base.Install(stateSaver); 

    string userName = this.Context.Parameters["username"]; 
    if (userName == null) 
    { 
     Console.WriteLine(s_usage); 
     throw new InstallException("Missing parameter 'username'"); 
    } 

    string userPass = this.Context.Parameters["password"]; 
    if (userPass == null) 
    { 
     Console.WriteLine(s_usage); 
     throw new InstallException("Missing parameter 'password'"); 
    } 

    m_serviceProcessInstaller.Username = userName; 
    m_serviceProcessInstaller.Password = userPass; 
    } 
} 

任何人都可以表明我怎麼指定服務的啓動參數?

回答

15

找到它。

我已經重寫像這樣的安裝方法:

public override void Install(IDictionary stateSaver) 
{ 
    string userName = this.Context.Parameters["username"]; 
    if (userName == null) 
    { 
    Console.WriteLine(s_usage); 
    throw new InstallException("Missing parameter 'username'"); 
    } 

    string userPass = this.Context.Parameters["password"]; 
    if (userPass == null) 
    { 
    Console.WriteLine(s_usage); 
    throw new InstallException("Missing parameter 'password'"); 
    } 

    m_serviceProcessInstaller.Username = userName; 
    m_serviceProcessInstaller.Password = userPass; 

    var path = new StringBuilder(Context.Parameters["assemblypath"]); 
    if (path[0] != '"') 
    { 
    path.Insert(0, '"'); 
    path.Append('"'); 
    } 
    path.Append(" --service"); 
    Context.Parameters["assemblypath"] = path.ToString(); 
    base.Install(stateSaver); 
} 

雖然,我給預定義的命令行參數(--service)時,代碼是容易適應通過真實命令行參數,只是使用相同的模式傳遞用戶名密碼參數。

+2

如果您將處理程序附加到服務安裝程序對象的BeforeInstall事件而不是覆蓋Install方法,則此方法也適用。 – 2012-06-14 04:12:33

4

我知道這是一箇舊帖子,但認爲我會張貼我的迴應。我使用BeforeInstall事件在.net 4服務中執行了此操作。

的ServiceProcessInstaller的BeforeInstall事件:

private void serviceProcessInstaller1_BeforeInstall(object sender, InstallEventArgs e) 
{ 
    System.ServiceProcess.ServiceProcessInstaller installer = sender as System.ServiceProcess.ServiceProcessInstaller; 

    if (installer != null) 
    { 
     //Get the existing assembly path parameter 
     StringBuilder sbPathWIthParams = new StringBuilder(installer.Context.Parameters["assemblypath"]); 

     //Wrap the existing path in quotes if it isn't already 
     if (!sbPathWIthParams[0].Equals("\"")) 
     { 
      sbPathWIthParams.Insert(0, "\""); 
      sbPathWIthParams.Append("\""); 
     } 

     //Add desired parameters 
     sbPathWIthParams.Append(" test"); 

     //Set the new path 
     installer.Context.Parameters["assemblypath"] = sbPathWIthParams.ToString(); 
    } 
} 

安裝的服務如下所示: enter image description here

它執行罰款,我可以從服務的主要功能內檢查該參數。

相關問題