简体   繁体   English

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

[英]How to tell if WCF service client proxy has timed out

I have a WCF service using wsHttpBinding with message encryption. 我有一个使用wsHttpBinding和消息加密的WCF服务。 I use the same service reference unless it has become faulted, in which case I create a new one. 我使用相同的服务引用,除非它出现故障,在这种情况下我创建一个新的。 I'm running in to an issue where the session has timed out and the service has closed its end, but the client application still has the CommunicationState as Opened. 我遇到的问题是会话超时并且服务已经结束,但客户端应用程序仍然将CommunicationState作为Opened。

How can I tell in ClientBase if the connection has timed out? 如何在ClientBase中告知连接是否已超时? I want in my client application to create a new service proxy if the current one has timed out. 我想在我的客户端应用程序中创建一个新的服务代理,如果当前的服务代理超时。

Below is my client-side binding: 以下是我的客户端绑定:

  <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>

Why don't you just wrap all of your service calls in a try/catch, and in the catch block of the specific exception type of server connection timeout, recreate the proxy. 为什么不将所有服务调用都包装在try / catch中,并在特定异常类型的服务器连接超时的catch块中重新创建代理。

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

Please consider accessing that shared web service reference using a simple getter, where you basically try to "ping" your web service just before returning reference to it (that "ping" method should be really simple, like Boolean Ping() { return true; } ) If it throws exception (here you have to cater for all possible scenarios), simply handle it, suppress it (if it's an exception you can identify and which you expected) and create new shared connection. 请考虑使用简单的getter访问该共享Web服务引用,您基本上在返回引用之前尝试“ping”您的Web服务(“ping”方法应该非常简单,如Boolean Ping() { return true; } )如果抛出异常(在这里你必须应付各种可能出现的情况),简单地处理它,压制它(如果它是你能识别和你的预期),并创建新的共享连接异常。

Alternatively you can design a method pinging your web service each [web service timeout in minutes] / 2 minutes in a background thread. 或者,您可以设计一个方法,在后台线程中每个[web服务超时以分钟为单位] / 2分钟ping您的Web服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM