2012-01-23 10 views
1

私はWCFサーバーとクライアントアプリケーションを作成しました。サーバー側では、アプリケーションがいつ実行されているかを知る必要があります。WCFサービスの可用性

私は、WCFのメソッドを作成し、このようにそれを呼び出そうとしている:

var myEndpoint = new EndpointAddress(url); //I generate HTTP url to endpoint here 
var myBinding = new WSHttpBinding { Security = { Mode = SecurityMode.None } }; 

var myChannelFactory = new ChannelFactory<IMycontract>(myBinding, myEndpoint); 

ISqlExpressSyncContract client = null; 

try 
{ 
    client = myChannelFactory.CreateChannel(); 
    client.IsAvailable(); 
    ((ICommunicationObject)client).Close(); 
    return true; 
} 
catch (Exception) 
{ 
    if (client != null) 
    { 
     ((ICommunicationObject)client).Abort(); 
    } 

    return false; 
} 

コードは動作しますが、それは利用できないときにタイムアウトが長すぎます。私はここにhttp://msdn.microsoft.com/en-us/magazine/ee335779.aspxを提供するようなUDP発見を使ってみました。しかし、クライアントが利用できない場合にも同じ問題があります。

各クライアントを高速にpingしてその可用性ステータスを確認するロジックを実装する最も良い方法は何ですか?

答えて

3

はそうのようなタイムアウト値を下げてみてください。

myBinding.OpenTimeout = TimeSpan.FromSeconds(5); 

このタイムアウト値は、通信チャネルを開くためのものです。必要に応じて、SendTimeout,ReceiveTimeoutCloseTimeoutも調整することができます。これらのすべてはバインディングオブジェクトにも適用されます。

0

私は 'System.ServiceProcess'名前空間でServiceControllerクラスを使用していますが、これはきれいに動作しています。

try 
{ 
    ServiceController sc = new ServiceController("Service Name", "Computer IP Address"); 
    Console.WriteLine("The service status is currently set to {0}", 
     sc.Status.ToString()); 

    if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || 
     (sc.Status.Equals(ServiceControllerStatus.StopPending))) 
    { 
     Console.WriteLine("Service is Stopped, Ending the application..."); 
     Console.Read(); 
     EndApplication(); 
    } 
    else 
    { 
     Console.WriteLine("Service is Started..."); 
    } 
} 
catch (Exception) 
{ 
    Console.WriteLine("Error Occurred trying to access the Server service..."); 
    Console.Read(); 
    EndApplication(); 
} 
関連する問題