IChannelListener与ChannelListenerBase 信道监听器实现了接口System.ServiceModel.Channels.IChannelListener。如下面的代码片段所示,IChannelListener继承自ICommunicationObject接口。WaitForChannel和BeginWaitForChannel/EndWaitForChannel方法用于检验信道(栈)创建/获取的可行性。这两个方法会试图创建一个新的信道(栈)或者获取一个现有的信道(栈),如果在规定的时限范围内新的信道(栈)创建成功或者现有的信道(栈)被成功获取,则方法返回True,否则返回False。GetProperty<T>用于返回基于指定类型的属性,而Uri属性返回真正的监听地址。 public interface IChannelListener : ICommunicationObject { bool WaitForChannel(TimeSpan timeout); IAsyncResult BeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state); bool EndWaitForChannel(IAsyncResult result); T GetProperty<T>() where T : class; Uri Uri { get; } } 除了IChannelListener接口,WCF还定义了一个继承它的泛型接口System.ServiceModel. Channels.IChannelListener<TChannel>,泛型参数TChannel实现了IChannel接口,代表信道监听器创建的基于某种信道形状的信道。两组方法AcceptChannel和BeginAccept Channel/EndAcceptChannel以同步或异步的方式创建或者获取用于接收处理消息的信道栈。 public interface IChannelListener<TChannel> : IChannelListener, ICommunicationObject where TChannel : class, 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); } 除了这两个接口,WCF还定义了System.ServiceModel.Channels.ChannelListenerBase和System.ServiceModel.Channels.ChannelListenerBase<TChannel>两个抽象类,它们作为信道监听器的基类。这两个抽象类的继承和接口实现如下面的代码所示。 public abstract class ChannelListenerBase : ChannelManagerBase, IChannelListener, ICommunicationObject { //省略成员 } public abstract class ChannelListenerBase<TChannel> : ChannelListenerBase, IChannelListener<TChannel>, IChannelListener, ICommunicationObject where TChannel : class, IChannel { //省略成员 }