【发布时间】:2017-02-11 12:48:11
【问题描述】:
我调查了一件坏事,假设我们有一个数据服务,我们想在消费者端调用它,例如:
Uri dataManURI = new Uri("http://localhost:2040/DTService.svc/rest");
DataServiceContext dataServiceContext = new DataServiceContext(dataManURI);
var all = from ex in dataServiceContext.CreateQuery<ExternalPath_DTO>("ExternalPaths")
select ex;
经过很长时间我会得到:
加载外部路径失败!,EXP:System.Data.Services.Client.DataServiceTransportException: The operation has timed out ---> System.Net.WebException: The operation has timed out 在 System.Net.HttpWebRequest.GetResponse() 在 System.Data.Services.Client.HttpWebRequestMessage.GetResponse() --- 内部异常堆栈跟踪结束 --- 在 System.Data.Services.Client.HttpWebRequestMessage.GetResponse() 在 System.Data.Services.Client.DataServiceContext.GetResponseHelper(ODataRequestMessageWrapper 请求,IAsyncResult asyncResult,布尔句柄WebException) 在 System.Data.Services.Client.QueryResult.ExecuteQuery()
或 WCF 服务:
AuthClient ac = new AuthClient("authEndPoint", Token);
ac.RemoveUser(new RemoveUser_DTO_IN() { UserId = uidToDel });
因此,如果服务不可用,则需要很长时间才能捕获异常
在另一侧 UI 被冻结直到响应(在我的情况下 1 分钟得到失败响应)。
如果是,这些解决方案是否有用,但如何?
1- 定义一个简单的操作,例如Ping() 并在任何主要操作之前调用它?
AuthClient ac = new AuthClient("authEndPoint", Token);
var watch = System.Diagnostics.Stopwatch.StartNew();
var ping = Task.Run(() =>
{
ac.Ping();
});
try
{
if (!ping.Wait(ac.Endpoint.Binding.OpenTimeout))
{
//Timeout on Ping for end point
watch.Stop();
return;
}
}
catch (Exception exp)
{
watch.Stop();
return;
}
我的期望是在 3 秒内得到响应(打开超时),在我看来,如果 Ping() 不会在 3 秒内回复,则该服务不可用。
2- telnet 服务?
解决方案1:我认为没有任务编程和!ping.Wait(ac.Endpoint.Binding.OpenTimeout)的解决方案1是不合适的,因为我上面的解释如果服务不可用则需要1分钟(端点接收时间)说操作超时。
解决方案 2:如何,实例?
是否有任何其他机制可以非常快速地知道服务是否可用?
任何帮助将不胜感激。
【问题讨论】:
-
“唤醒服务” 或 “ping 服务以查看它是否处于活动状态” 是 XY 问题。你的实际问题是“调用服务时,客户端超时”,所以修复那个问题。调查为什么服务需要很长时间才能响应。它很可能已经启动并运行并在您 ping 它时愉快地响应,但是您打算执行的操作仍然会超时,因为它遇到了性能不佳的数据库或其他什么。
-
我知道这可能是因为网络问题而不是代码性能或与我的代码相关的任何事情......
标签: c# .net performance wcf wcf-data-services