WCF继承实际应用技巧分享

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

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

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

 

[ServiceContract]  
public interface ISimpleCalculator  
{  
//Other Members  
[OperationContract]  
int Add(int arg1, int arg2);  
}   
[ServiceContract]  
public interface IScientificCalculator : ISimpleCalculator  
{  
[OperationContract]  
int Multiply(int arg1, int arg2);  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

 

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

 

public class ScientificCalculatorService : IScientificCalculator  
{  
//Other Members   
#region IScientificCalculator Members   
public int Multiply(int arg1, int arg2)  
{  
return arg1 * arg2;  
}   
#endregion   
#region ISimpleCalculator Members   
public int Add(int arg1, int arg2)  
{  
return arg1 + arg2;  
}   
#endregion  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

 

但是紧接着,Client端呢?

 

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

 

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

 

static void Main(string[] args)  
{  
ScientificCalculatorClient calculator = new ScientificCalculatorClient();   
UseScientificCalculator(calculator);  
calculator.Close();  
}   
//Will not be supported now  
static void UseSimpleCalculator(ISimpleCalculator calculator)  
{  
Console.WriteLine("Calculator Add : {0}", calculator.Add(5, 4));  
}   
static void UseScientificCalculator(IScientificCalculator calculator)  
{  
Console.WriteLine("Calculator Add : {0}", calculator.Add(5, 4));  
Console.WriteLine("Calculator Multiply : {0}", calculator.Multiply(5, 4));  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

 

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

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

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

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

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

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

 

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

 

我们需要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 15:20:54

WCF WS-Disc

2010-02-22 17:21:02

WCF消息交换

2010-02-25 15:25:19

WCF通道

2010-03-01 15:40:04

WCF实例停用

2010-03-02 10:50:57

WCF元数据交换

2010-02-25 18:04:02

WCF IIS宿主

2010-02-23 13:03:34

WCF序列化

2010-02-26 10:46:12

WCF行为扩展

2010-03-01 09:48:23

WCF会话服务

2010-02-25 10:52:29

WCF响应服务

2010-02-01 17:09:07

C++链表操作

2010-02-24 17:07:26

WCF序列化引擎

2010-02-22 17:58:06

WCF异步上传

2010-03-03 16:25:41

Python字符串显示

2010-02-22 11:25:50

WCF DateSet

2010-02-26 14:12:27

WCF元数据

2010-02-23 16:46:47

WCF并发能力
点赞
收藏

51CTO技术栈公众号