简体   繁体   English

WCF回调TimeOut

[英]Do WCF Callbacks TimeOut

I've written a system that uses a Duplex NetTcp Channel with a callback to function as a publish/subscribe server. 我编写了一个系统,它使用带有回调的Duplex NetTcp通道作为发布/订阅服务器。

Do I have to worry about the callback timing out if not connection is sent after a while or will the callback pipe be maintained indefinitely? 如果一段时间后没有发送连接,或者无限期地维护回调管道,我是否必须担心回调超时?

The callback won't be maintained indefinitely, It will look for the timeout values you've set in your config. 回调将无法无限期维护,它将查找您在配置中设置的超时值。 If you enable reliable sessions then you can set the inactivity timeouts of your clients. 如果启用可靠会话,则可以设置客户端的非活动超时。 You can configure timeouts like this: 您可以配置这样的超时:

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

When these values are reached and there is still no response, your communication channel becomes faulted and you need to re-create a client proxy to consume the service. 达到这些值并且仍然没有响应时,您的通信通道会出现故障,您需要重新创建客户端代理才能使用该服务。 The default value for receiveTimeout is 10 minutes so you can increase that, but also make sure to increase your inactivityTimeout as well, your inactivityTimeout should be greater than your receiveTimeout . receiveTimeout的默认值是10分钟,因此您可以增加它,但也要确保增加inactivityTimeout ,您的inactivityTimeout应该大于您的receiveTimeout

EDIT: 编辑:

You could keep changing your receiveTimeout programmatically based on values your client sends back to the server, the key is keeping the new timeout values same on the service and client. 您可以根据客户端发送回服务器的值receiveTimeout编程方式继续更改receiveTimeout ,关键是在服务和客户端上保持新的超时值相同。 You would go about doing this on the client (an example I'm taking from a chat service I'm making with WCF and consuming with Silverlight clients): 您将在客户端上执行此操作(我正在使用我正在使用WCF进行聊天服务并使用Silverlight客户端进行操作的示例):

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

In the client configuration: 在客户端配置中:

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

So you can define multiple endpoints or bindings in the config on your client or your server, then based on whatever event in your application you can instantiate _clientProxyX to consume _serviceInstanceX which will have different binding/endpoint values but the same contract as your previous service instance. 因此,您可以在客户端或服务器上的配置中定义多个端点或绑定,然后根据应用程序中的任何事件,您可以实例化_clientProxyX以使用_serviceInstanceX,它将具有不同的绑定/端点值,但与您之前的服务实例具有相同的合同。 In the example above the first binding has a timeout of 1 minute, the second binding 2 minutes. 在上面的示例中,第一个绑定的超时为1分钟,第二个绑定的超时为2分钟。 An important point to consider is that if you wish to recreate new client proxies like this, then you need to tear down your old client proxy and crate a new one, which effectively disconnects your clients from the service, at least momentarily. 需要考虑的重要一点是,如果您希望重新创建这样的新客户端代理,那么您需要拆除旧的客户端代理并创建一个新客户端代理,这有效地将客户端与服务断开连接,至少是暂时的。

Also you can modify those values ( openTimeout , closeTimeout etc.) programmatically on both the server when instantiating a new service host. 此外,您可以在实例化新服务主机时以编程方式修改这些值( openTimeoutcloseTimeout等)。 You can create a new host, based on one of the binding configurations you've defined in your config, or create a new config programmatically, something like this: 您可以根据您在配置中定义的绑定配置之一创建新主机,也可以以编程方式创建新配置,如下所示:

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();

This looks quite messy I know, but the point is WCF gives you a lot of flexibility in being able to modify you binding configs programmatically. 我知道这看起来很混乱,但关键是WCF为您提供了很多灵活性,能够以编程方式修改绑定配置。 Be sure to check out this great answer on modifying your WCF config files. 一定要检查出这个伟大的答案如何修改您的WCF的配置文件。 And you can also easily create a whole service configuration programmtically . 您还可以轻松地以编程方式创建整个服务配置

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

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