WCF Disable Deserialization Order Sensitivity

后端 未结 4 571
说谎
说谎 2020-12-09 15:01

I have a recurring problem when passing Serialized objects between non-.NET Clients, and .NET WCF Services.

When WCF Deserializes objects, it is strictly dependant

相关标签:
4条回答
  • 2020-12-09 15:42

    There's an old thread on this here:

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/a891928b-d27a-4ef2-83b3-ee407c6b9187

    Looks like the only option is to swap the serializer, but then it becomes opt in which is even more annoying.

    Edit: you could write your own serializer to re-order the elements and then pass it on to DataContractSerializer.

    0 讨论(0)
  • 2020-12-09 15:42

    For me, the DataContractSerializer has been my savior with .NET Framework 4.0

    When referencing an old asmx web service, we had null values for all the fields that were "after" inserted new fields in the server wsdl. So whenever there were new fields added on server side, that was nullifying several values on client side. This was forcing us into updating our reference in our code to always stick to the server wsdl.

    I found two solutions to make our client tolerant to new fields in the xml stream:

    • Either use an old web reference in visual studio (old client code generator for compatibility)
    • Or use a WCF classic reference, but change the serializer in the svcmap file. ClientOptions -> Serializer = "DataContractSerializer" instead of "Auto". I found out that in our case, the "Auto" value was the same as "XmlSerializer" value.
    0 讨论(0)
  • 2020-12-09 15:49

    You can specify the serialization order by decorating the elements in your data contract:

    [DataContract]
    public class Foo 
    { 
      [DataMember(Order=1)]
      public int ID { get; set; } 
    
      [DataMember(Order=2)]
      public int Bar { get; set; } 
    }
    

    So you can make sure the serialization order is the same all the time. But there is no way to tell the deserializer to "forget" about the order - the point is: this is handled by means of an XML schema, and done using the <xs:sequence> element - and that does imply and require order. You cannot just turn that off, I'm afraid.

    Based on that XML schema, your non-.NET clients should be able to verify whether or not their XML that they're about to send conforms to that schema - and if it doesn't because the Bar and ID element have been swapped, they shouldn't be sending that invalid XML.

    0 讨论(0)
  • 2020-12-09 16:02

    You can use the IsRequired property of the DataMember attribute to specify that the elements are required. In that way, instead of getting "mysterious" null value, you'll get a more explicit error message indicating that a required element is missing.

    [DataContract]
    public class Foo 
    { 
      [DataMember(IsRequired=true, Order=1)]
      public int ID { get; set; } 
    
      [DataMember(IsRequired=true, Order=2)]
      public int Bar { get; set; } 
    }
    

    What's happening in your case is:

    • The DataContract expects Bar and ID elements in that order (alphabetic because you haven't specified an explicit order).

    • It encounters an ID element without a preceding Bar element. Since Bar is not required, it simply ignores it.

    • The ID element following Bar is ignored because it is in the wrong position.

    Having said that, setting IsRequired to true will only help in version 1 of your contract. Elements added in subsequent versions will normally have IsRequired set to false. MSDN has an article on data contract versioning.

    0 讨论(0)
提交回复
热议问题