为什么超时异常没有出错我的频道



我在同一台计算机上运行双工 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会出错频道(请参阅此答案),但不知何故,情况似乎并非如此。 有什么方法可以在服务进程终止后更快地强制通道出现故障?

这个问题 双工通道故障事件在第二次连接尝试时不会上升 建议Faulted事件并不总是触发。MSDN 上的 WCF 状态流程图证实了这种可能性 - http://msdn.microsoft.com/en-us/library/ms789041.aspx

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

我知道这个问题很老了。但我搜索了很多,总是在这里结束。所以我想我会在这里发布我的发现:

这取决于哪个超时。

如果您击中绑定的SendTimeoutReceiveTimeout(在我的情况下NetTcpBinding),那么是的,通道会出错。

但是,如果您点击服务的OperationTimeout(在我的例子中是双工频道),那么您只会得到一个TimeoutException并且频道不会出错。

最新更新