如何对WCF绑定元素进行自定义操作

WCF开发插件在开发领域中占据着重要的位置。它可以帮助开发人员轻松的实现一个安全性高及可跨平台的企业级解决方案。接下来,我们通过一个案例来演示如果自定义一个WCF绑定元素。通过该绑定元素来创建我们在上面一个案例中创建的两个自定义信道管理器:SimpleChannelFactory和SimpleChannelListener。按照上面的命名方式,我们把这个自定义绑定元素命名为:SimpleBindingElement,下面是整个SimpleBindingElement的定义:

目前成都创新互联公司已为上千的企业提供了网站建设、域名、网络空间、网站托管维护、企业网站设计、常德网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

 
 
 
  1. public class SimpleBindingElement : BindingElement   
  2. {   
  3. public SimpleBindingElement()   
  4. {   
  5. PrintHelper.Print(this, "SimpleBindingElement");   
  6. }   
  7. public override BindingElement Clone()   
  8. {   
  9. PrintHelper.Print(this, "Clone");   
  10. return new SimpleBindingElement();   
  11. }   
  12. public override T GetProperty< T>(BindingContext context)   
  13. {   
  14. PrintHelper.Print(this, string.Format("GetProperty< {0}>",
     typeof(T).Name));   
  15. return context.GetInnerProperty< T>();   
  16. }   
  17. public override IChannelFactory< TChannel> BuildChannelFactory
    < TChannel>(BindingContext context)   
  18. {   
  19. PrintHelper.Print(this, "BuildChannelFactory< TChannel>");   
  20. return new SimpleChannelFactory< TChannel>(context) as 
    IChannelFactory< TChannel>;   
  21. }   
  22. public override IChannelListener< TChannel> BuildChannelListener
    < TChannel>(BindingContext context)   
  23. {   
  24. PrintHelper.Print(this, "BuildChannelListener< TChannel>");   
  25. return new SimpleChannelListener< TChannel>(context) as 
    IChannelListener< TChannel>;   
  26. }   

SimpleBindingElement直接继承自抽象的基类BindingElement,对SimpleChannelFactory和SimpleChannelListener的创建分别实现在两个被重写的方法中:BuildChannelFactory< TChannel>和BuildChannelListener< TChannel>中。此外还重写了两个额外的方法:Clone和GetProperty< T>,前者用于克隆一个新的绑定元素,后一个和定义在信道、信道管理器的同名方法一样,用于获取基于某种类型的属性。

WCF绑定元素的相关自定义操作方法就为大家介绍到这里。

分享文章:如何对WCF绑定元素进行自定义操作
分享URL:http://www.mswzjz.cn/qtweb/news7/68857.html

攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等

广告

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