WCF系列:Binding模型 從綁定元素認識系統預定義綁定

由於綁定對象由一系列有序的綁定元素組成,綁定元素最終決定着信道棧中信道的組成,而信道的組成最終又決定了信道棧對消息進行處理的方式和能力,所有要確定綁定的特性和能力,我們可以通過查看其綁定元素的構成來一窺究竟。爲此我們我們寫了一個簡單的方法,用於列出一個具體的綁定對象所有的綁定元素,在介紹一個個具體的系統綁定中,我會使用該方法:
  1. static void ListAllBindingElements(Binding binding)
  2. {
  3.     BindingElementCollection elements = binding.CreateBindingElements();
  4.     for (int i = 0; i < elements.Count; i++)
  5.     {
  6. Console.WriteLine("{0}. {1}", i+1, elements.GetType().FullName);
  7.     }
  8. }
複製代碼
1.BasicHttpBinding
我們通過調用默認的構造函數創建一個綁定對象,並藉助上面的ListAllBindingElements方法列出該綁定對象所有的綁定元素:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         BasicHttpBinding binding = new BasicHttpBinding();
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
將會得到如下的輸出,從中可以看出在默認的情況下,一個BasicHttpBinding包含兩個最基本的綁定元素:最爲傳輸元素的HttpTransportBindingElement和作爲消息編碼元素的TextMessageEncodingBindingElement。所以BasicHttpBinding在默認的情況下采用HTTP傳輸協議,和基於文本的消息編碼方式。之所以將此綁定命名爲BasicHttpBinding,很大程度上緣於它僅僅包含一些最基本的用於消息通信的元素。

1. System.ServiceModel.Channels.TextMessageEncodingBindingElement
2. System.ServiceModel.Channels.HttpTransportBindingElement


除了提供最基本的傳輸和編碼功能外,BasicHttpBinding還提供了對安全的支持,無論是基於傳輸的安全還是基於消息的安全,都可以通過對綁定進行相應的設置實現。我們同樣通過列出綁定元素的方式來證明這一點。下面的代碼中,在創建BasicHttpBinding對象的時候,指定一個BasicHttpSecurityMode.Transport參數將安全模式設爲傳輸安全模式:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
在最終的輸出中我們可以看到,傳輸綁定元素由HttpTransportBindingElement變成了HttpsTransportBindingElement,由此可以看出BasicHttpBinding通過HTTPS實現傳輸安全。

1. System.ServiceModel.Channels.TextMessageEncodingBindingElement
2. System.ServiceModel.Channels.HttpsTransportBindingElement


如果我們設置成基於消息的安全模式,並將客戶端的憑證類型(Client Credential Type)設爲證書(Certificate,這對於基於消息安全模式的BasicHttpBinding是必須的)。
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Message);
  6.         binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
  7.         ListAllBindingElements(binding);
  8.     }
  9. }
複製代碼
那麼通過輸出,我們可以看到在原有綁定元素之上,又多出了一個新的綁定元素:AsymmetricSecurityBindingElement,該元素通過非對稱加密(也就是基於X509證書的加密方式)的方式實現了基於消息的安全。

1. System.ServiceModel.Channels.AsymmetricSecurityBindingElement
2. System.ServiceModel.Channels.TextMessageEncodingBindingElement
3. System.ServiceModel.Channels.HttpsTransportBindingElement


對於BasicHttpBinding來說,默認採用基於文本的消息編碼方式(TextMessageEncodingBindingElement),實際上BasicHttpBinding還提供對基於MTOM編碼方式的支持。我們可以通過編程或者配置的方式對消息編碼方式進行顯式指定。在下面的代碼中,通過MessageEncoding屬性將編碼方式指定爲:WSMessageEncoding.Mtom。
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         BasicHttpBinding binding = new BasicHttpBinding();
  6.         binding.MessageEncoding = WSMessageEncoding.Mtom;
  7.         ListAllBindingElements(binding);
  8.     }
  9. }
複製代碼
那麼我們我們最終輸出的綁定元素列表中,TextMessageEncodingBindingElement將會被實現MTOM消息編碼的MtomMessageEncodingBindingElement代替。

1. System.ServiceModel.Channels.MtomMessageEncodingBindingElement
2. System.ServiceModel.Channels.HttpTransportBindingElement


BasicHttpBinding是WS-BP 1.1 Spec (Basic Profile) 標準的,ASP.NET ASMX Web Service的很多標準存在於WS-BP 1.1 Spec中,比如SOAP 1.1、WSDL 1.1、Message Security 1.0等等,所以BasicHttpBinding可以和傳統的ASP.NET ASMX Web Service進行互操作。

2. WsHttpBinding
我們通過與BasicHttpBinding的方式來分析WsHttpBinding,先通過下面的方式列出在默認條件下(通過默認的構造函數創建WsHttpBinding對象)該綁定對象具有的所有綁定元素:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         WsHttpBinding binding = new WsHttpBinding();
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
從下面的輸出來看,從上到下一共包含4個綁定元素:TransactionFlowBindingElement、SymmetricSecurityBindingElement、TextMessageEncodingBindingElement和HttpTransportBindingElement。TransactionFlowBindingElement實現了對事物流轉;SymmetricSecurityBindingElement通過對稱加密的方式實現基於消息的安全;TextMessageEncodingBindingElement和HttpTransportBindingElement表明WsHttpBinding和BasicHttpBinding一樣採用基於文本的編碼方式和基於HTTP的傳輸協議。

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.SymmetricSecurityBindingElement
3. System.ServiceModel.Channels.TextMessageEncodingBindingElement
4.System.ServiceModel.Channels.HttpTransportBindingElement


在這了需要特別指出的就是WsHttpBinding對事務的支持。對於SOA來說,事務永遠是一個重要的主題,我們不僅僅需要單方的事務支持,比如將服務端的操作納入一個單一的事務之中,也需要事務的流轉,將從客戶端開始的事務自動流向服務端;不僅僅需要基於單次服務調用的事務,還需要基於多次服務訪問的事務(將多次服務調用納入同一個事務之中);不僅僅需要基於單一平臺的事務支持,還需要跨平臺的事務(比如將基於.NET平臺的WCF服務調用和基於J2EE平臺的Web服務調用納入同一個事務中)。在WS-*體系中,WS-T(Transactions)爲事務定義了規範,而在WCF中,則通過TransactionFlowBindingElement實現了WS-Transactions規範。

WsHttpBinding在默認的情況下就提供了對基於消息安全的支持,此外WsHttpBinding仍然提供基於HTTPS的傳輸安全。在下面我們對代碼稍加改動,通過構造函數將WsHttpBinding設置爲基於傳輸的安全模式:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         WsHttpBinding binding = new WsHttpBinding(SecurityMode.Transport);
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
那麼基於消息模式的SymmetricSecurityBindingElement將被去除,而作爲傳輸綁定元素的HttpTransportBindingElement將被替換成HttpsTransportBindingElement,藉此實現基於HTTPS的傳輸安全。

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.TextMessageEncodingBindingElement
3.System.ServiceModel.Channels.HttpsTransportBindingElement


除了提供對傳輸和消息安全的支持之外,WsHttpBinding還對傳輸的可靠性提供支持。可靠性消息傳輸確保在網絡環境不好的情況下消息的有效、有序抵達。WS-*通過WS-RM(Reliable Messaging)爲可靠傳輸定義了規範,在WCF中WS-RM通過可靠會話(Reliable Session)實現了WS-RM,而WS-RM在WCF的實現通過ReliableSessionBindingElement承載。下面的代碼中,我們通過另一個構造函數設定WsHttpBinding對可靠會話的支持(第二個參數代表是否支持可靠會話)。
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message, true);
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
最終的輸出將包含五個綁定元素,第二個就是實現了可靠會話的ReliableSessionBindingElement。

1.System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.ReliableSessionBindingElement
3. System.ServiceModel.Channels.SymmetricSecurityBindingElement
4. System.ServiceModel.Channels.TextMessageEncodingBindingElement
5.System.ServiceModel.Channels.HttpTransportBindingElement


此外,和BasicHttpBinding一樣,WsHttpBinding定義了類型爲System.ServiceModel.WSMessageEncoding枚舉類型的MessageEncoding屬性,有兩種WSMessageEncoding枚舉值供你選擇:Text和MTOM。

綜上所述,WsHttpBinding對大部分的WS-*提供支持,這包括我們上面提到的WS-Transactions、WS-Security、WS-Reliable Messaging等等。所以從互操作角度講,WsHttpBinding可以和滿足這些標準的Web Service進行互操作。

3. WsDualHttpBinding
在前面對消息交換模式的介紹中,我們談到三種典型的消息交換模式:單向的數據報模式、請求/回覆模式和雙工模式。WsDualHttpBinding就是專門爲HTTP傳輸下雙工消息交換模式設計的。

除了基於傳輸的安全之外,WsHttpbing的所有的特性都被WsDualHttpBinding繼承下來,這包括:基於HTTP的傳輸、基於文本和MTOM的消息編碼、WS-Security、WS-Transactions、WS-Reliable Messaging(Reliable Session)等等。我們仍然通過輸出綁定元素的方式證明這一點:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         WSDualHttpBinding binding = new WSDualHttpBinding();
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
下面列出了輸出的所有綁定元素,從中可以看出TransactionFlowBindingElement對WS-Transactions的支持;ReliableSessionBindingElement對WS-RM的支持;SymmetricSecurityBindingElement對WS-Security的支持;這些綁定元素和TextMessageEncodingBindingElement、HttpTransportBindingElement都是與WsHttpbing共有的,而CompositeDuplexBindingElement和OneWayBindingElement則是WsDualHttpBinding不具有的,這兩個綁定元素實現雙工通信和單項的數據報模式通信。

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.ReliableSessionBindingElement
3. System.ServiceModel.Channels.SymmetricSecurityBindingElement
4. System.ServiceModel.Channels.CompositeDuplexBindingElement
5. System.ServiceModel.Channels.OneWayBindingElement
6. System.ServiceModel.Channels.TextMessageEncodingBindingElement
7. System.ServiceModel.Channels.HttpTransportBindingElement

對於WsHttpbing和WsDualHttpBinding的比較,還有一點值得注意的是在默認情況下,WsHttpbing並沒有ReliableSessionBindingElement,也就是說在默認的情況下,WsHttpbing並不支持可靠會話,而對於基於雙工通信的WsDualHttpBinding,可靠會話則是必須的。至於WsDualHttpBinding爲何不支持基於傳輸的安全,原因也很簡單,因爲HTTP協議下的傳輸安全通過HTTPS(SSL)實現,HTTPS依賴於一個真正意義上的Web站點,也就是隻有訪問一個真正意義上Web站點的資源的前提下,HTTPS纔會有有意義。而對於雙工通信來說,由於客戶端滿足這樣要求,所以從服務端回調客戶端的傳輸安全是無法確保的。

雙工通信需要一個雙工的通信通道,但是屬性TCP/IP的讀者應該很清楚,HTTP協議僅僅是一個單純的請求/回覆通信協議,也就是說基於HTTP的通信通道不可以支持雙工通信,那麼WsDualHttpBinding又是如果在HTTP傳輸協議上實現雙工通信的呢?答案很簡單,WsDualHttpBinding採用了兩個HTTP通道。

4. NetTcpBinding
到此爲止,我們一共介紹了三種類型的綁定。從對於傳輸協議的支持來看,它們都就是基於HTTP或者HTTPS的綁定;從對標準的支持看來,BasicHttpBinding提供對WS-BP 1.1的支持,WsHttpBinding和WsDualHttpBinding則對WS-*新的協議提供很好的支持,比如WS-Transactions、WS-Reliable Messaging、WS-Security等等;從消息編碼的角度來看,它們均支持基於純文本的消息編碼和MTOM編碼。這些屬性都決定了這三種綁定具有較好的互操作性,也就是說,對於此三種綁定的應用並不限於對於基於.NET平臺應用的交互,如果通過這些綁定寄宿我們的服務,其他平臺的客戶端可以調用我們的服務,同理我們也可以利用基於這些綁定的客戶端訪問其他非.NET平臺的Web服務,只要對方支持相應的標準。
接下來我們要介紹的另外三種綁定,相比之下就不具有如此好的互操作性,它們只能應用於單純的WCF客戶端和服務之間的交互。它們基於不同的傳輸協議,我們先來介紹基於TCP傳輸協議的NetTcpBinding。

我們照例採用列出綁定元素列表的方式分析綁定的特性,我們先通過下面的代碼看看一個採用默認構造函數創建的NetTcpBinding對象會包含哪些綁丁元素。
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         NetTcpBinding binding = new NetTcpBinding();
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
程序運行後,下面4個綁定元素會被先後輸出。我們借來分析一個NetTcpBinding對象在默認的情況下具有哪些特性:TcpTransportBindingElement表明採用TCP作爲傳輸協議;WindowsStreamSecurityBindingElement提供基於Windows憑證的傳輸安全;BinaryMessageEncodingBindingElement實現基於二進制的消息編碼;TransactionFlowBindingElement則提供對事務流轉的支持。

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.BinaryMessageEncodingBindingElement
3. System.ServiceModel.Channels.SslStreamSecurityBindingElement
4. System.ServiceModel.Channels.TcpTransportBindingElement


上面涉及的四個綁定元素,除了WindowsStreamSecurityBindingElement,相信有了前面的介紹,讀者不會感到陌生。在這裏我們來簡單討論一下WindowsStreamSecurityBindingElement。WindowsStreamSecurityBindingElement繼承自System.ServiceModel.Channels.StreamUpgradeBindingElement,StreamUpgradeBindingElement是一種特殊的綁定元素。前面我們講了,綁定元素的使命在於對相應信道的創建,而StreamUpgradeBindingElement的特別之處在於它並會參與信道的創建。StreamUpgradeBindingElement一般應用於基於流的傳輸(Stream Oriented Transport),比如TCP、命名管道等等。它一般位於TransportBindingElement之上,在傳輸層基礎上提供進一步的升級處理(Transport Upgrade),比如安全加密、壓縮等等。WindowsStreamSecurityBindingElement在這裏的提供基於Windows客戶端憑證的傳輸安全,與之相對的,還有一個System.ServiceModel.Channels.SslStreamSecurityBindingElement,提供基於SSL的傳輸安全。如果我們將綁定的客戶端憑證的類型改成Certificate或者None,SslStreamSecurityBindingElement將會被採用:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         NetTcpBinding binding = new NetTcpBinding();
  6.         binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
  7.         ListAllBindingElements(binding);
  8.     }
  9. }
複製代碼
WindowsStreamSecurityBindingElement將會被SslStreamSecurityBindingElement替換:

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.BinaryMessageEncodingBindingElement
3. System.ServiceModel.Channels.SslStreamSecurityBindingElement
4. System.ServiceModel.Channels.TcpTransportBindingElement


除了對傳輸安全模式的支持(默認),NetTcpBinding也提供對消息安全模式提供支持,比如下面的代碼中,再調用構造函數的時候直接將安全模式類型指定爲:SecurityMode.Message。
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
實際上,如果採用消息安全模式,SymmetricSecurityBindingElement將會添加進來實現基於消息級別的簽名、加密安全措施。這也可以從下面的輸出結果看出來:

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.SymmetricSecurityBindingElement
3. System.ServiceModel.Channels.BinaryMessageEncodingBindingElement
4. System.ServiceModel.Channels.TcpTransportBindingElement


除了單純的傳輸安全模式和消息模式之外,NetTcpBinding還支持一種混合的安全模式,該模式的SecurityMode枚舉值表示爲:SecurityMode.TransportWithMessageCredential。該模式通過傳輸安全保障數據的一致性和保密性,通過消息安全提供身份驗證。關於不同種類的安全模式,將在“安全”一章中進行詳細講解。SslStreamSecurityBindingElement和TransportSecurityBindingElement一起提供該模式的安全,如下面的代碼所示:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         NetTcpBinding binding = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
輸出結果:

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.TransportSecurityBindingElement
3. System.ServiceModel.Channels.BinaryMessageEncodingBindingElement
4. System.ServiceModel.Channels.SslStreamSecurityBindingElement
5. System.ServiceModel.Channels.TcpTransportBindingElement


和WsHttpBinding一樣,NetTcpBinding也提供對可靠會話的支持,以保障數據包或者消息的可靠、有序傳遞。不過與WsHttpBinding的實現機制不同的是,基於NetTcpBinding是採用TCP協議固有的可靠傳輸機制,比如消息確認機制、重發機制等等。下面的代碼,通過ReliableSession.Enabled屬性讓綁定實現對可靠會話的支持:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         NetTcpBinding binding = new NetTcpBinding();
  6.         binding.ReliableSession.Enabled = true;
  7.         ListAllBindingElements(binding);
  8.     }
複製代碼
}

和WsHttpBinding一樣,通過ReliableSessionBindingElement實現對可靠會話的支持:

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.ReliableSessionBindingElement
3. System.ServiceModel.Channels.BinaryMessageEncodingBindingElement
4. System.ServiceModel.Channels.WindowsStreamSecurityBindingElement
5. System.ServiceModel.Channels.TcpTransportBindingElement


由於NetTcpBinding採用TCP作爲傳輸協議,所以它一般只應用於Intranet中;由於採用二進制的消息編碼方式,在性能上較之基於文本的編碼會有較大的提高;此外,由於和HTTP協議不同,TCP本身就是一個基於雙工通信的協議,所以和WsDualBinding一樣可以用於基於雙工消息交換模式的WCF應用中。

5. NetNamedPipeBinding
NetNamedPipeBinding,顧名思義,就是基於命名管道傳輸的綁定。命名管道本身可以支持跨機器的通信,而在WCF中對NetNamedPipeBinding作了更加嚴格的限制,使其只能用於同一臺機器的跨進程通信(IPC)。所以在所有的綁定中,NetNamedPipeBinding將是性能最好的綁定類型。

我們照例通過分析綁定元素的方式來理解綁定本身的特性與能力。先通過下面的代碼列出NetNamedPipeBinding默認的綁定元素:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         NetNamedPipeBinding binding = new NetNamedPipeBinding();
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
從輸出的綁定元素集合,我們可以得出這樣的結論:NamedPipeTransportBindingElement實現了基於命名管道的傳輸;
WindowsStreamSecurityBindingElement提供了基於Windows憑證的傳輸安全保障;BinaryMessageEncodingBindingElement實現了基於二進制的消息編碼;而TransactionFlowBindingElement則爲事務流轉提供支持。

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.BinaryMessageEncodingBindingElement
3. System.ServiceModel.Channels.WindowsStreamSecurityBindingElement
4. System.ServiceModel.Channels.NamedPipeTransportBindingElement


由於NetNamedPipeBinding的特殊性(提供基於IPC的通信),所以決定了它的一些相關的特性:僅僅支持傳輸模式的安全(實際上消息安全模式在IPC場景下已經沒有意義);客戶端憑證之限於Windows。我們可以將安全模式設爲None,使其不採用任何安全模式:
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
  6.         ListAllBindingElements(binding);
  7.     }
  8. }
複製代碼
這樣,WindowsStreamSecurityBindingElement將從綁定元素集合中剔除:

1. System.ServiceModel.Channels.TransactionFlowBindingElement
2. System.ServiceModel.Channels.BinaryMessageEncodingBindingElement
3. System.ServiceModel.Channels.NamedPipeTransportBindingElement

除了上述的五種綁定類型,WCF中還定義了其他一些綁定,比如NetMsmqBinding、MsmqIntegrationBinding、WebHttpBinding等等,將會在具體設計到這些特殊的綁定的章節中介紹。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章