WCF后续之旅(2): 如何对Channel Layer进行扩展——创建自定义Channel

上一篇文章中,我们通过一个直接借助BasicHttpBinding对象实现Client和Server端进行通信的例子,对WCF channel layer进行了一个大致上的介绍。由此引出了一些列通信相关的概念和对象,比如Channel,Outputchannel, Input channel,Request channel, Reply Channel,Duplex channel,Channel Shape,Channel manager,Channel factory, Channel listener,Binding element 等。通过这些元素,我们很容易地实现对WCF channel layer进行扩展。

对channellayer进行扩展一般适用于当你的需求通过现有的Binding,或者channel不能实现,而需要自定义一些channel来实现你所需的功能。不如现在的WCF系统定义的Channel中没有实现对Message body的压缩功能。你可以就需要将此功能定义到一个customchannel中,然后将其注入到channel stack中。一般来说,仅仅创建custom channel是不够的,因为在runtime,channel是通过Channel manager进行创建的,所以你需要创建对应的Channel factory(如何对发送方进行扩展)或者Channel listener(如果对接受方进行扩展)。而Channel factory和channel listener最终又是通过Binding element进行创建的,所以你还需要创建相应的Binding element。(Binding element=〉Channel factory&Channel listener=>Channel

在本章节中,我们将继续讨论WCF channel layer。我们将通过如何创建和应用custom channel来介绍channel layer一些知识。

1、ICommunicationObject 和 CommunicationObject

我们知道WCF channel layer的绝大部分对象,比如Channel,Channelfactory,Channellistener,从功能上讲都是用于通信(Communication)的对象,对传统的communicationobject,比如socket,他们往往都具有通过状态和状态转化规则(状态机:Statemachine)。这些状态包括Creating、Created、Opening、Opened、Closing、Closed等等。为了统一管理这些状态和状态之间的转化,WCF定义个一个特殊的Interface:ICommunicationObject

public interface ICommunicationObject
{
   
// Events
    event EventHandler Closed;
   
event EventHandler Closing;
   
event EventHandler Faulted;
   
event EventHandler Opened;
   
event EventHandler Opening;

   
// Methods
    void Abort();
    IAsyncResult BeginClose(AsyncCallback callback,
object state);
    IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback,
object state);
    IAsyncResult BeginOpen(AsyncCallback callback,
object state);
    IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback,
object state);
   
void Close();
   
void Close(TimeSpan timeout);
   
void EndClose(IAsyncResult result);
   
void EndOpen(IAsyncResult result);
   
void Open();
   
void Open(TimeSpan timeout);

   
// Properties
    CommunicationState State { get; }
}
 

ICommunicationObject定义了3种成员:

  • Property:State, 得到当前的状态,返回值是一个CommunicationState  枚举。
  • Method:同步、异步Open和Close方法。
  • Event:通过注册这些状态相关的Event,当时对象转化到对应的状态时执行相应操作。

WCF定义了一个abstract class: CommunicationObject直接实现了该Interface。CommunicationObject的实现统一的State machine。WCF channel layer的很多的class都直接或者间接的继承了这个class。你也可以让你的class继承该class。当你让你自己的class继承CommunicationObject的时候,在override 掉base相应的method的时候,强烈建议你先调用base对应的方法,CommunicationObject会帮你进行相应的State转换和触发相应的事件。

2. Channel 和Channel Shape

上一篇文章中,我们讨论过了。在不同的消息交换模式(MEP)中,发送方和接受方的Channel扮演的角色是不相同的。我们并把这种不同MEP中消息交互双方Channel的结构差异表述为Channel shape。我们有四种不同的Channel shape:Datagram、Request/reply、Duplex和P2P。不同Channel shape中Channel的结构性差性通过实现不同的Channel interface来体现。

对于Datagram channel shape,采用了One-way的MEP。发送方的channel 必须实现IOutputChannel interface。该Interface的方法成员主要集中在用于发送message的Send方法(同步/异步):

 

public interface IOutputChannel : IChannel, ICommunicationObject
{
   
// Methods
    IAsyncResult BeginSend(Message message, AsyncCallback callback, object state);
    IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback,
object state);
   
void EndSend(IAsyncResult result);
   
void Send(Message message);
   
void Send(Message message, TimeSpan timeout);

   
// Properties
    EndpointAddress RemoteAddress { get; }
    Uri Via
{ get; }
}
 

与之相应是IInputChannel inteface,该Interface用于Datagram channel shape中接收方的channel定义。其主要方法成员主要集中在用于接收Message的Receive方法(同步/异步):

public   interface IInputChannel : IChannel, ICommunicationObject
{
   
// Methods
    IAsyncResult BeginReceive(AsyncCallback callback, object state);
    IAsyncResult BeginReceive(TimeSpan timeout, AsyncCallback callback,
object state);
    IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback,
object state);
    IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback,
object state);
    Message EndReceive(IAsyncResult result);
   
bool EndTryReceive(IAsyncResult result, out Message message);
   
bool EndWaitForMessage(IAsyncResult result);
    Message Receive();
    Message Receive(TimeSpan timeout);
   
bool TryReceive(TimeSpan timeout, out Message message);
   
bool WaitForMessage(TimeSpan timeout);

   
// Properties
    EndpointAddress LocalAddress { get; }
}
 

 注:无论对于同步或者异步方法,一般由两个重载,一个接收一个TimeSpan 作为参数,表是Send或者Receive允许的时间范围。而另一个没有该参数的方式,不不是建议你使用一个无限的TimeSpan,而是使用一个可配置的默认时间段(实际上是Binding对象对应的属性)

不同于Datagram channel shape,Request/request channel shape下交互双方的Channel具有不同的行为。发送方的Channel实现IRequestChannel。该interface的方面成员主要集中在一些用于向接收方进行请求的Request方法(同步/异步):上面。

 

public interface IRequestChannel : IChannel, ICommunicationObject
{
   
// Methods
    IAsyncResult BeginRequest(Message message, AsyncCallback callback, object state);
    IAsyncResult BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback,
object state);
    Message EndRequest(IAsyncResult result);
    Message Request(Message message);
    Message Request(Message message, TimeSpan timeout);

   
// Properties
    EndpointAddress RemoteAddress { get; }
    Uri Via
{ get; }
}
 

同理,对于接收方的IReplyChannel则主要定义了一些用于Reply的方法:

public interface IReplyChannel : IChannel, ICommunicationObject
{
   
// Methods
    IAsyncResult BeginReceiveRequest(AsyncCallback callback, object state);
    IAsyncResult BeginReceiveRequest(TimeSpan timeout, AsyncCallback callback,
object state);
    IAsyncResult BeginTryReceiveRequest(TimeSpan timeout, AsyncCallback callback,
object state);
    IAsyncResult BeginWaitForRequest(TimeSpan timeout, AsyncCallback callback,
object state);
    RequestContext EndReceiveRequest(IAsyncResult result);
   
bool EndTryReceiveRequest(IAsyncResult result, out RequestContext context);
   
bool EndWaitForRequest(IAsyncResult result);
    RequestContext ReceiveRequest();
    RequestContext ReceiveRequest(TimeSpan timeout);
   
bool TryReceiveRequest(TimeSpan timeout, out RequestContext context);
   
bool WaitForRequest(TimeSpan timeout);

   
// Properties
    EndpointAddress LocalAddress { get; }
}

而对与Duplex和P2P,消息交互双方使用相同的Channel:Duplex channel。本质上讲,DuplexChannel = OutputChannel + IntputChannel。这一点从IDuplexChannel的定义上就可以看出来:

public interface IDuplexChannel : IInputChannel, IOutputChannel, IChannel, ICommunicationObject
{
}
 

3、创建Custom Channel

为了让大家对WCF channel layer有一个深刻的认识,以及掌握如何有效地对其进行扩展。我在整篇文章中穿插介绍一个具体的Sample:创建一个自定义的channel,以及相关的辅助对象,比如Channel factory、Channel listener和Binding element

这个Sample将基于我们最为常用的Request/Reply channel shape。所以我们需要创建两个Channel,一个是用于发送方的实现了IRequestChannel的Channel,而另一个则是实现了IReplyChannel的用于接收方的Channel。

为了简单起见,在我定义的channel的每个方法仅仅打印出相应的方法名称而已(这样做不但简单,还有的一个好处,那就是当我最后将其应用到具体的Messaging场景中,可以根据控制台打印出来的文字清楚地看清当我们的Channel应用到具体的场景中后先后执行了那些方法)。

我们先来看看实现了IRequestChannel的MyRequestChannel的定义:

namespace Artech.ChannleStackExplore.Channels
{
   
public class MyRequestChannel :ChannelBase, IRequestChannel
   
{
       
private IRequestChannel InnerChannel
       
{get;set;} 

       
public MyRequestChannel(ChannelManagerBase channleManager, IRequestChannel innerChannel)
            :
base(channleManager)
       
{
           
this.InnerChannel = innerChannel;
        }
 

       
ChannelBase Members 

       
IRequestChannel Members
    }

}
 

这里唯一需要注意的一点是:在实际的运行环境中,我们的channel仅仅了位于Channel stack的某个环节。该channel和其他的一些channel组成一个管道,这个管道里流淌是Message。所以当一个Channel执行了它相应的操作的时候,需要将message传到下一个channel作进一步处理。所有我们的Channel需要下一个Channel的应用,这个Channel就是我们的InnerChannel字段,该成员在构造函数中指定。

 

private IRequestChannel InnerChannel
{get;set;} 

public MyRequestChannel(ChannelManagerBase channleManager, IRequestChannel innerChannel)
            :
base(channleManager)
{
   
this.InnerChannel = innerChannel;
}
 

所以,对于每一个方法,在实现了本Channel的功能之后,只需要调用InnerChannel 的对应的方法即可。

我们再来看看实现了IReplyChannel的MyReplyChannel, 它用于接收方:

 

namespace Artech.ChannleStackExplore.Channels
{
   
public class MyReplyChannel: ChannelBase, IReplyChannel
   
{
       
private IReplyChannel InnerChannel
       
{ get; set; } 

       
public MyReplyChannel(ChannelManagerBase channelManager, IReplyChannel innerChannel):base(channelManager)
       
{
           
this.InnerChannel = innerChannel;
        }
 

       
ChannelBase Members 

       
IReplyChannel Members
    }

}
 

MyReplyChannel的定义方式和MyRequestChannel完全一样,我们就不用再多说什么了。

4. 创建Custom Channel Factory & Channel Listener

通过上一篇文章的介绍,我们知道Channel是通过Channel Manager来创建并管理的,在发送方的Channel Manager被称为Channel Factory

对于Channel factory,除了定义了两个Interface之外(IChannelFactoryIChannelFactory<TChannel>)

 

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




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

还定义了两个Base class:ChannelFactoryBaseChannelFactoryBase<TChannel>(限于篇幅,在这里就不多作介绍了)。

为了简单起见,我们上我们的Channel factory继承自ChannelFactoryBase<TChannel>

namespace Artech.ChannleStackExplore.Channels
{
   
public class MyChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
   
{
       
private IChannelFactory<TChannel> InnerChannelFactory
       
{ get; set; } 

       
public MyChannelFactory(BindingContext context)
       
{
           
this.InnerChannelFactory = context.BuildInnerChannelFactory<TChannel>();
        }
 

       
protected override TChannel OnCreateChannel(EndpointAddress address, Uri via)
       
{
            Console.WriteLine(
"MyChannelFactory<TChannel>.OnClose()");
            TChannel innerChannel
= this.InnerChannelFactory.CreateChannel(address, via);
           
return (TChannel)(object)(new MyRequestChannel(this, innerChannel as IRequestChannel));
        }
 

       
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
       
{
            Console.WriteLine(
"MyChannelFactory<TChannel>.OnBeginOpen()");
           
return this.InnerChannelFactory.BeginOpen(timeout, callback, state);
        }
 

       
protected override void OnEndOpen(IAsyncResult result)
       
{
            Console.WriteLine(
"MyChannelFactory<TChannel>.OnEndOpen()");
           
this.InnerChannelFactory.EndOpen(result);
        }
 

       
protected override void OnOpen(TimeSpan timeout)
       
{
            Console.WriteLine(
"MyChannelFactory<TChannel>.OnOpen()");
           
this.InnerChannelFactory.Open();
        }

    }

}
 

我们说过,和Channel stack一样,Channel factory仍然是一个stack,原因很简单,一个个的Channel需要相应的channel factory来创建。同Channel一样,当channel factory创建了自己的channel之后需要将接力棒交到下一个Channel factory。不过不通于Channel的是,下一个Channel factory不时在构造函数直接指定的,而是通过构造函数中的BindingContext 对象的BuildInnerChannelFactory()创建。

private IChannelFactory<TChannel> InnerChannelFactory
{ get; set; } 

public MyChannelFactory(BindingContext context)
{
     
this.InnerChannelFactory = context.BuildInnerChannelFactory<TChannel>();
}
 

注:BindingContext 的两个最重要的方法就是BuildInnerChannelFactoryBuildInnerChannelListener。前者创建Inner channel factory后者创建Inner Channel listener。

熟悉了ChannelFactory的定义,大家很自然的想得到ChannelListner的定义(不过ChannelListner的成员比ChannelFactory 要多些):

namespace Artech.ChannleStackExplore.Channels
{
   
public class MyChannelListener<TChannel> : ChannelListenerBase<TChannel> where TChannel : class, IChannel
   
{
       
private IChannelListener<TChannel> InnerChannelListener
       
{ get; set; } 

       
public MyChannelListener(BindingContext context)
       
{
           
this.InnerChannelListener = context.BuildInnerChannelListener<TChannel>();
        }
 

       
protected override TChannel OnAcceptChannel(TimeSpan timeout)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnAcceptChannel()");
            TChannel innerChannel
= this.InnerChannelListener.AcceptChannel(timeout);
           
return new MyReplyChannel(this, innerChannel as IReplyChannel) as TChannel;
        }
 

       
protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnBeginAcceptChannel()");
          
return this.InnerChannelListener.BeginAcceptChannel(timeout, callback, state);
        }
 

       
protected override TChannel OnEndAcceptChannel(IAsyncResult result)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnEndAcceptChannel()");
            TChannel innerChannel
= this.InnerChannelListener.EndAcceptChannel(result);
           
return new MyReplyChannel(this, innerChannel as IReplyChannel) as TChannel;
        }
 

       
protected override IAsyncResult OnBeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnBeginWaitForChannel()");
           
return this.InnerChannelListener.BeginWaitForChannel(timeout, callback, state);
        }
 

       
protected override bool OnEndWaitForChannel(IAsyncResult result)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnEndWaitForChannel()");
           
return this.InnerChannelListener.EndWaitForChannel(result);
        }
 

       
protected override bool OnWaitForChannel(TimeSpan timeout)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnWaitForChannel()");
           
return this.InnerChannelListener.WaitForChannel(timeout);
        }
 

       
public override Uri Uri
       
{
           
get 
           
{
                Console.WriteLine(
"MyChannelListener<TChannel>.Uri");
               
return this.InnerChannelListener.Uri;
            }
 

        }
 

       
protected override void OnAbort()
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnAbort()");
           
this.InnerChannelListener.Abort();
        }
 

       
protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnBeginClose()");
           
return this.InnerChannelListener.BeginClose(timeout, callback, state);
        }
 

       
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnBeginOpen()");
           
return this.InnerChannelListener.BeginOpen(timeout, callback, state);
        }
 

       
protected override void OnClose(TimeSpan timeout)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnClose()");
           
this.InnerChannelListener.Close(timeout);
        }
 

       
protected override void OnEndClose(IAsyncResult result)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnEndClose()");
           
this.InnerChannelListener.EndClose(result);
        }
 

       
protected override void OnEndOpen(IAsyncResult result)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnEndOpen()");
           
this.InnerChannelListener.EndOpen(result);
        }
 

       
protected override void OnOpen(TimeSpan timeout)
       
{
            Console.WriteLine(
"MyChannelListener<TChannel>.OnOpen()");
           
this.InnerChannelListener.Open(timeout);
        }

    }

}
 

5. 创建Custom Binding Element

我们知道Binding是Service mode layer进入Channel layer的中介,而Binding由一系列的Binding element组成。我们上面创建的Channel factory和Channel listener需要最终通过对应的BindingElement应用到Binding中才能最终发挥作用。我们就来创建这个BindingElement:MyBindingElement。

namespace Artech.ChannleStackExplore.Channels
{
 
public class MyBindingElement:BindingElement
   
{
       
public override BindingElement Clone()
       
{
           
return new MyBindingElement();
        }
 

       
public override T GetProperty<T>(BindingContext context)
       
{
           
return context.GetInnerProperty<T>();
        }
 

       
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
       
{
            Console.WriteLine(
"MyBindingElement.BuildChannelFactory()");
           
return new MyChannelFactory<TChannel>(context) as IChannelFactory<TChannel>;
        }
 

       
public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
       
{
            Console.WriteLine(
"MyBindingElement.BuildChannelListener()");
           
return new MyChannelListener<TChannel>(context) as IChannelListener<TChannel>;
        }

    }

}
 

够简单吧,直接调用MyChannelFactoryMyChannelListener的构造函数就可以了。

6. 创建Custom Binding

我们进入了最后的阶段,创建一个Custom Binding。MyBinding继承Binding class。 在CreateBindingElements方法中将我们的Binding element(MyBindingElement),连同其他必须的Binding element添加到BindingElementCollection 中。

namespace Artech.ChannleStackExplore.Channels
{
   
public class MyBinding:Binding
   
{
       
public override BindingElementCollection CreateBindingElements()
       
{
            BindingElementCollection elemens
= new BindingElementCollection();
            elemens.Add(
new TextMessageEncodingBindingElement());
            elemens.Add(
new MyBindingElement());
            elemens.Add(
new HttpTransportBindingElement());
           
return elemens.Clone();
        }
 

       
public override string Scheme
       
{
           
get
           
{
               
return "http";
            }

        }

    }

}
 


注:对BindElement的组装可以通过configuration来实现。

7. 使用Custom Binding

我们上面所做的一切都汇集到我们的Custombinding:MyBinding。既然我们为之写了那么多代码,我们一定要通过某种方式测试一下它时候具有我们需要的功能。我们通过MyBinding创建一个Messaging via Binding的应用。不熟悉的朋友可以转到上一篇去熟悉一下。

Server端的代码:

namespace Server
{
   
class Program
   
{
       
static void Main(string[] args)
       
{
            MyBinding binding
= new MyBinding();
            IChannelListener
<IReplyChannel> channelListener= binding.BuildChannelListener<IReplyChannel>(new Uri("http://127.0.0.1:8888/messagingviabinding"));
            channelListener.Open();

           
while (true)
           
{
                IReplyChannel channel
= channelListener.AcceptChannel(TimeSpan.MaxValue);
                channel.Open();
                RequestContext context
= channel.ReceiveRequest(TimeSpan.MaxValue);

                Console.WriteLine(
"Receive a request message:\n{0}", context.RequestMessage);
                Message replyMessage
= Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "http://artech.messagingviabinding", "This is a mannualy created reply message for the purpose of testing");
                context.Reply(replyMessage);
                channel.Close();
            }

        }

    }

}


Client端的代码:

 

namespace Client
{
   
class Program
   
{
       
static void Main(string[] args)
       
{
            MyBinding binding
= new MyBinding();
            IChannelFactory
<IRequestChannel> channelFactory = binding.BuildChannelFactory<IRequestChannel>();
            channelFactory.Open();

            IRequestChannel channel
= channelFactory.CreateChannel(new EndpointAddress("http://127.0.0.1:8888/messagingviabinding"));
            channel.Open();

            Message requestMessage
= Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "http://artech.messagingviabinding", "This is a mannualy created reply message for the purpose of testing");
            Message replyMessage
= channel.Request(requestMessage);
            Console.WriteLine(
"Receive a reply message:\n{0}", replyMessage);
            channel.Close();
            channelFactory.Close();
            Console.Read();
        }
 

    }

}
 

运行的结果将会是这样:

Server:

wcf2_02_01

Client:

wcf2_02_02

通过上面的输出结果,你很直观的了解到了整个程序执行过程中,我们的定义在Channel,Channel factory,Channel listener和Binding element的方法是如何被依次执行的。

 

你可能感兴趣的:(channel)