WCF继承实际应用技巧分享

开发 开发工具
当一个熟悉了使用C#继承的开发人员开始使用WCF继承进行编写程序时,都会有哪些不一样的体验呢?在这里我们将会向大家详细介绍。

当我们在使用WCF开发工具进行相应功能的开发时,首先要熟练掌握的当然是基于这一工具下的代码的编写方式。那么今天我们就先来体验一下WCF继承的相关应用方式,以此加深我们对这方面的认知程度。

在过去中,我们已经习惯了C#继承的各个特性,我们可以按如下的方式定义我们的继承关系:

 

  1. [ServiceContract]  
  2. public interface ISimpleCalculator  
  3. {  
  4. //Other Members  
  5. [OperationContract]  
  6. int Add(int arg1, int arg2);  
  7. }   
  8. [ServiceContract]  
  9. public interface IScientificCalculator : ISimpleCalculator  
  10. {  
  11. [OperationContract]  
  12. int Multiply(int arg1, int arg2);  

 

Ok,不要担心,在服务端这样的特性依然稳健地存在着:

 

  1. public class ScientificCalculatorService : IScientificCalculator  
  2. {  
  3. //Other Members   
  4. #region IScientificCalculator Members   
  5. public int Multiply(int arg1, int arg2)  
  6. {  
  7. return arg1 * arg2;  
  8. }   
  9. #endregion   
  10. #region ISimpleCalculator Members   
  11. public int Add(int arg1, int arg2)  
  12. {  
  13. return arg1 + arg2;  
  14. }   
  15. #endregion  

 

但是紧接着,Client端呢?

 

  1. [System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  2. [System.ServiceModel.ServiceContractAttribute(ConfigurationName=
    "ServiceReference.IScientificCalculator")]  
  3. public interface IScientificCalculator {  
  4. //Other Members  
  5. [System.ServiceModel.OperationContractAttribute(Action=
    "http://tempuri.org/ISimpleCalculator/Add"ReplyAction=
    "http://tempuri.org/ISimpleCalculator/AddResponse")]  
  6. int Add(int arg1, int arg2);  
  7. [System.ServiceModel.OperationContractAttribute(Action=
    "http://tempuri.org/IScientificCalculator/Multiply"
    ReplyAction="http://tempuri.org/IScientificCalculator/MultiplyResponse")]  
  8. int Multiply(int arg1, int arg2);  

 

在Reference.cs文件内,我们只能看到IScientificCalculator 接口的身影,却找不到ISimpleCalculator的踪迹。而事实上我们在服务端对这两个接口都定义了ServiceContract的Attribute,也许这对你来说并不重要,或者你不太关心这些继承特性所带来的优势,但是正也是因为这些继承特性所能带来的优势(包括多态等经典的OO特性)我们需要改造这个Reference.cs以使其适应我们“真正的需要”。类似以下的应用将会失败:

 

  1. static void Main(string[] args)  
  2. {  
  3. ScientificCalculatorClient calculator = new ScientificCalculatorClient();   
  4. UseScientificCalculator(calculator);  
  5. calculator.Close();  
  6. }   
  7. //Will not be supported now  
  8. static void UseSimpleCalculator(ISimpleCalculator calculator)  
  9. {  
  10. Console.WriteLine("Calculator Add : {0}", calculator.Add(5, 4));  
  11. }   
  12. static void UseScientificCalculator(IScientificCalculator calculator)  
  13. {  
  14. Console.WriteLine("Calculator Add : {0}", calculator.Add(5, 4));  
  15. Console.WriteLine("Calculator Multiply : {0}", calculator.Multiply(5, 4));  

 

当前的WCF继承问题就是:#t#

ISimpleCalculator接口在客户端是不被识别的。要解除这样的矛盾,就是要让客户端也拥有该接口。

首先我们考虑到我们与Service之间的通信是依赖ServiceContract来描述的,ServiceContract就类似OO中的Interface,一经发布就不可以修改了(尽量!)。我们能做的最好就是能在Client端将这些内容重新搭建起来,包括之间的继承关系。

在Add ServiceReference之后系统为我们自动生成了很多内容,找到Reference.cs,这将是我们大刀阔斧的地方……

我们可以看到它里面只实现了一个IScientificCalculator接口,这是我们先前就提到过的,我们的系统调用服务,都是通过从这里获取它们想要的“服务端”的一些类去构造本地实例来完成一系列操作的。那么我们现在只需要在这里引入相应的接口继承结构即可……

将原来实现的唯一接口注释掉,并添加以下代码:

 

  1. //[System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  2. //[System.ServiceModel.ServiceContractAttribute(ConfigurationName = 
    "ServiceReference.IScientificCalculator")]  
  3. [ServiceContract]  
  4. public interface ISimpleCalculator  
  5. {  
  6. //Other Members   
  7. // TODO: Add your service operations here  
  8. [OperationContract]  
  9. int Add(int arg1, int arg2);  
  10. }  
  11. //[System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  12. //[System.ServiceModel.ServiceContractAttribute(ConfigurationName =
     
    "ServiceReference.IScientificCalculator")]  
  13. [ServiceContract(ConfigurationName="ServiceReference.
    IScientificCalculatorVolnet"
    )]  
  14. public interface IScientificCalculator : ISimpleCalculator  
  15. {   
  16. [OperationContract]  
  17. int Multiply(int arg1, int arg2);  

 

我们需要using System.ServiceModel之后才可使用以上的WCF继承代码,该代码片断其实没有什么很特别的地方,它与服务端的接口继承没有什么大的出入,唯一需要关注的则是我黑体标注的“ConfigurationName="ServiceReference.IScientificCalculatorVolnet"”,注意,我这里不是在为自己的昵称做广告噢,而是以示区别。

责任编辑:曹凯 来源: CSDN
相关推荐

2010-03-01 17:52:03

WCF选择绑定

2009-12-21 14:49:27

2010-02-22 13:28:05

WCF异步调用

2010-02-22 17:21:02

WCF消息交换

2010-02-22 15:20:54

WCF WS-Disc

2010-02-25 15:25:19

WCF通道

2010-02-26 10:46:12

WCF行为扩展

2010-02-25 18:04:02

WCF IIS宿主

2010-03-01 09:48:23

WCF会话服务

2010-03-02 10:50:57

WCF元数据交换

2010-03-01 15:40:04

WCF实例停用

2010-02-23 13:03:34

WCF序列化

2010-02-25 10:52:29

WCF响应服务

2010-02-01 17:09:07

C++链表操作

2010-02-24 17:07:26

WCF序列化引擎

2010-03-03 16:25:41

Python字符串显示

2010-02-22 17:58:06

WCF异步上传

2010-02-26 10:56:06

WCF Stream

2010-03-02 17:35:20

WCF服务加载

2010-02-22 11:25:50

WCF DateSet
点赞
收藏

51CTO技术栈公众号