WCF继承实际应用技巧分享
创始人
2024-06-23 22:10:57
0

当我们在使用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"”,注意,我这里不是在为自己的昵称做广告噢,而是以示区别。

相关内容

热门资讯

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