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