客戶端呼叫服務拋出 TimeoutException,堆疊顯示 HttpChannelFactory.HttpRequestChannel.WaitForReply 等待回應超過 1 分鐘(預設 SendTimeout)。
並發連接數超過 WCF 預設節流限制。預設配置:
maxConcurrentCalls = 16(最大並發呼叫數)maxConcurrentSessions = 10(最大會話數,PerSession 模式關鍵)maxConcurrentInstances = 26(最大實例數)若客戶端數量超過 10 且未正確關閉通道,會話佔用不釋放,新請求排隊最終超時。
var client = new ServiceClient();
try
{
client.DoWork();
client.Close(); // 正常關閉,釋放會話
}
catch
{
client.Abort(); // 異常時中止,避免卡在關閉狀態
}
使用 using 區塊不可靠,因 Dispose() 內部呼叫 Close() 可能拋出例外導致通道未釋放。
在服務端 web.config / app.config 中調整 <serviceThrottling>:
<serviceThrottling
maxConcurrentCalls="100"
maxConcurrentSessions="100"
maxConcurrentInstances="200" />
參數說明:
maxConcurrentCalls:同時處理的訊息數上限。maxConcurrentSessions:同時活躍的會話數上限(PerSession 服務關鍵)。maxConcurrentInstances:服務實例總數上限,建議設為前兩者之和或更大。減少 receiveTimeout、inactivityTimeout,加速閒置會話回收。
try/finally 確保 Close()/Abort()。ServiceModelService 4.0.0.0\Calls Outstanding、Sessions Outstanding。PerCall 或 Singleton 實例模式,降低會話壓力。WCF 連接數耗盡是高併發場景常見陷阱。結合「客戶端嚴謹關閉通道」與「服務端合理配置節流」,可徹底解決超時問題,保障服務穩定性。