如何判断WCF服务客户端代理是否已超时

时间:2012-07-23 15:55:51

标签: c# wcf wshttpbinding

我使用带有消息加密的wsHttpBinding来获得WCF服务。我使用相同的服务引用,除非它出现故障,在这种情况下我创建一个新的。我遇到的问题是会话超时并且服务已经结束,但客户端应用程序仍然将CommunicationState作为已打开。

如何在ClientBase中告知连接是否已超时?我想在我的客户端应用程序中创建一个新的服务代理,如果当前的服务代理超时。

以下是我的客户端绑定:

  <wsHttpBinding>
    <binding name="wsHttpBindingWithAuthClient" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="20000000"
      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
      allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="UserName" negotiateServiceCredential="true"
          algorithmSuite="Default" />
      </security>
    </binding>
  </wsHttpBinding>

2 个答案:

答案 0 :(得分:2)

为什么不将所有服务调用都包装在try / catch中,并在特定异常类型的服务器连接超时的catch块中重新创建代理。

try
{
  serviceProxyGlobal.Method()
}
catch(WhateverServerTimeoutException ex)
{
  serviceProxyGlobal = new ServiceProxy();
  //retry maybe?
}
catch(Exception ex)
{
  logException(ex);
}

答案 1 :(得分:0)

请考虑使用简单的getter访问该共享Web服务引用,您基本上在返回引用之前尝试“ping”您的Web服务(“ping”方法应该非常简单,例如Boolean Ping() { return true; } )如果它抛出异常(这里你必须满足所有可能的场景),只需处理它,禁止它(如果它是一个例外,你可以识别和你期望的)并创建新的共享连接。

或者,您可以设计一个方法,在后台线程中每个[web服务超时以分钟为单位] / 2分钟ping您的Web服务。

相关问题