6.6 创建自定义通道
创建基类型
//Create base class
using System;
using System.ServiceModel.Channels;
namespace WCFChannelSample
{
class DelegatorChannelBase<TShape> : ChannelBase
where TShape : class, IChannel
{
private TShape _innerChannel; //引用堆栈中的下一个通道
private string _source; //打印的字符
protected TShape InnerChannel { get { return _innerChannel; } } //提供_innerChannel给继承类型
protected DelegatorChannelBase(ChannelManagerBase channelManagerBase, TShape innerChannel, string source)
: base(channelManagerBase)
{
if (innerChannel == null)
throw new ArgumentNullException("DelegatorChannelBase requires a non-null channel.", "innerChannel");
_source = string.Format("{0} CHANNEL STATE CHANGE: DelegatorChannelBase", source);
_innerChannel = innerChannel;
}
//IChannel实现
public override T GetProperty<T>()
{
return this.InnerChannel.GetProperty<T>();
}
#region CommunicationObject Members
protected override void OnAbort()
{
Console.WriteLine("Abort:" + _source);
this.InnerChannel.Abort();
}
protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
{
Console.WriteLine("BeginClose:" + _source);
return this.InnerChannel.BeginClose(timeout, callback, state);
}
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
Console.WriteLine("BeginOpen:" + _source);
return this.InnerChannel.BeginOpen(timeout, callback, state);
}
protected override void OnClose(TimeSpan timeout)
{
Console.WriteLine("Close:" + _source);
this.InnerChannel.Close(timeout);
}
protected override void OnEndClose(IAsyncResult result)
{
Console.WriteLine("EndClose:" + _source);
this.InnerChannel.EndClose(result);
}
protected override void OnEndOpen(IAsyncResult result)
{
Console.WriteLine("EndOpen:" + _source);
this.InnerChannel.EndOpen(result);
}
protected override void OnOpen(TimeSpan timeout)
{
Console.WriteLine("Open:" + _source);
this.InnerChannel.Open(timeout);
}
#endregion
}
}
数据报发送通道
//数据报发送通道
using System.ServiceModel.Channels;
namespace WCFChannelSample
{
class DelegatorInputChannel<TShape> : DelegatorChannelBase<TShape>, IInputChannel
where TShape : class, IInputChannel
{
internal DelegatorInputChannel(ChannelManagerBase channelManagerBase, TShape innerChannel, string source)
: base(channelManagerBase, innerChannel, source) { }
...
}
}
数据报接收会话通道
//数据报接收会话通道
using System.ServiceModel.Channels;
namespace WCFChannelSample
{
class DelegatorOutputSessionChannel<TShape> : DelegatorChannelBase<TShape>, IOutputSessionChannel
where TShape : class, IOutputSessionChannel
{
internal DelegatorOutputSessionChannel(ChannelManagerBase channelManagerBase, TShape innerChannel, string source)
: base(channelManagerBase, innerChannel, source) { }
...
}
}