gpt4 book ai didi

wcf - 从 WCF 中的 CommunicationObjectFaultedException 中恢复

转载 作者:行者123 更新时间:2023-12-03 21:13:19 25 4
gpt4 key购买 nike

我有一个客户端应用程序,它每 10 秒尝试一次通过 WCF Web 服务发送消息。这个客户端应用程序将安装在船上的一台计算机上,我们知道它的互联网连接不稳定。我希望应用程序尝试通过服务发送数据,如果不能,将消息排队,直到它可以通过服务发送它们。

为了测试这个设置,我启动了客户端应用程序和 Web 服务(都在我的本地机器上),一切正常。我尝试通过终止网络服务并重新启动来模拟不良的互联网连接。一旦我终止服务,我就会开始收到 CommunicationObjectFaultedExceptions——这是预期的。但是在我重新启动服务后,我继续收到这些异常。

我很确定我对 Web 服务范式有一些不了解,但我不知道那是什么。任何人都可以提供有关此设置是否可行的建议,如果可行,如何解决此问题(即重新建立与 Web 服务的通信 channel )?

谢谢!

克莱

最佳答案

客户端服务代理一旦出现故障就不能再使用。您必须处理旧的并重新创建新的。

您还必须确保正确关闭客户端服务代理。 WCF 服务代理可能会在关闭时抛出异常,如果发生这种情况,连接不会关闭,因此您必须中止。使用“try{Close}/catch{Abort}”模式。还要记住, dispose 方法调用 close (因此可以从 dispose 中抛出异常),所以你不能只对普通的一次性类使用 using 。

例如:

try
{
if (yourServiceProxy != null)
{
if (yourServiceProxy.State != CommunicationState.Faulted)
{
yourServiceProxy.Close();
}
else
{
yourServiceProxy.Abort();
}
}
}
catch (CommunicationException)
{
// Communication exceptions are normal when
// closing the connection.
yourServiceProxy.Abort();
}
catch (TimeoutException)
{
// Timeout exceptions are normal when closing
// the connection.
yourServiceProxy.Abort();
}
catch (Exception)
{
// Any other exception and you should
// abort the connection and rethrow to
// allow the exception to bubble upwards.
yourServiceProxy.Abort();
throw;
}
finally
{
// This is just to stop you from trying to
// close it again (with the null check at the start).
// This may not be necessary depending on
// your architecture.
yourServiceProxy = null;
}

有一篇关于此的博客文章 here

关于wcf - 从 WCF 中的 CommunicationObjectFaultedException 中恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com