WCF信道监听器代码示例解析

WCF是一个功能比较强大的开发工具,可以帮助我们创建一个功能稳定,安全性高的解决方案。在这里,我们创建一个自定义的信道监听器:SimpleReplyChannelListner。#t#

该WCF信道监听器用于在请求-回复消息交换模式下进行请求的监听。在本案例中,我们来创建与之相对的信道工厂:SimpleChannelFactory< TChannel>,用于请求-回复消息交换模式下进行用于请求发送信道的创建。由于SimpleChannelFactory< TChannel>的实现相对简单,将所有代码一并附上。

SimpleChannelFactory< TChannel>直接继承自抽象基类SimpleChannelFactoryBase< TChannel>。字段成员_innerChannelFactory表示信道工厂栈中后一个信道工厂对象,该成员在构造函数中通过传入的BindingContext对象的BuildInnerChannelFactory< TChannel>方法创建。OnCreateChannel是核心大方法,实现了真正的信道创建过程,在这里我们创建了我们自定义的信道:SimpleRequestChannel.。构建SimpleRequestChannel. 的InnerChannel通过­­­_innerChannelFactory的CreateChannel方法创建。对于其他的方法(OnOpen、OnBeginOpen和OnEndOpen),我们仅仅通过PrintHelper输出当前的方法名称,并调用­_innerChannelFactory相应的方法。

WCF信道监听器代码示例:

 
 
 
  1. public class SimpleChannelFactory< TChannel> : 
    ChannelFactoryBase< TChannel>
  2. {
  3. public IChannelFactory< TChannel> _innerChannelFactory;
  4. public SimpleChannelFactory(BindingContext context)
  5. {
  6. PrintHelper.Print(this, "SimpleChannelFactory");
  7. this._innerChannelFactory = context.BuildInnerChannelFactory
    < TChannel>();
  8. protected override TChannel OnCreateChannel
    (EndpointAddress address, Uri via)
  9. {
  10. PrintHelper.Print(this, "OnCreateChannel");
  11. IRequestChannel innerChannel = this._innerChannelFactory.
    CreateChannel(address, via) as IRequestChannel;
  12. SimpleRequestChannel. channel = new SimpleRequestChannel.
    (this, innerChannel);
  13. return (TChannel)(object)channel;
  14. }
  15. protected override IAsyncResult OnBeginOpen
    (TimeSpan timeout, AsyncCallback callback, object state)
  16. {
  17. PrintHelper.Print(this, "OnBeginOpen");
  18. return this._innerChannelFactory.BeginOpen(timeout, callback, state);
  19. protected override void OnEndOpen(IAsyncResult result
  20. {
  21. PrintHelper.Print(this, "OnEndOpen");
  22. this._innerChannelFactory.EndOpen(result);
  23. }
  24. protected override void OnOpen(TimeSpan timeout)
  25. {
  26. PrintHelper.Print(this, "OnOpen");
  27. this._innerChannelFactory.Open(timeout);
  28. }
  29. }

以上就是对WCF信道监听器的相关介绍。

名称栏目:WCF信道监听器代码示例解析
文章来源:http://www.hantingmc.com/qtweb/news4/433754.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联