不論是我們采用SOAP還是REST架構風格,運行時框架體系依然不曾改變,終結點也仍舊是通信的核心。在Web HTTP編程模型中,我們采用基于WebHttpBinding綁定的終結點。綁定是一組相關綁定元素的有序組合,綁定的特性與能力決定于它包含的綁定元素,在這里我們通過分析綁定元素的方式來剖析WebHttpBinding綁定與其它綁定有何不同。采用HTTP/HTTPS通信協議的WebHttpBinding具有一些與WSHttpBinding/WS2007HttpBinding相同的屬性,在這里我們只關心如下定義的Security屬性。
1: public class WebHttpBinding : Binding, IBindingRuntimePreferences
2: {
3: //其它成員
4: public WebHttpSecurity Security { get; }
5: }
6: public sealed class WebHttpSecurity
7: {
8: // 其它成員
9: public WebHttpSecurityMode Mode { get; set; }
10: public HttpTransportSecurity Transport { get; }
11: }
12: public enum WebHttpSecurityMode
13: {
14: None,
15: Transport,
16: TransportCredentialOnly
17: }
基于SOAP的綁定一般具有兩種基本的安全模式,即Message和Transport模式。對于前者,它是完全建立在WS-Security為核心的安全協議之上的,而整個WS-*協議簇都是基于SOAP的,所以自然不能應用在WebHttpBinding上,所以它只能通過HTTPS提供針對Transport模式的安全支持。具體來說,通過枚舉WebHttpSecurityMode表示的安全模式具有如下三個選項:
None:HTTP 請求未使用任何安全性;
Transport:HTTP 請求使用傳輸級安全性;
TransportCredentialOnly:僅提供基于 HTTP 的客戶端身份驗證。
一、WebHttpBinding的綁定元素
現在我們根據上述三種不同的安全模式創建相應的WebHttpBinding對象,然后通過如下的程序在控制臺中答應出所有的綁定元素類型。
1: static void Main(string[] args)
2: {
3: WebHttpBinding binding = new WebHttpBinding(WebHttpSecurityMode.None);
4: ListBindingElements(binding);
5:
6: binding = new WebHttpBinding(WebHttpSecurityMode.Transport);
7: ListBindingElements(binding);
8:
9: binding = new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly);
10: ListBindingElements(binding);
11: }
12: static void ListBindingElements(WebHttpBinding binding)
13: {
14: int index = 1;
15: Console.WriteLine(binding.Security.Mode + ":");
16: foreach (var element in binding.CreateBindingElements())
17: {
18: Console.WriteLine("{0}. {1}", index++, element.GetType().FullName);
19: }
20: Console.WriteLine();
21: }
上述的程序執行之后會在控制臺上產生如下的輸出,從中我們不難看出三個WebHttpBinding均由一個消息編碼元素和傳輸元素組成,我們知道這兩種綁定元素最所有類型的綁定所必需的。
1: None:
2: 1. System.ServiceModel.Channels.WebMessageEncodingBindingElement
3: 2. System.ServiceModel.Channels.HttpTransportBindingElement
4:
5: Transport:
6: 1. System.ServiceModel.Channels.WebMessageEncodingBindingElement
7: 2. System.ServiceModel.Channels.HttpsTransportBindingElement
8:
9: TransportCredentialOnly:
10: 1. System.ServiceModel.Channels.WebMessageEncodingBindingElement
11: 2. System.ServiceModel.Channels.HttpTransportBindingElement
對于WebHttpBinding的兩個綁定元素來說,由于它通過HTTPS提供針對Transport安全的支持,所以當安全模式為Transport時對應的傳輸綁定元素為HttpsTransportBindingElement,對于其余兩種安全模式則直接采用HttpTransportBindingElement作為傳輸綁定元素?,F在我們著重討論是作為消息編碼綁定元素的WebMessageEncodingBindingElement類型,以及它涉及的消息編碼機制。
二、消息編碼
我們先來看看WebMessageEncodingBindingElement的基本的定義。如下面的代碼片斷所示,它是MessageEncodingBindingElement的子類,并且具有與TextMessageEncodingElement類似的屬性定義。其中MaxReadPoolSize和MaxWritePoolSize表示表示無需分配新的XmlDictionaryReader/XmlDictionaryWriter便可以讀取的便可同時讀取/寫入的最大消息數,默認值分別是64和16。ReaderQuotas屬性返回用于約束讀取的XML的復雜度的XmlDictionaryReaderQuotas對象,而WriteEncoding屬性表示采用的字符編碼類型,默認采用UTF-8編碼方式。由于WebHttpBinding不使用SOAP,表示消息版本的MessageVersion屬性自然返回None,如果我們對該屬性進行設置,指定的屬性值也只能是MessageVersion.None。
1: public sealed class WebMessageEncodingBindingElement : MessageEncodingBindingElement,...
2: {
3: //其它成員
4: public override MessageEncoderFactory CreateMessageEncoderFactory();
5:
6: public bool CrossDomainScriptAccessEnabled {get; set; }
7: public WebContentTypeMapper ContentTypeMapper { get; set; }
8:
9: public int MaxReadPoolSize { get; set; }
10: public int MaxWritePoolSize { get; set; }