2011-02-01 18 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

この方法は、Installメソッドをオーバーライドするのではなく、サービスインストーラーオブジェクトのBeforeInstallイベントにハンドラーをアタッチする場合にも機能します。 –

+2

実際にはそうではありません。それはしなければならないと私はかなり慣れていたが、私はちょうどチェックしたとそれはしていない。オーバーライドバージョンに固執する。 –

+0

インストーラに資格情報を渡すのと同じ解決策があります。問題は、ログファイルにもあなたの資格情報が含まれていることです。これは私の意見では大きな問題です。ログファイルに「影響を受けるパラメータはありますか」の書き込みを無効にする方法はありますか?私は完全なログファイルを無効にしたくない! – flayn

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

それは罰金実行し、私はサービスの主な機能の中からパラメータを調べることができます。

関連する問題