WCF 區分 傳輸可靠性 (Transport Reliability,如 TCP 封包重傳) 與 訊息可靠性 (Message Reliability,端對端訊息保證)。訊息可靠性基於 WS-ReliableMessaging (WS-RM) 標準,提供跨中介節點的端對端保證。
| Binding | 可靠會話 | 預設 | 有序傳遞 | 預設 | 協定 |
|---|---|---|---|---|---|
| BasicHttpBinding | 不支援 | - | 不支援 | - | - |
| NetTcpBinding | 支援 | Off | 支援 | On | WS-RM (OleTx/WS-AT) |
| NetNamedPipeBinding | 固有可靠 | On | 固有有序 | On | 內部管道 |
| WSHttpBinding | 支援 | Off | 支援 | On | WS-RM (WS-AT) |
| WSDualHttpBinding | 支援 | On | 支援 | On | WS-RM (雙 HTTP 通道) |
| NetMsmqBinding | 不支援 | - | 不支援 | - | MSMQ 佇列 |
組態:
<netTcpBinding>
<binding name="reliableTcp">
<reliableSession enabled="true" ordered="true" inactivityTimeout="00:10:00" />
</binding>
</netTcpBinding>
程式碼**:
var binding = new NetTcpBinding(SecurityMode.Transport, true); // 第二參數 reliableSession=true
binding.ReliableSession.Ordered = true;
binding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
用戶端與服務端設定必須一致,否則通訊失敗。
服務實作若依賴訊息順序 (如狀態機、序列化處理),應宣告需求:
[DeliveryRequirements(RequireOrderedDelivery = true, QueuedDeliveryRequirements = QueuedDeliveryRequirementsMode.Allowed)]
public class OrderedService : IOrderedService { ... }
NetNamedPipeBinding 固有有序,自動滿足。<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="true" />
確認 ordered="true" 且 enabled="true"。
InactivityTimeout 過短導致連線頻繁中斷,過長佔用伺服器資源,建議 5-30 分鐘視業務調整。transferMode=Streamed) 不相容 可靠會話。.NET Core / gRPC 採用 HTTP/2 多工、內建流控與重試;ASP.NET Core 搭配 Polly 實現應用層重試策略。新架構傾向應用層冪等設計 + 基礎設施重試,而非依賴 WS-RM 協定。
WCF 可靠會話提供標準化的端對端訊息保證,適用於需跨中介節點、不可接受訊息遺漏/亂序的企業整合場景。正確選擇支援綁定、配置有序/非作用中逾時、宣告 DeliveryRequirements,並評估效能成本,是穩健應用的關鍵。