Search code examples
c#wcfsoapwsdl

Adding Property to Object in WCF Service Breaks Client


We consume a WCF service using C# code. The client was generated in Visual Studio by right-clicking "Add Service Reference" and pointing it at the WSDL.

Recently, the WCF provider adding some properties to one of the objects they serialize. The class went from

public class MyClass
{
        public string Foo { get; set; }
        public string Baz { get; set; }
        public string Zed {get; set; }
}

to this:

public class MyClass
{
        public string Foo { get; set; }
        public string Bar { get; set; } //<= New Property
        public string Baz { get; set; }
        public string Zed {get; set; }
}

On our end, this caused Baz and Zed to suddenly start being null when deserialized, until we updated the service reference. In fact, the real object had some ~20 properties alphabetically after Bar, and they were all null (or 0 for ints, false for bools, etc).

It seems an odd way for the deserialization to fail. It didn't throw an exception or ignore the new properties it didn't know anything about.... it just made every property that appeared alphabetically after the new one deserialize to the default value.

So my question is, what's going on here and how do I prevent it? Preferably, I'd like some kind of setting for the client to tell it to "ignore new properties," but telling the service provider how they can prevent future breaking changes would be fine too.


Solution

  • MSDN has an article which lists the serialization ordering of the datamembers. One key point from that document:

    current type’s data members that do not have the Order property of the DataMemberAttribute attribute set, in alphabetical order.

    So if you add a new property, without the Order-property of the DataMemberAttribute, the property is alphabetically ordered.

    Based on discussion here, your only options are:

    1. Change the serializer to something else
    2. Make sure that the order of the elements in XML matches the order of your properties. Maybe you can always use the Order-property of the DataMemberAttribute?