如何在调用合约方法之前检测 WCF 会话崩溃?
我正在为我的 WCF 服务使用会话模式。问题如下:如果会话被破坏并且不再存在,客户端在调用合约之前无法知道它。
例如,如果服务已重新启动,则客户端的会话 ID 无效,因为该会话已在服务器端关闭。
我在调用合约之前检查通道状态,即使会话已经中断,其值也是 CommunicationState.Opened 。因此,当我在检查后调用合约时,我收到带有以下消息的 CommunicationException:
远程端点不再识别该序列。这很可能是由于远程端点上的中止造成的。 wsrm:Identifier 的值不是已知的序列标识符。可靠会话出现故障。
有什么解决方法吗?我需要一种方法在调用合约之前获取适当的会话状态,以便我可以在不出现异常的情况下恢复它。
PS CommunicationException 类型是通用的,因此我无法通过捕获此异常来检测会话崩溃。
PPS 我在此处提出了类似的问题,但在那如果我不知道原因,现在我不知道如何逃避。
I am using a session mode for my WCF service. The problem is the following: if session is broken and no longer exists, client can't know it before calling a contract.
For example, if the service has been restarted, the client's session id is invalid, because that session has been closed on the server side.
I check the channel state before calling the contract and its value is CommunicationState.Opened even if session is already broken. So, when I call the contract after this check I get a CommunicationException with this message:
The remote endpoint no longer recognizes this sequence. This is most likely due to an abort on the remote endpoint. The value of wsrm:Identifier is not a known Sequence identifier. The reliable session was faulted.
Is there any workaround? I need a way to get an appropriate session state before calling a contract so that I can restore it without getting an exception.
P.S. The CommunicationException type is general, so I can't detect a session crash by catching this exception.
P.P.S. I have asked the similar question here, but in that case I didn't know the reason, now I don't know how to evade it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,没有解决方法 - 您所能(并且应该做)的就是使用适当的防御性编程原则,以便能够在发生此类异常时捕获并处理它们。
不幸的是,如果服务器崩溃或网络出现故障,则没有任何机制可以将这种情况通知所有潜在客户 - 他们只会在下次尝试致电时发现。
更新:是的,
CommunicationException
只是与 WCF 相关的所有异常的公共基类 - 请查看 MSDN 文档 查看您可以捕获的所有后代异常,更具体 -EndpointNotFoundException
、FaultException< /code> (或
FaultException
)、ProtocolException
等等!No, there is no workaround - all you can (and should do) is use proper defensive programming principles to be able to catch and handle those kind of exceptions as they happen.
If the server crashes or the network goes down, unfortunately, there's no mechanism to inform all potential clients of this case - they'll just find out the next time they try to call.
Update: yes, the
CommunicationException
is just the common base class for all exceptions related to WCF - check out the MSDN docs to see about all the descendant exceptions you can catch to be more specific -EndpointNotFoundException
,FaultException
(orFaultException<T>
),ProtocolException
and many many more!