簡體   English   中英

C#WCF NetTcpBinding超時不起作用

[英]C# WCF NetTcpBinding timeout not working

我在初始調用時出現NetTcpBinding的打開,發送,接收和關閉超時問題( [OperationContract(IsOneWay = true, IsInitiating = true)] )當前我正在嘗試連接到給定的IPAddress,如果失敗我將連接到其他地址。 這是因為我無法通過外部IP連接到我自己的服務器(由於路由器上的NAT環回?),而不是使用2個版本(一個用於公共版本,一個用於我自己版本),而是使用Try-Catch系統嘗試連接到我的外部IP,如果可行->繼續,如果失敗->嘗試本地IP->繼續。

但是,問題在於超時值。 由於我正在等待引發和捕獲異常,因此我需要盡可能降低此值(最好為5秒),而不是默認的20秒左右。

我試圖在客戶端和服務器端都設置這些值

tcpBinding.OpenTimeout = new TimeSpan(0, 0, 5);
tcpBinding.CloseTimeout = new TimeSpan(0, 0, 5);
tcpBinding.SendTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);

服務器端設置:

svh = new ServiceHost(typeof(ServiceAssembly.ServiceImplementation));
NetTcpBinding tcpBinding = new NetTcpBinding();

tcpBinding.MaxConnections = 100;
tcpBinding.MaxBufferPoolSize = (int)4096000;
tcpBinding.MaxBufferSize = 4096000;
tcpBinding.MaxReceivedMessageSize = (int)4096000;
tcpBinding.OpenTimeout = new TimeSpan(0, 0, 5);
tcpBinding.CloseTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);
tcpBinding.SendTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReliableSession.Ordered = true;
tcpBinding.TransactionFlow = false;
tcpBinding.Security.Transport.ProtectionLevel =
System.Net.Security.ProtectionLevel.EncryptAndSign;
tcpBinding.Security.Transport.ClientCredentialType =
TcpClientCredentialType.Windows;
tcpBinding.Security.Mode = SecurityMode.None;

svh.AddServiceEndpoint(typeof(ServiceAssembly.IChat),tcpBinding,"net.tcp://IPAddress:3100/MyService");

SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
svh.Open();

客戶端設置和呼叫

InstanceContext context = new InstanceContext(callback);
NetTcpBinding tcpBinding = new NetTcpBinding();

tcpBinding.MaxConnections = 100;
tcpBinding.MaxBufferPoolSize = (int)4096000;
tcpBinding.MaxBufferSize = 4096000;
tcpBinding.MaxReceivedMessageSize = (int)4096000;
tcpBinding.TransactionFlow = false;
tcpBinding.OpenTimeout = new TimeSpan(0, 0, 5);
tcpBinding.CloseTimeout = new TimeSpan(0, 0, 5);
tcpBinding.SendTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);
tcpBinding.ReliableSession.Ordered = true;

tcpBinding.Security.Transport.ProtectionLevel =
    System.Net.Security.ProtectionLevel.EncryptAndSign;
tcpBinding.Security.Transport.ClientCredentialType =
    TcpClientCredentialType.Windows;
tcpBinding.Security.Mode = SecurityMode.None;

 try
 {
      scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
            "net.tcp://" + m_ipAddress + ":" + m_port + "/MyService");
      s = scf.CreateChannel(); 
      s.Connect(client); <- First connection
 }
 catch
 {
     try
     {
         scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
               "net.tcp://" + m_localAddress + ":" + m_port + "/MyService");
         s = scf.CreateChannel();
         s.Connect(client);
         m_ipAddress = m_localAddress;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }

但是拋出異常的超時時間仍然是20秒左右。 更改這些值不會更改引發異常之前的時間。 我在下面添加了一個圖像以顯示調用堆棧。 在此處輸入圖片說明 需要有關如何解決此問題或什至使其更好的任何建議。

我發現對我有幫助的解決方案-不是刪除此超時,而是在本地時連接到我自己的服務器,如果不在本地IP上則連接到我自己的服務器-實際上是在尋找進程本身。 如果找到->在本地連接,否則->通過給定IP連接。 這不能回答我的實際問題,但是可以解決我的最初問題。 似乎超時“延遲”是由於DNS引起的,而不是我無法防止的事情。

如果有人感興趣,這是我的解決方案代碼。

if (Process.GetProcessesByName("ProcessName").Length > 0)
     isLocal = true;

 if (isLocal)
     scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
           "net.tcp://" + m_localAddress + ":" + m_port + "/MyService");
 else
     scf = new DuplexChannelFactory<IChat>(context, tcpBinding,
           "net.tcp://" + m_ipAddress + ":" + m_port + "/MyService");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM