WCF回调TimeOut

时间:2012-05-14 15:33:53

标签: .net wcf callback publish-subscribe

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

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

1 个答案:

答案 0 :(得分:10)

回调将无法无限期维护,它将查找您在配置中设置的超时值。如果启用可靠会话,则可以设置客户端的非活动超时。您可以配置如下的超时:

 <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分钟,因此您可以增加该值,但同时也要确保增加inactivityTimeoutinactivityTimeout应大于receiveTimeout

修改

您可以根据客户端发送回服务器的值以编程方式继续更改receiveTimeout,关键是在服务和客户端上保持超时值相同。 您可以在客户端上执行此操作(我正在使用我正在使用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能够以编程方式修改绑定配置,为您提供了很大的灵活性。 请务必查看有关修改WCF配置文件的this完美答案。您还可以轻松创建整个service configuration programmtically