WCF服务契约基本应用技巧解读
创始人
2024-06-24 00:01:10
0

我们在应用WCF服务契约的时候,需要掌握一些应用技巧,才能帮助我们轻松的应用这一功能来完成各种功能需求。在这里我们就一起来看看WCF服务契约的分解与设计方法,以方便大家理解。

C++与C#均支持操作的重载,但在WCF的编程模型中,却并不支持这种技术。坦白说,在WCF的编程模型,对于面向对象的支持都是比较弱的,包括后面要介绍的继承体系与多态,都存在许多问题。因此,在服务端我们不能定义这样的WCF服务契约:

  1. [ServiceContract]   
  2. interface ICalculator   
  3. {   
  4. [OperationContract]   
  5. int Add(int arg1,int arg2);   
  6. [OperationContract]   
  7. double Add(double arg1,double arg2);   

虽然在编译时能够通过,然而一旦在装载宿主时,就会抛出InvalidOperationException异常。以ICalculator契约为例,WCF会认为是零个操作。

解决的办法是利用OperationContract特性的Name属性,例如:

  1. [ServiceContract]   
  2. interface ICalculator   
  3. {   
  4. [OperationContract(Name = "AddInt")]   
  5. int Add(int arg1,int arg2);   
  6. [OperationContract(Name = "AddDouble")]   
  7. double Add(double arg1,double arg2);   

不过采用这种方式,存在的问题是生成的代理会将Name属性指定的名称作为代理操作的方法名。这对于编程者而言,并非好的方式。所幸我们可以手动对生成的代理进行修改,将它修改为与WCF服务契约一致的操作名。由于,此时通过Name指定了操作的别名,因此,避免了装载宿主抛出的异常。

契约的继承

即使父接口标记了[ServiceContract],子接口仍然需要标记[ServiceContract],因为ServiceContractAttribute是不可继承的。服务类对服务契约的实现,与传统的C#编程没有什么区别。例如:

  1. [ServiceContract]   
  2. interface ISimpleCalculator   
  3. {   
  4. [OperationContract]   
  5. int Add(int arg1,int arg2);   
  6. }   
  7. [ServiceContract]   
  8. interface IScientificCalculator : ISimpleCalculator   
  9. {   
  10. [OperationContract]   
  11. int Multiply(int arg1,int arg2);   
  12. }   
  13. class MyCalculator : IScientificCalculator   
  14. {   
  15. public int Add(int arg1,int arg2) { return arg1 + arg2;   
  16. }   
  17. public int Multiply(int arg1,int arg2) { return arg1 * arg2;   
  18. }   

公开终结点的时候,可以对***层的契约接口公开一个单独的终结点:

  1. < service name=”MyCalculator”> < endpoint> < addressaddress=
    ”http://localhost:8001/MyCalculator/”> < bindingbinding=
    ”basicHttpBinding”> < contractcontract=” IScientificCalculator”>
     < /endpoint> < /service>  

客户端在导入如上的WCF服务契约时,会取消服务契约的继承层级,并利用OperationContract特性中的Action与ReplyAction属性,保留原来定义每个操作的契约名。但为了使客户端编程能够与服务编程保持一致,***是恢复客户端的契约层级。方法并无什么太玄妙的地方,无非就是根据服务契约层级对客户端契约进行手工修改。修改后的客户端契约及其代理的定义如下:

  1. [ServiceContract]   
  2. public interface ISimpleCalculator {   
  3. [OperationContract]   
  4. int Add(int arg1,int arg2);   
  5. }   
  6. public partial class SimpleCalculatorClient : ClientBase
    < ISimpleCalculator>, ISimpleCalculator   
  7. {   
  8. public int Add(int arg1,int arg2)   
  9. {   
  10. return Channel.Add(arg1,arg2);   
  11. } //Rest of the proxy }   
  12. [ServiceContract]   
  13. public interface IScientificCalculator : ISimpleCalculator {   
  14. [OperationContract]   
  15. int Multiply(int arg1,int arg2);   
  16. }   
  17. public partial class ScientificCalculatorClient : ClientBase
    < IScientificCalculator>,IScientificCalculator {   
  18. public int Add(int arg1,int arg2) {   
  19. return Channel.Add(arg1,arg2); }   
  20. public int Multiply(int arg1,int arg2) {   
  21. return Channel.Multiply(arg1,arg2); }   
  22. //Rest of the proxy } 

在书中还提出了所谓的代理链(Proxy Chaining)技术,实质上就是使得分别实现不同层级接口的代理类形成一个IS-A的继承关系。如上的定义,就可以使ScientificCalculatorClient继承自SimpleCalculatorClient,而不是继承ClientBase< IScientificCalculator>:

  1. public partial class SimpleCalculatorClient : 
    ClientBase< IScientificCalculator>, ISimpleCalculator {   
  2. public int Add(int arg1,int arg2) {   
  3. return Channel.Add(arg1,arg2);   
  4. } //Rest of the proxy }   
  5. public class ScientificCalculatorClient : SimpleCalculatorClient, 
    IScientificCalculator {   
  6. public int Multiply(int arg1,int arg2) {   
  7. return Channel.Multiply(arg1,arg2); } //Rest of the proxy } 

只有这样,如下代码才是正确的:

  1. SimpleCalculatorClient proxy1 = new SimpleCalculatorClient( );   
  2. SimpleCalculatorClient proxy2 = new ScientificCalculatorClient( );   
  3. ScientificCalculatorClient proxy3 = new ScientificCalculatorClient( ); 

以上就是对WCF服务契约的相关介绍。

【编辑推荐】

  1. WCF限流操作实际设置方式揭秘
  2. WCF实例停用基本应用技巧分享
  3. WCF分布操作应对特定操作情况
  4. WCF死锁三种不同方式介绍
  5. WCF回调契约如何进行正确定义

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
30分钟搞定iOS自定义相机 最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以。就花时间简单研究...
Intel将Moblin社区控... 本周二,非营利机构Linux基金会宣布,他们将担负起Moblin社区的管理工作,而这之前,Mobli...