WCF面向服务应用程序系列之七:契约版本管理—服务契约的继承

      在契约版本管理的前二章,我们主要介绍了数据契约对版本的影响,本章我们介绍一下服务契约的变化对版本的影响。服务契约的接口支持继承功能,我们可以定义一个契约层级,但是ServiceContract特性是不能继承的,因此,接口层级中的每组接口都必须显示的标记ServiceContract特性。

      下面我们通过一个DEMO来演示服务契约的继承:

      1、新建WCF Service Library程序,修改解决方案名称为ContractVersion,修改项目名称为InheritanceContract,删除自动添加的文件。

      2、新建接口IInheritanceServiceA,代码如下:

 
  
[ServiceContract(Name = " InheritanceServiceContract " , Namespace = " http://schemas.xinhaijulan.com/demos/InheritanceServiceContract " )]
interface IInheritanceServiceA
{
[OperationContract]
string Operation1();

[OperationContract]
string Operation2();
}

      3、新建接口IInheritanceServiceA2,并继承接口IInheritanceServiceA,代码如下:

 
  
[ServiceContract(Name = " InheritanceServiceContract " , Namespace = " http://schemas.xinhaijulan.com/demos/InheritanceServiceContract " )]
interface IInheritanceServiceA2 : IInheritanceServiceA
{
[OperationContract]
string Operation3();
}

      注意,每个接口都必须单独实现ServiceContract标记。

      4、新建InheritanceService类,继承并实现接口IInheritanceServiceA,代码如下:

 
  
class InheritanceService : IInheritanceServiceA
{
public string Operation1()
{
return " IInheritanceServiceA.Operation1() invoked. " ;
}

public string Operation2()
{
return " IInheritanceServiceA.Operation2() invoked. " ;
}
}

      5、修改App.config中的服务名称 、端点契约、服务地址,然后编译,代码如下:

代码
 
   
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< configuration >

< system.web >
< compilation debug = " true " />
</ system.web >
<!-- When deploying the service library project, the content of the config file must be added to the host ' s
app.config file. System.Configuration does not support config files for libraries. -->
< system.serviceModel >
< services >
< service name = " InheritanceContract.InheritanceService " >
< host >
< baseAddresses >
< add baseAddress = " http://localhost:8732/Design_Time_Addresses/InheritanceContract/InheritanceService/ " />
</ baseAddresses >
</ host >
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
< endpoint address = "" binding = " wsHttpBinding " contract = " InheritanceContract.IInheritanceServiceA " >
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
< identity >
< dns value = " localhost " />
</ identity >
</ endpoint >
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
< endpoint address = " mex " binding = " mexHttpBinding " contract = " IMetadataExchange " />
</ service >
</ services >
< behaviors >
< serviceBehaviors >
< behavior >
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
< serviceMetadata httpGetEnabled = " True " />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true . Set to false before deployment
to avoid disclosing exception information
-->
< serviceDebug includeExceptionDetailInFaults = " False " />
</ behavior >
</ serviceBehaviors >
</ behaviors >
</ system.serviceModel >

</ configuration >

      6、创建客户端控制台程序InheritanceClientA,添加Service Reference,修改名称空间为InheritanceContract,代码如下:

 
  
static void Main( string [] args)
{
using (InheritanceContract.InheritanceServiceContractClient client = new InheritanceContract.InheritanceServiceContractClient())
{
Console.WriteLine(client.Operation1());
Console.WriteLine(client.Operation2());
}

Console.ReadLine();
}

      7、设置InheritanceClientA为启动项目,运行项目,将在控制台看到如下输出:

 
  
IInheritanceServiceA.Operation1() invoked.
IInheritanceServiceA.Operation2() invoked.

      8、修改InheritanceService类,使之继承并实现IInheritanceServiceA2接口,【此时必须修改app.config文件中的端点的Contract的指向为新的接口:InheritanceContract.IInheritanceServiceA2】然后重新编译,代码如下:

<endpoint address ="" binding="wsHttpBinding" contract="InheritanceContract.IInheritanceServiceA2">
 
  
class InheritanceService : IInheritanceServiceA2
{
public string Operation1()
{
return " IInheritanceServiceA.Operation1() invoked. " ;
}

public string Operation2()
{
return " IInheritanceServiceA.Operation2() invoked. " ;
}

public string Operation3()
{
return " IInheritanceServiceA2.Operation3() invoked. " ;
}
}

      9、创建客户端控制台程序InheritanceClientA2,添加Service Reference,修改名称空间为InheritanceContract,代码如下:

 
  
static void Main( string [] args)
{
using (InheritanceContract.InheritanceServiceContractClient client = new InheritanceContract.InheritanceServiceContractClient())
{
Console.WriteLine(client.Operation1());
Console.WriteLine(client.Operation2());
Console.WriteLine(client.Operation3());
}

Console.ReadLine();
}

      10、设置InheritanceClientA2为启动项目,运行项目,将在控制台看到如下输出:

 
  
IInheritanceServiceA.Operation1() invoked.
IInheritanceServiceA.Operation2() invoked.
IInheritanceServiceA2.Operation3() invoked.

      11、此时再次设置 InheritanceClientA为启动项目,运行项目,将在控制台看到如下输出:

 
  
IInheritanceServiceA.Operation1() invoked.
IInheritanceServiceA.Operation2() invoked.

      从以上DEMO可以看出服务契约的继承不会对原来的客户端产生影响,然而这种不严格的版本管理是否应该采纳,还是应该根据具体的情况来进行权衡。

      至此,WCF契约版本管理—服务契约的继承介绍完毕。

      点击下载DEMO

 
  
作者:心海巨澜
出处:http:
//xinhaijulan.cnblogs.com
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

转载于:https://www.cnblogs.com/xinhaijulan/archive/2010/10/17/1853393.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值