2009-05-13 25 views
0

「myWindowsService」と「SqlService」などの同じマシン上で実行されている別のサービスとの依存関係を設定する方法はありますか?Windowsサービス依存性注入

あなたが「myWindowsService」がインストールされます「SQLサービス」の名前を知らないが、私のサービスは、SQLがすでに実行されていることに依存している場合PROBがある

..

ありがとう

答えて

0

編集: 質問を正しく読まなかった。

既知のインスタンス名を持つ既知のホワイトリストに対してすべてのサービスを列挙することで回避できます(もっとも洗練されたソリューションではありません)。あなたは依存関係を設定することができます(ただし、サービスの名前を知っている必要があります)するManagementObjectを使用して


bool SetServiceDependencies(string serviceName, string[] dependencies) 
    { 
     try 
     { 
      string objPath = string.Format("Win32_Service.Name='{0}'", serviceName); 
      //Uses lazy initialization 
      ManagementObject mmo = new ManagementObject(new ManagementPath(objPath)); 
      //Get properties to check if object is valid, if not then it throws a ManagementException 
      PropertyDataCollection pc = mmo.Properties; 
     } 
     catch (ManagementException me) 
     { //Handle errors 
      if (me.ErrorCode == ManagementStatus.NotFound) { 
       //Service not found 
      } 
      return false; 
     } 
     try 
     { 
      object[] wmiParams = new object[11]; //parameters for Win32_Service mmo object Change-parameters 
      wmiParams[10] = dependencies; 

      //Should we remove dependencies, use array containging 1 empty string 
      if (dependencies == null || dependencies.Length == 0) 
      { 
       wmiParams[10] = new string[] { "" }; 
      } 

      //Change dependencies 
      string returnStatus = mWmiService.InvokeMethod("Change", wmiParams).ToString(); 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
     return true; 
    } 
0

あなたは

sc create <service> 

構文を使用している場合は、あなたが他を供給することができますこのサービスは、インストールされているサービスが起動しているサービスに依存するようにします。

sc create <service> depend= mssqlserver 

これは、ServiceInstallerクラスを使用している場合にも自動的に実行できます。そのコントローラーのプロパティに起動依存関係を定義する領域があります。

あなたは他のサービスの名前を知らないかもしれないが、依存関係を変更することも非常に簡単だと分かっています。