【问题标题】:Do WCF Callbacks TimeOut做 WCF 回调超时
【发布时间】:2012-05-22 02:56:24
【问题描述】:

我编写了一个系统,它使用带有回调的双工 NetTcp 通道作为发布/订阅服务器。

如果一段时间后没有发送连接,我是否需要担心回调超时或者回调管道会无限期维护?

【问题讨论】:

    标签: .net wcf callback publish-subscribe


    【解决方案1】:

    回调不会无限期地维持,它会寻找您在配置中设置的超时值。如果启用可靠会话,则可以设置客户端的不活动超时。您可以像这样配置超时:

     <netTcpBinding>
        <binding 
                 closeTimeout="00:01:00"
                 openTimeout="00:01:00" 
                 receiveTimeout="00:10:00" 
                 sendTimeout="00:01:00"
                 transactionFlow="false" 
               ......>
            <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="true" />
        </binding>
      </netTcpBinding>
    

    当达到这些值但仍然没有响应时,您的通信通道出现故障,您需要重新创建客户端代理以使用该服务。 receiveTimeout 的默认值为 10 分钟,因此您可以增加它,但也要确保增加您的 inactivityTimeout,您的 inactivityTimeout 应该大于您的 receiveTimeout

    编辑:

    您可以根据您的客户端发送回服务器的值以编程方式不断更改您的receiveTimeout,关键是保持服务和客户端上的 new 超时值相同。 您将在客户端上执行此操作(我从使用 WCF 制作并使用 Silverlight 客户端的聊天服务中获取示例):

    //Create different clients dynamically
    MyChatServiceClient _client1 = new MyChatServiceClient( "NetTcpBinding_IMyChatService_1");
    MyChatServiceClient _client2 = new MyChatServiceClient( "NetTcpBinding_IMyChatService_2");
    

    在客户端配置中:

    <!--In your config file, define multiple endpoints/behaviors with different values based on your needs-->
            <bindings>
                <customBinding>
                    <binding name="NetTcpBinding_IMyChatService_1" receiveTimeout="00:01:00" ...>
                        <binaryMessageEncoding />
                        <tcpTransport maxReceivedMessageSize="283647" maxBufferSize="283647" />
                    </binding>
                    <binding name="NetTcpBinding_IMyChatService_2" receiveTimeout="00:22:00" ...>
                        <binaryMessageEncoding />
                        <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                    </binding>
                </customBinding>
            </bindings>
            <client>
                <endpoint address="net.tcp://192.168.1.51:4520/VideoChatServer/"
                    binding="customBinding" bindingConfiguration="NetTcpBinding_IMyChatService_1"
                    contract="IMyChatService" name="NetTcpBinding_IMyChatService_1" />
    
              <endpoint address="net.tcp://192.168.1.51:4522/VideoChatServer/"
                    binding="customBinding" bindingConfiguration="NetTcpBinding_IMyChatService_2"
                    contract="IMyChatService" name="NetTcpBinding_IMyChatService_2" />
            </client>
    

    因此,您可以在客户端或服务器的配置中定义多个端点或绑定,然后基于任何 应用程序中的事件,您可以实例化 _clientProxyX 以使用 _serviceInstanceX ,这将具有不同的绑定/端点值 但与您的上一个服务实例具有相同的合同。在上面的示例中,第一个绑定的超时时间为 1 分钟, 第二次装订2分钟。需要考虑的重要一点是,如果您希望像这样重新创建新的客户端代理,那么您需要拆除旧客户端 代理并创建一个新的,这可以有效地断开您的客户与服务的连接,至少是暂时的。

    您还可以在实例化新服务主机时在两个服务器上以编程方式修改这些值(openTimeoutcloseTimeout 等)。 您可以根据您在配置中定义的绑定配置之一创建一个新主机, 或以编程方式创建新配置,如下所示:

    var host = new ServiceHost(typeof(MyChatService));
                var webHttpBinding = new System.ServiceModel.WebHttpBinding();
                //Modify new timeout values before starting the host
                webHttpBinding.OpenTimeout = new TimeSpan(1, 0, 0);
                webHttpBinding.CloseTimeout = new TimeSpan(1, 0, 0);
                host.AddServiceEndpoint(webHttpBinding, "http://192.168.1.51/myService.svc");
                //start the host after necessary adjustments
                host.Open();
    

    我知道这看起来很混乱,但关键是 WCF 为您提供了很大的灵活性,能够以编程方式修改绑定配置。 请务必查看 this 修改 WCF 配置文件的好答案。而且你还可以轻松创建一个整体service configuration programmtically

    【讨论】:

    • 我想处理周期性事件,是否可以接受每隔 x 分钟从服务器发送一条“Keep-Alive”消息并将超时设置为 2*x 的做法?
    • 所以您想根据您的客户端返回到服务器的内容不断更改 receiveTimeout 吗?我以前没有做过,但我认为这不一定是坏习惯。为什么不将超时设置为“无限”?您也可以像这样以编程方式执行此操作: binding.ReceiveTimeout = new TimeSpan(300,0,0,0,0);这将使您的 receiveTimeout 为 300 天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2011-07-09
    • 2012-08-19
    • 1970-01-01
    • 2014-03-31
    相关资源
    最近更新 更多