编程语言
首页 > 编程语言> > c#-为什么TimeoutException不会使我的频道出错?

c#-为什么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;

如果我终止了Service进程,则客户端会在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),但似乎并非如此.在终止服务进程后,有什么办法可以迫使通道更快地出现故障?

解决方法:

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中的逻辑移到其他位置,以便在您通过其他路径达到关闭状态时执行该逻辑.

标签:duplex,timeoutexception,wcf,c,net
来源: https://codeday.me/bug/20191031/1977280.html