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

我有一个双工WCF服务和客户端在同一台机器上运行。 客户端配置为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分钟,我的error handling程序才会被调用。 我认为TimeoutException会使通道TimeoutException (请参阅此答案 ),但不知何故情况似乎并非如此。 有什么办法可以在服务进程被杀后更快地强制通道出现故障吗?

此问题Duplex channel Faulted事件在第二次连接尝试时没有上升建议Faulted事件并不总是被触发。 MSDN上的WCF状态流程图证实了这种可能性 – http://msdn.microsoft.com/en-us/library/ms789041.aspx

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