为什么TimeoutException不会导致我的频道错误?

时间:2012-11-15 17:29:24

标签: c# .net wcf duplex timeoutexception

我在同一台机器上运行双工WCF服务和客户端。客户端配置为15秒超时:

<binding name="NetTcpBinding_IServiceIPC" closeTimeout="00:00:15"
      openTimeout="00:00:15" receiveTimeout="00:00:15" sendTimeout="00:00:15" />

客户端正在处理这样的错误:

client.InnerChannel.Faulted += FaultHandler;
client.InnerDuplexChannel.Faulted += FaultHandler;
client.ChannelFactory.Faulted += FaultHandler;

如果我终止了我的服务流程,客户端会在15秒后正确获取TimeoutException

This request operation sent to net.tcp://localhost:8732/Service/ did not receive a reply within the configured timeout (00:00:15).  The time allotted to this operation may have been a portion of a longer timeout.  This may be because the service is still processing the operation or because the service was unable to send a reply message.  Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client. (System.TimeoutException)

但是,此时频道没有出现故障。在我终止服务进程后大约5分钟,我的错误处理程序才会被调用。我认为TimeoutException会导致频道出错(请参阅此answer),但不知何故情况并非如此。有没有什么办法可以在服务进程被杀后更快地强制通道出现故障?

2 个答案:

答案 0 :(得分:4)

这个问题Duplex channel Faulted event does not rise on second connection attempt建议Faulted事件并不总是被触发。 MSDN上的WCF状态流程图确认了这种可能性 - http://msdn.microsoft.com/en-us/library/ms789041.aspx

关闭状态有很多路径没有经过故障状态。最有可能的是,当您超时时,正在调用Abort()方法,并且您将从打开状态转换到关闭状态,而不会经历故障状态。添加一些日志以在整个执行过程中检查状态。如果您在超时后尝试重新打开频道,这可以解释为什么您在5分钟后最终处于故障状态。要解决更大的问题,请将FaultedHandler中的逻辑移到别处,以便在通过其他路径达到关闭状态时执行。

答案 1 :(得分:0)

我知道这个问题很旧。但是我搜索了很多东西,并且总是在这里结束。所以我想把发现发表在这里:

这取决于哪个超时时间。

如果您击中了绑定的SendTimeoutReceiveTimeout(在我的情况下为NetTcpBinding),那么可以,该频道将发生故障。

但是,如果您点击了服务的OperationTimeout(在我的情况下是DuplexChannel),那么您只会得到一个TimeoutException,该频道将出现故障。