Reputation: 14088
I have created WCF server and client applications. On the server side I need to know when the application is running.
I have created a WCF method and try to call it like this:
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;
}
The code works but the timeout is too long when it isn't available. I tried using UDP discovery like provide here http://msdn.microsoft.com/en-us/magazine/ee335779.aspx. But it has the same problem when the client is not available.
What the best way to implement logic to fast ping each client and check their availability status?
Upvotes: 1
Views: 400
Reputation: 29
I'm using the ServiceController class in the 'System.ServiceProcess' namespace and it's working beautifully.
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();
}
Upvotes: 0
Reputation: 40566
Try lowering the timeout value like so:
myBinding.OpenTimeout = TimeSpan.FromSeconds(5);
This timeout value is for opening the communication channel. If needed, you can also tweak SendTimeout
, ReceiveTimeout
and CloseTimeout
, all of them also on the binding object.
Upvotes: 3