且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WCF 版本控制:更新属性命名空间并支持以前的命名空间

更新时间:2023-11-24 16:04:10

IMO,您需要在(***)旧端点为以前的客户托管旧服务,并在新端点拥有新服务.当所有旧客户端迁移到新版本时,您可以取消旧服务.或许,您可以使用继承来减少您的工作量,例如

IMO, you need to host old services for your previous clients at (preferably) old end points and have new services at new end points. You can take out old services when all your old clients migrate to newer version. Perhaps, you can use inheritance to reduce your efforts, for example

[DataContract(OldNameSpace)]
ClassA {
 ...
}

[DataContract(NewNameSpace)]
ClassB : ClassA {
}

同样,从新的服务契约继承来创建新的服务契约.服务实现不需要改变,除非它需要实现新的契约.现在您必须配置两个端点 - 一个用于旧合同,另一个用于新合同.

Similarly, create new service contract from inheriting from new one. Service implementation need not change expect it needs to implement new contract. Now you have to configure two end point - one for old contract and another for new contract.

放置示例接口和实现

假设您的旧合同类似于

public interface IOldContract
{
    ClassA GetFoo();
    void DoBar(ClassA a);
}

现在您可以选择新合同作为

Now you can choose the new contract either as

public interface INewContract
{
    ClassB GetFoo();
    void DoBar(ClassB b);
    ClassB GetMix(ClassB a);
}

或作为

public interface INewContract2 : IOldContract
{
    ClassB GetFoo2();
    void DoBar2(ClassB b);
    ClassB GetMix2(ClassB b);
}

我倾向于采用后来的变化(因为新合同与旧合同保持兼容).但是在您的情况下,您可以选择前者,因为无论如何您都会暴露两个端点.现在你需要修改服务实现如下:

I tend to go with later variation (as new contract remains compatible with old one). But in your case, you can choose former as you will be anyway exposing two endpoints. Now you need modify service implementation as follows:

    public class ServiceImplementation : INewContract2
{
    #region INewContract2 Members

    public ClassB GetFoo2()
    {
        // Your old implementation goes here 
    }

    public void DoBar2(ClassB b)
    {
        DoBar(b);
    }

    public ClassB GetMix2(ClassB b)
    {
        return GetMixHelper(b);
    }

    #endregion

    #region IOldContract Members

    public ClassA GetFoo()
    {
        return GetFoo2();
    }

    public void DoBar(ClassA a)
    {
        // You old implementation goes here
    }


    public ClassA GetMix(ClassA a)
    {
        return GetMixHelper(a);
    }

    #endregion

    private ClassB GetMixHelper(ClassA a)
    {
        // Your old implementation goes here
    }

}

我希望你能明白.即使在这里,您也有多种代码组织选择.您可以有两个骨架服务实现类 - 一个用于旧合同,另一个用于新合同.两者都会将实际功能委托给一个辅助类(这是您当前的实现).

I hope that you get the idea. Even here also you have multiple choices of code organization. You can have two skeleton service implementation classes - one for old contract and another for new contract. Both will delegate actually functionality to a helper class (which is your current implementation).