2012-06-25 100 views
4

我知道這個問題已經有很多問題了,但沒有回覆解決了我的問題。使用NetTcpBinding終止套接字連接

我有一個服務器上運行一個NetTcpBinding的,並且不時地(不總是像1出20到60的呼叫,完全是隨機的),我收到了客戶端此異常:

的套接字連接被中止。這可能是由於處理您的消息的錯誤 或遠程主機的 接收超時或基礎網絡資源問題所致。本地套接字 超時時間爲'00:01:00'。

我注意到,當客戶端的連接速度很慢時,這種異常發生得更頻繁。

此外,所述的超時時間爲1分鐘,但在1秒左右之後已經發生異常。

我做什麼:

var client = GetClient(); 
client.Open(); 
bool success = client.Call(); // Exception occurs here (the return value is a bool) 

我有時也得在服務器端此異常:

TCP錯誤(995:的I/O操作已中止,因爲無論 的一個線程退出或一個應用程序請求)在傳輸 數據時發生。

我有服務器跟蹤啓用,但我得到相同的例外,並沒有提供任何額外的見解。

我GetClient:

NetTcpBinding netTcpBinding = new NetTcpBinding 
{ 
    CloseTimeout = new TimeSpan(0, 0, 30), 
    OpenTimeout = new TimeSpan(0, 0, 30), 
    TransferMode = TransferMode.Buffered, 
    ListenBacklog = 2, 
    MaxConnections = 2, 
    MaxReceivedMessageSize = 10485600, 
    ReaderQuotas = { MaxStringContentLength = 1048576 }, 
    ReliableSession = { Enabled = false }, 
    Security = 
    { 
     Mode = SecurityMode.Transport, 
     Transport = { ClientCredentialType = TcpClientCredentialType.None, ProtectionLevel = ProtectionLevel.EncryptAndSign }, 
     Message = { ClientCredentialType = MessageCredentialType.Windows } 
    } 
}; 

從服務的重要配置:

<behaviors> 
    <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true" httpsHelpPageEnabled="true" /> 
      <serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="My.Service.WCF.Tools.CustomAuthorizationManager, My.Service.WCF"> 
       <authorizationPolicies> 
        <clear/> 
        <add policyType="My.Service.WCF.Tools.CustomAuthorizationPolicy, My.Service.WCF" /> 
       </authorizationPolicies> 
      </serviceAuthorization> 
      <serviceCredentials> 
       <serviceCertificate findValue="MyService" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" /> 
      </serviceCredentials> 
      <serviceThrottling maxConcurrentCalls="65536" maxConcurrentSessions="65536" maxConcurrentInstances="65536" /> 
     </behavior> 
    </serviceBehaviors> 
</behaviors> 
<bindings> 
    <customBinding> 
     <binding name="ServiceBinding" receiveTimeout="00:05:00" maxConnections="65536" listenBacklog="65536"> 
      <transactionFlow />      
      <sslStreamSecurity requireClientCertificate="false" /> 
      <binaryMessageEncoding /> 
      <tcpTransport transferMode="Buffered" portSharingEnabled="true" maxReceivedMessageSize="1048576" maxPendingConnections="65536" maxPendingAccepts="10"> 
       <connectionPoolSettings leaseTimeout="00:05:00" idleTimeout="00:05:00" maxOutboundConnectionsPerEndpoint="65536" /> 
      </tcpTransport> 
     </binding> 
     <binding name="MexBinding" receiveTimeout="00:05:00" maxConnections="65536" listenBacklog="65536"> 
      <tcpTransport transferMode="Buffered" portSharingEnabled="true" maxReceivedMessageSize="1048576" maxPendingConnections="65536" maxPendingAccepts="10"> 
       <connectionPoolSettings leaseTimeout="00:05:00" idleTimeout="00:05:00" maxOutboundConnectionsPerEndpoint="65536" /> 
      </tcpTransport> 
     </binding> 
    </customBinding> 
</bindings> 

我已經更新服務引用,並試圖改變一些緩衝和大小,但它沒有工作(不是因爲只有一個bool被返回,所以我期望有一個問題)。

使用固定:

OperationContext.Current.OperationCompleted += Test; 

private void Test(object sender, EventArgs e) 
{ 
    OperationContext.Current.Channel.Close(); 
} 

不要使用中止,但關閉的方法!

+0

你是否增加了sendTimeout以及客戶端上的netTcpBinding? – Rajesh

+0

是的,我也試過,但1分鐘已經很長時間了(一次通話只需要10ms左右),增加它並沒有幫助。此外,例外幾乎立即發生,而不是1分鐘後。 –

+0

您的打開和關閉超時在您的綁定客戶端上是30秒? – Rajesh

回答

1

我發現這個問題:我放棄對服務器端的通道與

OperationContext.Current.OperationCompleted 

呼籲:

OperationContext.Current.Channel.Abort(); 

這似乎在99%的情況做工精細,但不是全部,使用:

OperationContext.Current.Channel.Close(); 

完全解決了我的問題。