• WCF之Channel


    Channel分为2种:transport信道和protocol信道。

    Service和Client都有一个信道栈,由多个信道组成,其中,transport信道位于信道栈的最底层,protocol信道位于信道栈的最上层。

    image

    protocol信道用于消息交互、安全、事务、日志。

    transport信道用于transport和encoder。

    信道栈又被称为Binding~~娃哈哈,终于找到了共性。

    信道栈至少包含一个transport和一个encoder,不一定包括protocol信道。

    WFC支持3种消息交换模式:

    oneway

    duplex

    request-reply

    为此,WCF提供了10中不同的接口,统称为Channel Shapes:

    其中5个:

    IOutputChannel
    IInputChannel
    IDuplexChannel
    IRequestChannel
    IReplyChannel

    它们是不支持Session的。于是,还有5个支持Session的接口:

    IOutputSessionChannel
    IInputSessionChannel
    IDuplexSessionChannel
    IRequestSessionChannel
    IReplySessionChannel
    一个一个分析:
    1)OneWay
    消息发送仅一个方向:Client->Server
    Client不需要立刻获得server返回的response,client只需要知道消息已经被发送出去了。消息发送的同时,本次通信即宣告结束。
    OneWay由IOutputChannelIInputChannel组成,前者用来发消息,后者用于收消息:

    image

    2.Duplex

    说白了,就是由2个oneway组成的,汇聚在一个接口中:IDuplexChannel

    image

    3.Request-Reply

    每一个request都对应一个response。在client发送一个request后,必须等待response返回后,才能发送另外的request。

    使用了IRequestChannelIResponseChannel接口

    image

    貌似浏览器处理HTTP request时就是按照这种方式。

    有一名技术,叫做Shape Changing。大体是说,HTTP transport信道使用的是Request-Reply,而如果在HTTP上使用oneway和duplex,就必须使用Shape Changing技术。为此,我们要自定义一个新的protocol Channel,位于原有的transport channel之上。

    CustomBinding binding = new CustomBinding(
        new OneWayBindingElement(),
        new TextMessageEncodingBindingElement(),
        new HttpTransportBindingElement());
    //oneway
    BasicHttpBinding binding = new BasicHttpBinding();
    
    BindingParameterCollection parameters = new BindingParameterCollection();
    
    Message message = Message.CreateMessage(MessageVersion.Soap11, "urn:sendmessage");
    
    IChannelFactory<IOutputChannel> factory = binding.BuildChannelFactory<IOutputChannel>(parameters);
    
    IOutputChannel channel = factory.CreateChannel(new EndpointAddress("http://localhost/sendmessage/"));
    
    channel.Send(message);
    
    channel.Close();
    factory.Close();
    
    //duplex
    NetTcpBinding binding = new NetTcpBinding();
    BindingParameterCollection parameters = new BindingParameterCollection();
    
    Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:sendmessage");
    IChannelFactory<IDuplexChannel> factory = binding.BuildChannelFactory<IDuplexChannel>(parameters);
    
    IDuplexChannel channel = factory.CreateChannel(new EndpointAddress("net.tcp://localhost/sendmessage/"));
    
    channel.Send(message);
    
    channel.Close();
    factory.Close();
    
    //request-reply
    BasicHttpBinding binding = new BasicHttpBinding();
    
    BindingParameterCollection parameters = new BindingParameterCollection();
    
    Message request = Message.CreateMessage(MessageVersion.Soap11, "urn:sendmessage");
    
    IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(parameters);
    
    IRequestChannel channel = factory.CreateChannel(new EndpointAddress("http://localhost/sendmessage/"));
    
    Message response = channel.Request(request);
    
    channel.Close();
    factory.Close();
     
  • 相关阅读:
    「Java分享客栈」Nacos配置中心称王称霸,我Apollo一生也不弱于人!
    超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群
    中科大下载android源码
    binder框架核心类关系图
    BinderService模板类
    SpringBoot启动命令行参数【D】和【】的区别
    MySQL快速生成大量数据的SQL存储过程
    【已解决】'sql_server.pyodbc' isn't an available database backend.
    python 常用模块
    hive正则
  • 原文地址:https://www.cnblogs.com/Jax/p/1597985.html
Copyright © 2020-2023  润新知