【问题标题】:WCF Versioning restful way problemWCF Versioning 宁静方式问题
【发布时间】:2010-04-21 14:56:05
【问题描述】:

我在 Icontact 和 wcf 服务中有两种方法,我想为新需求修改一种方法。 并希望现有客户调用旧代码。新客户端调用新更改的方法和现有方法以实现向后兼容性。

代码:

[ServiceContract(Namespace = "http://www.testk.com/1/11", Name = "cService")]
public interface ICService
{
   [OperationContract(Name="GetWorkingDetails")]
   void GetWorkingDetails(int a);

   void GetList(int a);
}

//service Implementation
public class MyService : ICService
{
   [WebGet]
   void GetWorkingDetails(int a)
   {
     ////
   }

   [WebGet]
   void GetList(int a)
   {
      ////
   }
}

我在这里进行版本控制.....

[ServiceContract(Namespace = "http://www.testk.com/2/11", Name = "cService")]
  public interface ICServicev1
{
    [OperationContract(Name="GetWorkingDetails")]
    void GetWorkingDetailsV2(int a);
}

<endpoint address="" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="" name="v0"
          contract="ICService" />
<endpoint address="r1" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="" name="v1"
          contract="ICServicev1" />

当我调用现有方法时效果很好,当我调用service.svc/r1/GetWorkingDetails 时效果很好。但我也想打电话给service.svc/r1/GetList,这在之前的合同中。我怎么能称之为向后兼容。

谢谢

【问题讨论】:

    标签: wcf versioning backwards-compatibility


    【解决方案1】:

    我猜service.svc/r1/GetList 是不可能的。因为您没有将ICService 继承到ICServicev1(即public interface ICServicev1 : ICService)。事实上,您可以通过以下方式实现这一目标。

    1) 使用void GetList(int a); 方法创建ICServiceCommon 接口。

    2) 继承ICServiceCommon接口到ICServiceICServicev1[public interface ICService : ICServiceCommonpublic interface ICServicev1 : ICServiceCommon]

    3) 将ICServiceICServicev1 接口实现到MyService 类[public class MyService : ICService, ICServicev1]。

    旧版本客户端调用相同的方法。 [向后兼容]

    service.svc/GetWorkingDetails
    service.svc/GetList
    

    新客户可以调用

    service.svc/r1/GetWorkingDetailsV2
    service.svc/r1/GetList
    

    希望这种方法有用。

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2012-06-02
      • 2014-09-06
      • 2011-01-26
      • 1970-01-01
      相关资源
      最近更新 更多