新聞中心
WCF的客戶端處理方法是一個比較基礎的應用操作,我們需要在實際編程中去不斷的積累這方面的經(jīng)驗,來達到一種應用熟練的程度。今天我們就會為大家詳細介紹一下WCF客戶端處理的相關方法。#t#

WCF客戶端處理的自動生成實例中 是從ClientBase
1 根據(jù)傳入的Binding和EndpointAddress生成ServiceEndpoint
2 再根據(jù)ServiceEndpoint的類型生成ServiceChannelFactory 類的實例。當前BasicHttpBinding 生成的應該是ServiceChannelFactoryOverRequest類的實例,對應的IChannelBinder是RequestChannelBinder
注:
basicHttpBinding.BuildChannelFactory
RequestChannelBinder對象最重要的字段是channel,對應的值是HttpChannelFactory.CreateChannel(),返回的值是HttpChannelFactory.HttpRequestChannel
3 生成ServiceChannel,將ServiceChannelFactoryOverRequest和RequestChannelBinder做為參數(shù)傳入ServiceChannel。構造函數(shù)為ServiceChannel(ServiceChannelFactory factory, IChannelBinder binder)
4. 生成T的透理代理ServiceChannelProxy,將ServiceChannel做為參數(shù)傳入ServiceChannelProxy,構造
5.在調用透明代理相應的方法時,調用ServiceChannelProxy.Invoke(),如果是Service,調用ServiceChannel.Call(),此時實質是調用ServiceChannel封裝的IChannelBinder(當前是RequestChannelBinder)的call,
6 調用RequestChannelBinder.Request(),注意步驟2***一句,此時channel是HttpChannelFactory.HttpRequestChannel HttpChannelFactory.HttpRequestChannel創(chuàng)建HttpChannelRequest的請求,然后調用HttpChannelRequest.SendRequest發(fā)送消息。其實質就是封裝一個HttpWebRequest,將Message發(fā)送到服務器端address里,根,webservice的最終原理是一樣的。因此,要抓住幾個關系點,從總體上把握客戶端請求的流程
(1 ServiceChannelFactory 類的實例是什么類型
(2 IChannelBinder接口的實現(xiàn)是什么類型
(3 IChannelBinder.Channel是什么
BindingElement.BuildChannelFactory
這個方法很有意思,默認的實現(xiàn)是通用BindingContext
將當前Binding對象中的所有元素(BindingElementCollection對象的實例),one by one 的進行遍歷,每次移走一個,取出,然后再次調用BuildChannelFactory
舉個例子
對于BasicHttpBinding對象來說,封裝了
傳輸綁定元素
HttpsTransportBindingElement httpsTransport;
指定 HTTPS 傳輸以傳輸消息的綁定元素。
HttpTransportBindingElement httpTransport;
用于指定 HTTP 傳輸以傳輸消息的綁定元素
協(xié)議通道元素 (安全)
BasicHttpSecurity security;
配置 basicHttpBinding 綁定的安全設置。
消息編碼綁定元素
MtomMessageEncodingBindingElement mtomEncoding;
指定消息傳輸優(yōu)化機制 (MTOM) 消息所使用的編碼和版本管理的綁定元素。
TextMessageEncodingBindingElement textEncoding;
指定用于基于文本的 SOAP 消息的字符編碼與消息版本管理。此時,BindingElementCollection中有以上元素,先從集合中移出一個, 調用一次BuildChannelFactory
HttpTransportBindingElement httpTransport 重寫了BuildChannelFactory
BasicHttpBinding.BuildChannelFactory
以上就是我們?yōu)榇蠹医榻B的WCF客戶端處理的相關方法。
名稱欄目:WCF客戶端處理具體實現(xiàn)方法淺析
URL鏈接:http://m.5511xx.com/article/cdjodjd.html


咨詢
建站咨詢
