2013-03-06 1 views
8

いくつかの設定をWindowsサービスに渡すためにコマンドライン引数を使用しています(異なるコマンドラインのインスタンスはほとんどありません)。TopShelfでサービスのコマンドラインオプションを指定する方法

私のコードは次のようになります。私は、私が使用してサービスをインストールすると

HostFactory.Run(x =>         
{ 
    x.Service<MyHost>(s => 
    {      
     s.ConstructUsing(name => new MyHost()); 
     s.WhenStarted(tc => tc.Start());    
     s.WhenStopped(tc => tc.Stop());    
    }); 
    x.AddCommandLineDefinition("sqlserver", v => sqlInstance = v); 
}); 

myhost.exe install -sqlserver:someinstance 
残念ながら

sqlserverコマンドラインオプションを位相をインストールして、に行っていないだけで提供されていますサービスのパラメータ。だから私がサービスを実行するとき、私は必要なパラメータ値を取得しません。

TopShelfによって開始されたサービスのコマンドラインを変更する方法はありますか?

答えて

2

にこのスレッドを確認します。私はこれらのパラメータをexeの隣の設定に保存することでこれを回避します。したがって、ユーザーはこれを行うことができます:

次に、(いずれかのConfigurationManagerをSystem.IO.Fileを使用して、または使用して)

service.exe run /sqlserver:connectionstring 

をし、アプリがファイルできるように、ConnectionStringを保存し、ユーザーがこれを行う場合

service.exe run 

サービスがWindowsサービスとして実行される場合、アプリケーションはconfigからロードされます。

3

私は同様の要件がありますが、残念なことにファイルにコマンドラインパラメータを格納することはオプションではありませんでした。

免責条項:このアプローチは、Windows

にのみ有効ですまず私は、コマンドラインを含めるようにサービスののImagePath Windowsレジストリエントリを更新AddCommanLineParameterToStartupOptionsAfter Install Action

x.AfterInstall(
    installSettings => 
    { 
     AddCommandLineParametersToStartupOptions(installSettings); 
    }); 

を追加しましたパラメーター。

TopShelfはこのステップの後にパラメータを追加して、servicenameinstanceの重複を避けます。あなたはそれ以上のものを除外したいかもしれませんが、私の場合はこれで十分でした。

 private static void AddCommandLineParametersToStartupOptions(InstallHostSettings installSettings) 
    { 
      var serviceKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
       $"SYSTEM\\CurrentControlSet\\Services\\{installSettings.ServiceName}", 
       true); 

      if (serviceKey == null) 
      { 
       throw new Exception($"Could not locate Registry Key for service '{installSettings.ServiceName}'"); 
      } 

      var arguments = Environment.GetCommandLineArgs(); 

      string programName = null; 
      StringBuilder argumentsList = new StringBuilder(); 

      for (int i = 0; i < arguments.Length; i++) 
      { 
       if (i == 0) 
       { 
        // program name is the first argument 
        programName = arguments[i]; 
       } 
       else 
       { 
        // Remove these servicename and instance arguments as TopShelf adds them as well 
        // Remove install switch 
        if (arguments[i].StartsWith("-servicename", StringComparison.InvariantCultureIgnoreCase) | 
         arguments[i].StartsWith("-instance", StringComparison.InvariantCultureIgnoreCase) | 
         arguments[i].StartsWith("install", StringComparison.InvariantCultureIgnoreCase)) 
        { 
         continue; 
        } 
        argumentsList.Append(" "); 
        argumentsList.Append(arguments[i]); 
       } 
      } 

      // Apply the arguments to the ImagePath value under the service Registry key 
      var imageName = $"\"{Environment.CurrentDirectory}\\{programName}\" {argumentsList.ToString()}"; 
      serviceKey.SetValue("ImagePath", imageName, Microsoft.Win32.RegistryValueKind.String); 
}