2012-04-22 69 views
2

我有一台服务器从多个系统接收数据,将它们添加到数据库并使用最新接收的数据更新另一个应用程序(客户端)。这个客户端(都运行在同一台计算机上)以有组织的形式呈现数据并对其进行一些处理。而且,它可以使用服务器在数据库中执行查询。所以它使用服务器中的函数来获取历史数据。WCF服务中的自动重新连接

对于这个通信我使用WCF,而服务器被声明为在.config如下:

<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service name="ServiceName"> 
     <endpoint binding="netTcpBinding" contract="IServiceName"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.tcp://localhost:5050/msservice"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    </system.serviceModel> 

客户端使用以下配置:

<system.serviceModel> 
    <bindings> 
     <netTcpBinding> 
      <binding name="NetTcpBinding_IService" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxConnections="10" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"> 
       <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/> 
       <security mode="Transport"> 
        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/> 
        <message clientCredentialType="Windows"/> 
       </security> 
      </binding> 
     </netTcpBinding> 
    </bindings> 
    <client> 
     <endpoint address="net.tcp://localhost:5050/msservice" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService" contract="Server.IService" name="NetTcpBinding_IService"> 
      <identity> 
       <dns value="localhost"/> 
      </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

当客户端创建服务(连接到服务器)时,它使用称为订阅的服务功能,其中包括服务器连接的客户端列表中的客户端。当新数据到达时,它会在所有客户端引发一个事件。

但是,在客户机处于非活动状态(因为它不会定期向服务器发送消息,即使相反发生在非常高的频率上)之后,它会进入故障状态。发生这种情况时,服务器功能的每个客户端调用都会引发异常。

我希望,无论是在服务器端还是客户端,每当通道关闭时自动重新连接,以保证客户端仍然接收来自客户端的消息,并且客户端的函数调用由服务器。

非常感谢您的帮助!

回答

0

负载均衡器在1分钟后关闭空闲连接。所以,如果你想保持连接活着,那么应该总是有一些沟通

1)第一个选项是每分钟调用一个操作。

2)如果这是不可能的,那么

  • 启用绑定和WCF基础设施将处理它的可靠消息。
  • 您可以配置inactivityTimeout和receiveTimeout以确保通道保持打开状态。
  • 两个超时都必须设置,否则如果任何一个超时,通道就会出现故障。
  • 可靠的会话配置后,如果连接变得空闲,WCF将生成流量以保持打开状态。