zoukankan      html  css  js  c++  java
  • WCF客户端请求步骤 转载

    原文:http://www.cnblogs.com/artech/archive/2008/07/08/1237902.html

    在客户端自动生成的实例中

    是从ClientBase<of T>.Channel属性开始的,最终要创建T的透明代理,然后调用。
    以BasicHttpBinding为例,客户端请求的主要步骤如下:

    1 根据传入的Binding和EndpointAddress生成ServiceEndpoint

    2 再根据ServiceEndpoint的类型生成ServiceChannelFactory 类的实例
    当前BasicHttpBinding 生成的应该是ServiceChannelFactoryOverRequest类的实例,
    对应的IChannelBinder是RequestChannelBinder

    注:
    basicHttpBinding.BuildChannelFactory<IRequestChannel>要对 basicHttpBinding所有的绑定元素进行遍历
    默认情况下,不启用https,则传输元素使用HttpTransportBindingElement,该对象重写BuildChannelFactory<IRequestChannel>,返回值是HttpChannelFactory

    RequestChannelBinder对象最重要的字段是channel,对应的值是HttpChannelFactory.CreateChannel(),返回的值是HttpChannelFactory.HttpRequestChannel

    3 生成ServiceChannel,将ServiceChannelFactoryOverRequest和RequestChannelBinder做为参数传入ServiceChannel
    构造函数为ServiceChannel(ServiceChannelFactory factory, IChannelBinder binder)


    4. 生成T的透理代理ServiceChannelProxy,将ServiceChannel做为参数传入ServiceChannelProxy,构造


    5.在调用透明代理相应的方法时,调用ServiceChannelProxy.Invoke(),
    如果是Service,调用ServiceChannel.Call(),此时实质是调用ServiceChannel封装的IChannelBinder(当前是RequestChannelBinder)的call,


    6 调用RequestChannelBinder.Request(),注意步骤2最后一句,此时channel是HttpChannelFactory.HttpRequestChannel
    HttpChannelFactory.HttpRequestChannel创建 HttpChannelRequest的请求,然后调用HttpChannelRequest.SendRequest发送消息
    其实质就是封装一个HttpWebRequest,将Message发送到服务器端address里,根,webservice的最终原理是一样的


    因此,要抓住几个关系点,从总体上把握客户端请求的流程
    (1 ServiceChannelFactory 类的实例是什么类型
    (2 IChannelBinder接口的实现是什么类型
    (3 IChannelBinder.Channel是什么


    另外,BindingElement.BuildChannelFactory<TChannel>
    这个方法很有意思,默认的实现是通用BindingContext
    将当前Binding对象中的所有元素(BindingElementCollection对象的实例),one by one 的进行遍历,每次移走一个,取出,然后再次调用BuildChannelFactory<TChannel>

    举个例子

    对于BasicHttpBinding对象来说,封装了
    传输绑定元素
    HttpsTransportBindingElement httpsTransport;
    指定 HTTPS 传输以传输消息的绑定元素。
    HttpTransportBindingElement httpTransport;
    用于指定 HTTP 传输以传输消息的绑定元素

    协议通道元素 (安全)
    BasicHttpSecurity security;
    配置 basicHttpBinding 绑定的安全设置。

    消息编码绑定元素
    MtomMessageEncodingBindingElement mtomEncoding;
    指定消息传输优化机制 (MTOM) 消息所使用的编码和版本管理的绑定元素。
    TextMessageEncodingBindingElement textEncoding;
    指定用于基于文本的 SOAP 消息的字符编码与消息版本管理。


    此时,BindingElementCollection中有以上元素,先从集合中移出一个,
    调用一次BuildChannelFactory<TChannel>
    HttpTransportBindingElement httpTransport 重写了BuildChannelFactory<TChannel> 返回 HttpChannelFactory
    其它的绑定元素基本上调BindingElement的,是直接跳到下一个,

    所以,
    BasicHttpBinding.BuildChannelFactory<IRequestChannel>()返回的是
    HttpChannelFactory

  • 相关阅读:
    事务
    排序算法
    二维数组中的查找
    在Linux中安装Matlab
    null和“”的区别
    【学习笔记】〖九度OJ〗题目1433:FatMouse
    【学习笔记】〖九度OJ〗题目1464:Hello World for U
    year:2017 month:8 day:1
    year:2017 month:07 day:31
    year:2017 month:7 day:27
  • 原文地址:https://www.cnblogs.com/asingna/p/2178825.html
Copyright © 2011-2022 走看看