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

开发 开发工具
如何才只能自定义我们的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绑定元素的相关自定义操作方法就为大家介绍到这里。

【编辑推荐】

  1. WCF双工会话通道基本创建步骤详解
  2. WCF集合类在实际应用中特点分析
  3. WCF异步服务正确创建方式详解
  4. WCF异步操作具体定义与应用
  5. WCF自定义集合类型应用注意事项探讨
责任编辑:曹凯 来源: CSDN
相关推荐

2019-11-19 08:04:16

JavaHashSet去重

2009-11-09 16:06:53

WCF自定义集合

2009-12-07 14:35:42

WCF异步调用

2014-04-02 13:27:29

iOSNSArray对象

2010-02-25 16:27:44

WCF扩展点

2009-07-06 13:49:29

2010-02-25 11:23:29

WCF返回自定义格式

2010-02-22 17:07:50

WCF绑定元素

2009-08-03 16:37:49

C#异常类

2021-07-01 11:07:49

Swift 自定义操作符

2024-01-05 15:28:06

鸿蒙数据同步GlobalThis

2013-06-27 11:10:01

iOS开发自定义UISlider

2010-02-24 14:59:52

WCF自定义过滤器

2010-03-01 09:56:21

WCF自定义集合类型

2009-12-08 17:08:13

WCF Service

2010-03-02 18:01:07

WCF自定义消息筛选器

2009-12-22 11:29:27

WCF自定义集合类型

2011-12-05 15:02:21

Knockout

2009-09-07 22:00:15

LINQ自定义

2010-02-07 14:02:16

Android 界面
点赞
收藏

51CTO技术栈公众号