Dot-Net

做 WCF 回調超時

  • June 5, 2012

我編寫了一個系統,它使用帶有回調的雙工 NetTcp 通道來充當發布/訂閱伺服器。

如果一段時間後沒有發送連接,我是否必須擔心回調超時,或者回調管道會無限期地維護?

回調不會無限期維護,它會查找您在配置中設置的超時值。如果啟用可靠會話,則可以設置客戶端的不活動超時。您可以像這樣配置超時:

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

當達到這些值並且仍然沒有響應時,您的通信通道出現故障,您需要重新創建客戶端代理以使用服務。預設值為receiveTimeout10 分鐘,因此您可以增加該值,但也要確保增加您inactivityTimeout的,您的inactivityTimeout應該大於您的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 配置文件的出色答案。您還可以輕鬆地以程式方式創建整個服務配置。

引用自:https://stackoverflow.com/questions/10586466