Can WCF REST services support deserializing XML messages with an arbitrary element order?

I have a C # WCF REST service that allows me to add a bookmark by sending an HTTP POST request with entries serialized in XML. I have specified an operation contract for a service that picks up xml and automatically deserializes the object for me. My problem is that deserialization works correctly if the XML elements in the request are listed in alphabetical order and the values ​​for any elements that are out of order are not populated.

This was reported elsewhere .

I find this rather unsatisfactory; I find it necessary to build XML in a certain order to be unnecessary, and a serious headache. This is a basic requirement to add services to all clients and is a source of potentially complex debugging issues.

Is it possible to associate WCF with an attribute of an agnostic XML element?


Additional information for clarification:

My job contract is as follows:

[OperationContract] [WebInvoke(Method="POST", UriTemplate = "/{username}/bookmarks", ResponseFormat = WebMessageFormat.Xml)] public void PostBookmark(string username, RestBookmark newBookmark); 

RestMessage is as follows:

 [DataContract(Name = "Bookmark", Namespace = "")] public class RestBookmark { [DataMember] public string BookmarkMd5 { get; set; } [DataMember] public string Url { get; set; } [DataMember] public string UserName { get; set; } [DataMember] public string Title { get; set; } [DataMember] public string Description { get; set; } } 

If I send the following XML message, when I call PostBookmark (), only the UserName property of the RestMessage object will be populated:

 <?xml version="1.0"?><Bookmark><UserName>nick.street</UserName><Url>http://www.stackoverflow.com</Url><BookmarkMd5>f184eb3347cf94f6ce5f5fc2844e3bdd</BookmarkMd5><Description>Developer Forum</Description><Title>Stack Overflow</Title></Bookmark> 
+6
c # rest wcf
source share
4 answers

DataContractSerializer requires strict ordering of elements for both versions and performance.

There are several options that I can think of

  • write a message inspector to override the raw XML elements in AfterReceivedRequest before deserializing the dataContractSerializer.

  • using XmlSerializer for your services.

  • Design an HTTP process for XML XML messages instead.

  • pass an object as a dictionary (not recommended)

+1
source share

I have not tried to do this myself, but I will assume.

Would it help to create an [OnDeserializing] event in your DataContract that would fire before deserialization? At this point, you may be able to reorder the xml, but you need to order it so that deserialization works as expected.

If you have Juval Lowy Programming WCF Services 2nd Edition, this applies on page 107.

Here is the MSDN help page .

0
source share

If you do not specify Order in your DataMember to reflect the order of the nodes, then you must alphabetically order the nodes in your xml.

0
source share

Try setting order 0 in the DataMember attribute (for all participants):

[DataMember (Name = "Title", Order = 0)]

-one
source share

All Articles