beb*_*oor 10 c# methods wcf web-services ping
如何检查Web服务中的方法是否正常工作?我不能用ping.我仍然想检查客户端从Web服务调用的任何方法.我知道很难概括,但应该有一些方法.
我使用这种方法,它工作正常:
public bool IsAddressAvailable(string address)
{
try
{
System.Net.WebClient client = new WebClient();
client.DownloadData(address);
return true;
}
catch
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
只需在 Web 服务的方法中使用 try catch 并将异常记录到日志文件或事件日志中。例子:
[OperationContract]
public bool isGUID(string input)
{
bool functionReturnValue = false;
try
{
Guid guid;
functionReturnValue = Guid.TryParse(input, guid);
}
catch (Exception ex)
{
Log.WriteServerErrorLog(ex);
}
return functionReturnValue;
}
Run Code Online (Sandbox Code Playgroud)
您不需要 ping web 服务,而是使用看门狗服务或其他东西 ping 服务器。无需“ping”网络服务。我也认为无论如何你都不需要这样做。您的网络服务要么工作正常,要么由于代码错误而无法工作。