WCF serialization issue when moving existing class properties to newly
created base class
I'm seeing the serialization issue at the client side for the following
scenario.
Let's say we have the following class and method in our service code
[DataContract]
public class Derived
{
[DataMember]
public string A { get; set; }
[DataMember]
public string B { get; set; }
[DataMember]
public string C { get; set; }
[DataMember]
public string D { get; set; }
}
and the service interface has the method
[OperationContract]
Derived GetDerived();
Implementation of the method -
public Derived GetDerived()
{
var d = new Derived() {A = "A", B = "B", C = "C", D = "D"};
return d;
}
For this WCF service if I create the client proxy (by adding service
reference) and do the GetDerived() call, everything works fine.
Let's change the entity in service code little bit
[DataContract]
public class Base
{
[DataMember]
public string B { get; set; }
}
[DataContract]
public class Derived : Base
{
[DataMember]
public string A { get; set; }
[DataMember]
public string C { get; set; }
[DataMember]
public string D { get; set; }
}
So in this case I created a new Base class and moved property 'B' to the
Base class. With the same old proxy at the client side, if I do GetDerived
call, I do not see the value of A properly serialized at the client side.
However, the SOAP message for previous version and the current version is
the same (only order changed). I came across this
http://msdn.microsoft.com/en-us/library/ms729813.aspx article explaining
the ordering. Since I moved 'B' to base class, I don't see anyway to
control it's order in derived class.
Is there any workaround to this problem? This breaks the backward
compatibility to the client.
On the other hand, If I add the new property, let's say C1 to Derived
class (or any existing class), it changes the order as well. How come this
doesn't break the client? vs. the scenario that I mentioned earlier.
No comments:
Post a Comment