简体   繁体   English

如何在服务器端检测客户端的超时异常?

[英]How to detect client's timeout exception on the server side?

I am using synchronous WCF service which works well 99% of the time, but on some very rare occasions client times out before the server finishes processing. 我使用的同步WCF服务在99%的情况下运行良好,但在极少数情况下,客户端会在服务器完成处理之前超时。 Is there a way to detect, on the SERVER side, that the client has timed out? 有没有办法在SERVER端检测到客户端已经超时? I could use async operation, but in this case server-side timeout detection would save me quite a lot of work. 我可以使用异步操作,但在这种情况下,服务器端超时检测将为我节省大量的工作。 I'm using net.tcp binding, if this matters. 我正在使用net.tcp绑定,如果这很重要的话。

For net.tcp, http, etc. in general no. 对于net.tcp,http等一般没有。 (see comments above for some ideas; also it might be different for other protocols/bindings/etc.) (有关一些想法,请参阅上面的评论;对于其他协议/绑定/等,它也可能有所不同。)

The reason is, that the WCF infrastructure code on the server side will not use the channel before it has finished executing the service operation's implementation code and marshalling the response. 原因是服务器端的WCF基础结构代码在完成执行服务操作的实现代码并编组响应之前不会使用该通道。 Only then it will attempt to send the response and at this point recognize that the connection has already been aborted by the client. 只有这样它才会尝试发送响应,并在此时识别连接已被客户端中止。

When the server gets that error the user code (your service operation implementation) is already done and thus you cannot react to that from there anymore. 当服务器收到该错误时,用户代码(您的服务操作实现)已经完成,因此您无法再从那里做出反应。 It might be possible from within a dispatcher, or other extension point, but personally I have not tried. 它可能来自调度员或其他扩展点,但我个人没有尝试过。 However, that also would not save your server from unnecessary work, because as said, the client disconnect will still only be recognized when the server actually attempts to send the answer. 但是,这也不会使您的服务器免于不必要的工作,因为如上所述,当服务器实际尝试发送答案时,仍然只能识别客户端断开连接。

One "simple" way to mitigate such issues might be to split the work being done into several service operations/calls (if at all possible; and not accidentally introducing server-side state in the process). 缓解此类问题的一种“简单”方法可能是将正在完成的工作拆分为多个服务操作/调用(如果可能的话;并且不会在此过程中意外引入服务器端状态)。 Or as others have said, have the client implement a "Ping" interface that the server can use to check if the client is still "alive" and the response is still needed. 或者像其他人所说的那样,让客户端实现一个“Ping”接口,服务器可以使用它来检查客户端是否仍然“活着”并且仍然需要响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF超时异常服务器端 - WCF Timeout Exception Server Side 如何在不使用catch异常的情况下从.NET应用程序检测sql server超时 - how to detect sql server timeout from .NET application without using catch Exception CloudTableClient客户端超时 - CloudTableClient client side timeout 如何在服务器端侦听客户端事件 - How to listen to Client Side events on Server Side 在客户端的SQL Server 2012中检测断开的连接 - Detect broken connection in SQL Server 2012 on the client side SignalR - 用于检测客户端是否与集线器断开连接的服务器端方法? - SignalR - Server side method to detect if a client disconnects from a hub? 在一个解决方案中从 wpf 客户端到服务器的调用期间出现超时异常 - Timeout Exception during call from wpf client to server in one solution 如何在客户端缓慢的互联网成为asp.net服务器超时异常的原因? - How can a slow internet on client side be a reason for server time out exception in asp.net? WCF如何检测客户端和服务器是否在同一服务器中 - WCF how to detect if client and server are in same server 如何使用StackExchange.Redis解决客户端的Redis超时问题? - How to solve a Redis timeout on client side with StackExchange.Redis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM