心海巨澜

天行键,君子以自强不息;地势坤,君子以厚德载物!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

      在契约版本管理的前二章,我们主要介绍了数据契约对版本的影响,本章我们介绍一下服务契约的变化对版本的影响。服务契约的接口支持继承功能,我们可以定义一个契约层级,但是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
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

posted on 2010-10-17 10:34  心海巨澜  阅读(2206)  评论(2编辑  收藏  举报