【问题标题】:Recovering from a CommunicationObjectFaultedException in WCF从 WCF 中的 CommunicationObjectFaultedException 中恢复
【发布时间】:2010-11-17 11:59:56
【问题描述】:

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

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

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

谢谢!

克莱

【问题讨论】:

    标签: wcf exception channel


    【解决方案1】:

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

    您还必须确保正确关闭客户端服务代理。 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;
    }
    

    有一个blog article about this,但它现在似乎处于离线状态。存档版本可用on the Wayback Machine

    【讨论】:

    • 如果可以的话 +10 - 哇,这种行为完全不为人知,如果我没有偶然发现这个答案,我永远不会知道发生了什么。
    • 太棒了!我将它的一个版本实现为扩展方法:代理类上的 TryDispose 供其他人使用。
    • @Moby 的特技替身 - 你能分享你的代码吗?
    • 代码粘贴链接:找不到页面。
    • 对于这里的任何人,您都可以使用返回机器获取更多详细信息。 web.archive.org/web/20170625141905/http://…
    猜你喜欢
    • 2016-12-20
    • 2013-01-04
    • 1970-01-01
    • 2010-11-15
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    相关资源
    最近更新 更多