背景:WindowsService + WCF + NetTcpBinding
之前一直使用http協議模式,改為net.tcp之后隔段時間出現:由於系統緩沖區空間不足或隊列已滿,不能執行套接字上的操作。 127.0.0.1:9000

記錄時間:2016-01-14 10:02:58 日志級別:Exception 日志位置:CloudTraPlatSOA.Abstract.AbInSideSMS 當 前 行: 85 方法名稱: SendMessageInfo 日志描述:發送到[185*******]短信失敗,內容:高興的通知您:已經成功辦理 [ 基礎會員 ] ,會員到期時間:2017-01-14 Ex.Message: Unable to connect to the remote server InnerException.Message: 由於系統緩沖區空間不足或隊列已滿,不能執行套接字上的操作。 127.0.0.1:9000 Message.StackTrace: at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) 堆棧跟蹤: at SyncInvokeWebSendMessage(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext) at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext) at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result) at System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result) at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously) at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception) at System.ServiceModel.Channels.FramingDuplexSessionChannel.TryReceiveAsyncResult.OnReceive(IAsyncResult result) at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously) at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously, Exception exception) at System.ServiceModel.Channels.SynchronizedMessageSource.ReceiveAsyncResult.OnReceiveComplete(Object state) at System.ServiceModel.Channels.SessionConnectionReader.OnAsyncReadComplete(Object state) at System.ServiceModel.Channels.SocketConnection.FinishRead() at System.ServiceModel.Channels.SocketConnection.AsyncReadCallback(Boolean haveResult, Int32 error, Int32 bytesRead) at System.ServiceModel.Channels.OverlappedContext.CompleteCallback(UInt32 error, UInt32 numBytes, NativeOverlapped* nativeOverlapped) at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
原因:
tcp的端口請求已經達到本地設置的界限(MaxUserPort)
解決方式
修改兩個注冊表:
1. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort
如果沒有,則手動創建 DWord(32位) ”數值數據“改為十進制65534 或者認為適當的值。此值表示 用戶最大使用的端口數量,默認為5000。
2. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\TCPTimedWaitDelay
如果沒有,則手動創建 DWord(32位) ”數值數據“改為十進制30 或者你認為適當的值。此值表示一個關閉后的端口等待多久之后可以重新使用,默認為120秒,也就是2分鍾 才可以重新使用。
退出注冊表編輯器,然后重新啟動計算機
MaxUserPort在MSDN的描述:https://technet.microsoft.com/zh-cn/library/cc938196.aspx
MaxUserPort
- 描述:確定當應用程序向系統請求獲取可用的用戶端口時,TCP/IP 可指定的最高端口號。
- 如何查看或設置:
1. 使用regedit命令,訪問 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters 注冊表子鍵,然后創建新的 REG_DWORD 值MaxUserPort。
2. 將此值至少設置為十進制32768。
3. 停止並重新啟動系統。
-
- 缺省值:無
- 建議值:至少為十進制32768。
- 建議將 MaxUserPort 值設置為 60000(十六進制ea60)。如果設置的 MaxUserPort 值低於 60000,可能會顯示名稱服務提供程序接口 (NSPI) 代理警告,例如事件 9040。
TcpTimedWaitDelay
- 描述:確定 TCP/IP 在釋放已關閉的連接並再次使用其資源前必須經過的時間。關閉與釋放之間的這段時間稱為 TIME_WAIT 狀態或者兩倍最大段生存期(2MSL)狀態。此時間期間,重新打開到客戶機和服務器的連接的成本少於建立新連接。通過減少此條目的值,TCP/IP 可以更快地釋放關閉的連接,並為新連接提供更多資源。如果運行中的應用程序要求快速釋放連接或創建新連接,或者由於多個連接處於 TIME_WAIT 狀態而導致吞吐量較低,請調整此參數。
- 如何查看或設置:
1. 使用regedit命令,訪問 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters 注冊表子鍵,然后創建新的 REG_DWORD 值TcpTimedWaitDelay。
2. 將此值設置為十進制30,即十六進制 0x0000001e。此值將等待時間設置為 30 秒。
3. 停止並重新啟動系統。
-
- 缺省值:0xF0,此值將等待時間設置為 240 秒(4 分鍾)。
- 建議值:最小值為0x1E,此值將等待時間設置為 30 秒。