WCF 本質是通訊服務框架,支援多種傳輸協定、訊息編碼、WS-* 規範,所有細節由通道堆疊處理。為簡化開發,WCF 提供兩層模型:應用程式程式模型(面向開發者)與通道模型(底層通訊)。掌握通道模型能深入理解 WCF 架構,構建更健壯服務或擴展框架。
通道堆疊為分層通訊堆疊,底層傳輸通道適配底層傳輸(HTTP、TCP、Named Pipe 等),上層協定通道處理安全性、可靠性、交易等。訊息以 Message 物件流經各通道:

GetProperty<T>() 存取堆疊功能所有通訊物件實作 ICommunicationObject 定義狀態機:
public interface ICommunicationObject
{
CommunicationState State { get; }
event EventHandler Closed, Closing, Faulted, Opened, Opening;
void Abort(); void Close(); void Close(TimeSpan timeout);
void Open(); void Open(TimeSpan timeout);
IAsyncResult BeginClose(AsyncCallback cb, object state);
IAsyncResult BeginOpen(AsyncCallback cb, object state);
void EndClose(IAsyncResult result); void EndOpen(IAsyncResult result);
}

Created → Opening → Opened → Closing → Closed。發生不可復原錯誤轉 Faulted → Closed。狀態單向流轉,不可逆:

擴展 IChannel 的五種形狀對應訊息交換模式:


IChannelListener 由 Binding 建立,負責建立通道接收訊息並向上傳遞。內建對應各協定的 Listener(TransactionChannelListener、TcpChannelListener 等):

IChannelFactory 建立通道處理向下發送。內建對應各協定的 Factory(TcpChannelFactory 等):

WCF 提供 CommunicationObject、ChannelFactoryBase 等基類簡化自訂實作,但它們不屬於通道模型核心介面,僅為實作輔助。
本文系統闡述 WCF 通道程式模型核心架構:通道堆疊、三大介面、狀態機、五種通道型別、監聽器/工廠分工。下篇將以實例深化理解。