WCF技术黑幕 第7章(1)

WCF技术内幕 第7章(1)

第7章 通道管理器

在WCF的类型系统中,通道工厂有其特殊的名字,这些名字与发送者和接收者的命名不同。在接收端,这些类型被称为通道侦听器。在发送端,这些类型被称为通道工厂。

7.1 通道管理器的概念

通道工厂会负责管理通道的工作,ChannelManagerBase类型就成为一个强制通道管理器实现通道状态机,实现查询机制,传递绑定超时属性给通道的简单方式。

7.2 接收者:通道侦听器

在WCF里,通道侦听器做着相同的工作。它们会先绑定一个URI,然后等待消息的传入。当连接建立以后,以Accept开头的方法会返回一个通道的实例,然后程序使用这个通道对象来接收消息。

IChannelListener接口

    public interface IChannelListener : ICommunicationObject
    {
        Uri Uri { get; }

        IAsyncResult BeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state);
        bool EndWaitForChannel(IAsyncResult result);
        T GetProperty<T>() where T : class;
        bool WaitForChannel(TimeSpan timeout);
    }
这个接口会强制所有的通道实现通道状态机,并包含一些基本的通道侦听器成员。


IChannelListener<TChannel>接口

    public interface IChannelListener<TChannel> : IChannelListener, ICommunicationObject where TChannel : class, global::System.ServiceModel.Channels.IChannel
    {
        TChannel AcceptChannel();
        TChannel AcceptChannel(TimeSpan timeout);
        IAsyncResult BeginAcceptChannel(AsyncCallback callback, object state);
        IAsyncResult BeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state);
        TChannel EndAcceptChannel(IAsyncResult result);
    }

通道是实现通道形状,而通道侦听器是引用通道形状并且使用这个引用来创建特定的通道。


ChannelListenerBase类型

    public abstract class ChannelListenerBase : ChannelManagerBase, IChannelListener, ICommunicationObject
    {

    }

ChannelListenerBase<TChannel>类型

    public abstract class ChannelListenerBase<TChannel> : ChannelListenerBase, IChannelListener<TChannel>, IChannelListener, ICommunicationObject where TChannel : class, global::System.ServiceModel.Channels.IChannel
    {

    }

7.3 发送者:通道工厂

它们会通过CreateChannel方法创建符合要求的连接通道而不是消极等待消息到来。


IChannelFactory接口

    public interface IChannelFactory : ICommunicationObject
    {
        T GetProperty<T>() where T : class;
    }

IChannelFactory<TChannel>接口

    public interface IChannelFactory<TChannel> : IChannelFactory, ICommunicationObject
    {
        TChannel CreateChannel(EndpointAddress to);
        TChannel CreateChannel(EndpointAddress to, Uri via);
    }

ChannelFactoryBase类型

    public abstract class ChannelFactoryBase : ChannelManagerBase, IChannelFactory, ICommunicationObject
    {

    }

ChannelFactoryBase<TChannel>类型

    public abstract class ChannelFactoryBase<TChannel> : ChannelFactoryBase, IChannelFactory<TChannel>, IChannelFactory, ICommunicationObject
    {

    }

WCF技术黑幕 第7章(1)