【问题标题】:SOAP abstraction through classes not working properly?通过类的 SOAP 抽象不能正常工作?
【发布时间】:2019-02-20 08:40:09
【问题描述】:

WCF 中的原始 SOAP 合同(我故意省略了 DTO 属性以提高可读性):

public class A : B
{
    public int Field1 {get;set;}

    public int Field2 {get;set;}
}

public abstract class B 
{
    public int Field3 {get;set;}
}

一段时间后,这份原始合同交给客户,他们使用它。大家开心。但后来我决定将一个字段移到抽象级别:

public class A : B
{
    public int Field2 {get;set;}
}

public abstract class B 
{        
    public int Field1 {get;set;}//heh, become abstract

    public int Field3 {get;set;}
}

所以,基本上,如果这个字段被添加到每个具体类中,基本上是一样的,而对于 A 没有任何变化,但这会破坏旧客户端。

为什么会这样?如何在 SOAP 中实现 REST 灵活性,您可以在其中添加字段/将其从具体类移动到抽象并且一切正常?

【问题讨论】:

    标签: c# rest wcf


    【解决方案1】:

    wcf 以正确的顺序序列化和反序列化数据。 顺序是抽象类的第一个属性,然后是子类。 所以你原来 DataContact 的顺序是 Field3->1, Field1->2,Field2->3.

    然后你把 Field1 移到你的抽象类中,所以顺序是 Field1->1,Field3->2,Field2->3。

    更改后,客户端和服务器端的属性顺序不匹配。所以你应该改变你的抽象类的顺序。

     [DataContract]
    public abstract class Parent
    {
       // IsRequired is used to test whether has received the property
    
     // if not, it will show error. Order could change the order of the property in message 
        [DataMember(IsRequired = true,Order =1)]
        public int Field3 { get; set; }
        [DataMember(IsRequired = true,Order =2)]
        public int Field1 { get; set; }
    
    }
    

    我知道这种方式只适用于小变化,如果你想改变很多,请考虑使用 DataContractSurrogate https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/data-contract-surrogates

    【讨论】:

    • 如果它只是解决方案,这是非常可悲的。考虑到使用 WCF 时类中的顺序根本不重要,SOAP 过于复杂。
    • 另一种解决方案是 DataContractSurrogate,但我不确定它如何满足您的要求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2018-09-11
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多