WCF技术内幕 第8章(1)

第8章 绑定

8.1 绑定对象模型

绑定类型没有复杂的类型层次关系。实际上,绑定类型直接继承了object类型,并实现了IDefaultCommunicationTimeouts接口。

Binding的Test方法

Binding的CanBuildChannelFactory<TChannel>和CanBuildChannelListener<TChannel>用来测试Binding能否创建特定形状的通道工厂堆栈或通道侦听器堆栈。

CreateBindingElements方法

CreateBindingElements返回的集合对象就是创建通道工厂和通道侦听器堆栈的图纸。

            foreach (var bindingElement in new NetTcpBinding().CreateBindingElements())
            {
                Console.WriteLine(bindingElement.GetType().Name);
            }

            //TransactionFlowBindingElement
            //BinaryMessageEncodingBindingElement
            //WindowsStreamSecurityBindingElement
            //TcpTransportBindingElement

8.2 BindingElement类型

BindingElement是一个工厂对象,更确切地说,BindingElement类型定义了返回通道工厂和通道侦听器的方法。

    public abstract class BindingElement
    {
        public virtual IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context);
        public virtual IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context) where TChannel : class, IChannel;
        public virtual bool CanBuildChannelFactory<TChannel>(BindingContext context);
        public virtual bool CanBuildChannelListener<TChannel>(BindingContext context) where TChannel : class, IChannel;
        public abstract BindingElement Clone();
        public abstract T GetProperty<T>(BindingContext context) where T : class;
    }

BindingElement的测试方法

BindingContext对象保存了一个可扩展的BindingElement、BindingParameterCollection对象和与侦听地址相关的属性列表。BindingContext对象包含一个可以调用的BindingElement对象的列表,而且它会提供给其他BindingElement对象查询列表中下一个BindingElement元素对象的途径。


8.3 TransportBindingElement类型

TransportBindingElement类型定义了几个只有通道工厂或通道侦听器需要的成员,但是TransportBindingElement继承自BindingElement类型。

TransportBindingElement类型必须出现在BindingElement集合的末端。

    public abstract class TransportBindingElement : BindingElement
    {
        protected TransportBindingElement();
        protected TransportBindingElement(TransportBindingElement elementToBeCloned);

        [DefaultValue(false)]
        public bool ManualAddressing { get; set; }
        [DefaultValue(524288)]
        public virtual long MaxBufferPoolSize { get; set; }
        [DefaultValue(65536)]
        public virtual long MaxReceivedMessageSize { get; set; }
        public abstract string Scheme { get; }

        public override T GetProperty<T>(BindingContext context);
    }

MaxBufferPoolSize用来设置最大缓冲池的字节大小,而MaxReceivedMessageSize用来设置接收消息的最大字节大小。


8.4 BindingContext类型

BindingContext类型存储一个BindingElement对象的集合,展示了可以顺序创建通道工厂或通道侦听器堆栈的方法,且维护着一个额外的信息集合,这些信息在通道工厂或通道侦听器实例化时会使用到。


8.5 使用绑定

            BasicHttpBinding binding = new BasicHttpBinding();
            Uri address = new Uri("http://www.andersoft.com/Anders");

            //使用绑定来创建通道侦听器堆栈
            IChannelListener<IReplyChannel> channelListener = binding.BuildChannelListener<IReplyChannel>(address, new BindingParameterCollection());
            channelListener.Open();

            //获取侦听到的通道
            IReplyChannel channel = channelListener.AcceptChannel();
            //通过通道获取request context
            RequestContext requestContext = channel.ReceiveRequest();

            //返回message
            Message message = Message.CreateMessage(MessageVersion.Soap11WSAddressingAugust2004, "Anders");
            requestContext.Reply(message);

            requestContext.Close();
            channel.Close();

            BasicHttpBinding binding = new BasicHttpBinding();
            Uri address = new Uri("http://www.andersoft.com/Anders");

            //使用绑定来创建通道工厂
            IChannelFactory<IRequestChannel> channelFactory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
            channelFactory.Open();

            //创建通道
            IRequestChannel channel = channelFactory.CreateChannel(new EndpointAddress(address));
            channel.Open();

            //request message并获取reply message
            Message message = Message.CreateMessage(MessageVersion.Soap11WSAddressingAugust2004, "Anders");
            Message replyMessage = channel.Request(message);

            channel.Close();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值