Share via


WCF and eBay

eBay provides a very rich, though very complicated, SOAP API.  It is possible to use WCF with eBay, but it takes a few tricks.  First read this article.  I'll wait.  Ok, since that didn't scare you away I'll continue.  Everything there is still necessary except the custom message encoder for removing the quotes around the content type.  In fact the basicHttpBinding produced by svcutil.exe is just fine, though bumping up some of the quotas (maxNameTableCharCount, maxBufferSize, maxReceivedMessageSize) might not hurt.  Now calling GeteBayOfficialTime just works, but graduating to say GetItem, there's another problem.  GetItem returns a GetItemResponseType which derives from AbstractResponseType.  After successfully calling GetItemResponseType, all the fields it inherits are correctly populated but the fields it defines haven't been.  Looking at the response on the wire, those fields are there, for some reason they weren't deserialized.  The problem is eBay defined the schema for AbstractResponseType with an xs:any, so the code generated for this type includes an array of XmlElements with the XmlAny attribute:

[System.Xml.Serialization.XmlAnyAttribute(Order=12)]

public System.Xml.XmlElement[] Any

When the response from eBay arrives the XmlSerializer happily begins populating all the fields in AbstractResponseType and anything it doesn't recognize gets put into Any. Now when it's time to populate the fields defined in GetItemResponseType there's no xml left. Given this knowledge it's an easy fix, but unfortunately it involves manually editing the generated proxy. All that's necessary is to change the XmlAnyAttribute to an XmlIgnoreAttribute. Now upon receiving the response, the XmlSerializer "saves" any elements that don't map to AbstractResponseType and uses them to populate GetItemResponseType.