我创建了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;
}
代码可以工作,但是当它不可用时超时太长。我尝试使用UDP发现,例如在此提供http://msdn.microsoft.com/en-us/magazine/ee335779.aspx。但是当客户端不可用时,它会遇到同样的问题。
实施逻辑以快速ping每个客户端并检查其可用性状态的最佳方法是什么?
答案 0 :(得分:3)
尝试降低超时值,如下所示:
myBinding.OpenTimeout = TimeSpan.FromSeconds(5);
此超时值用于打开通信通道。如果需要,您还可以调整SendTimeout
,ReceiveTimeout
和CloseTimeout
,所有这些也都在绑定对象上。
答案 1 :(得分:0)
我在System.ServiceProcess&#39;中使用了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();
}