WCF客戶(hù)端處理具體實(shí)現(xiàn)方法淺析
WCF的客戶(hù)端處理方法是一個(gè)比較基礎(chǔ)的應(yīng)用操作,我們需要在實(shí)際編程中去不斷的積累這方面的經(jīng)驗(yàn),來(lái)達(dá)到一種應(yīng)用熟練的程度。今天我們就會(huì)為大家詳細(xì)介紹一下WCF客戶(hù)端處理的相關(guān)方法。#t#
WCF客戶(hù)端處理的自動(dòng)生成實(shí)例中 是從ClientBase<of T>.Channel屬性開(kāi)始的,最終要?jiǎng)?chuàng)建T的透明代理,然后調(diào)用。 以BasicHttpBinding為例,客戶(hù)端請(qǐng)求的主要步驟如下:
1 根據(jù)傳入的Binding和EndpointAddress生成ServiceEndpoint
2 再根據(jù)ServiceEndpoint的類(lèi)型生成ServiceChannelFactory 類(lèi)的實(shí)例。當(dāng)前BasicHttpBinding 生成的應(yīng)該是ServiceChannelFactoryOverRequest類(lèi)的實(shí)例,對(duì)應(yīng)的IChannelBinder是RequestChannelBinder
注:
basicHttpBinding.BuildChannelFactory<IRequestChannel>要對(duì) basicHttpBinding所有的綁定元素進(jìn)行遍歷。默認(rèn)情況下,不啟用https,則傳輸元素使用HttpTransportBindingElement,該對(duì)象重寫(xiě)B(tài)uildChannelFactory<IRequestChannel>,返回值是HttpChannelFactory
RequestChannelBinder對(duì)象最重要的字段是channel,對(duì)應(yīng)的值是HttpChannelFactory.CreateChannel(),返回的值是HttpChannelFactory.HttpRequestChannel
3 生成ServiceChannel,將ServiceChannelFactoryOverRequest和RequestChannelBinder做為參數(shù)傳入ServiceChannel。構(gòu)造函數(shù)為ServiceChannel(ServiceChannelFactory factory, IChannelBinder binder)
4. 生成T的透理代理ServiceChannelProxy,將ServiceChannel做為參數(shù)傳入ServiceChannelProxy,構(gòu)造
5.在調(diào)用透明代理相應(yīng)的方法時(shí),調(diào)用ServiceChannelProxy.Invoke(),如果是Service,調(diào)用ServiceChannel.Call(),此時(shí)實(shí)質(zhì)是調(diào)用ServiceChannel封裝的IChannelBinder(當(dāng)前是RequestChannelBinder)的call,
6 調(diào)用RequestChannelBinder.Request(),注意步驟2***一句,此時(shí)channel是HttpChannelFactory.HttpRequestChannel HttpChannelFactory.HttpRequestChannel創(chuàng)建HttpChannelRequest的請(qǐng)求,然后調(diào)用HttpChannelRequest.SendRequest發(fā)送消息。其實(shí)質(zhì)就是封裝一個(gè)HttpWebRequest,將Message發(fā)送到服務(wù)器端address里,根,webservice的最終原理是一樣的。因此,要抓住幾個(gè)關(guān)系點(diǎn),從總體上把握客戶(hù)端請(qǐng)求的流程
(1 ServiceChannelFactory 類(lèi)的實(shí)例是什么類(lèi)型
(2 IChannelBinder接口的實(shí)現(xiàn)是什么類(lèi)型
(3 IChannelBinder.Channel是什么
BindingElement.BuildChannelFactory<TChannel>
這個(gè)方法很有意思,默認(rèn)的實(shí)現(xiàn)是通用BindingContext
將當(dāng)前Binding對(duì)象中的所有元素(BindingElementCollection對(duì)象的實(shí)例),one by one 的進(jìn)行遍歷,每次移走一個(gè),取出,然后再次調(diào)用BuildChannelFactory<TChannel>
舉個(gè)例子
對(duì)于BasicHttpBinding對(duì)象來(lái)說(shuō),封裝了
傳輸綁定元素
HttpsTransportBindingElement httpsTransport;
指定 HTTPS 傳輸以傳輸消息的綁定元素。
HttpTransportBindingElement httpTransport;
用于指定 HTTP 傳輸以傳輸消息的綁定元素
協(xié)議通道元素 (安全)
BasicHttpSecurity security;
配置 basicHttpBinding 綁定的安全設(shè)置。
消息編碼綁定元素
MtomMessageEncodingBindingElement mtomEncoding;
指定消息傳輸優(yōu)化機(jī)制 (MTOM) 消息所使用的編碼和版本管理的綁定元素。
TextMessageEncodingBindingElement textEncoding;
指定用于基于文本的 SOAP 消息的字符編碼與消息版本管理。此時(shí),BindingElementCollection中有以上元素,先從集合中移出一個(gè), 調(diào)用一次BuildChannelFactory<TChannel>
HttpTransportBindingElement httpTransport 重寫(xiě)了BuildChannelFactory<TChannel> 返回 HttpChannelFactory。其它的綁定元素基本上調(diào)BindingElement的,是直接跳到下一個(gè),所以,
BasicHttpBinding.BuildChannelFactory<IRequestChannel>()返回的是HttpChannelFactory。
以上就是我們?yōu)榇蠹医榻B的WCF客戶(hù)端處理的相關(guān)方法。