none
Enforcing schema on incoming json RRS feed

  • Question

  • Hi,

    I'm writing a WCF service, and seem to have encountered a behavior that's not changeable: WCF will allow incoming json to specify additional properties without throwing an error, or allowing me to. I'd like to avoid this because it introduces confusion to the API. Consider a scenario around a bookmark site: when POSTing to /bookmarks to add a new bookmark, a user could specify the Id parameter believing it is an Update call instead of a New call. The return of a POST to /bookmarks would be a bookmark json object with the newly created Id.

    The closest documentation I've found here is the IgnoreExtensionDataObject property, but it explicitly calls out the behavior I'm trying to avoid:

    Regardless IgnoreExtensionDataObject setting, always processes known data (both in and out) and does not throw exceptions when extra data comes in. You can also set this property using the <dataContractSerializer> element in an application configuration file.

    http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.ignoreextensiondataobject.aspx

    Are there any ways I can turn this off? Alternately, what are my options... I am considering direct access to the Stream object and creating my own dynamic json object (code like: http://stackoverflow.com/questions/3142495/deserialize-json-into-c-dynamic-object/3806407). But my object has embedded arrays and transforming it into a real Bookmark object (the one with the DataContract/DataMembers) seems like a pain.

    Thanks!

    Sunday, April 3, 2011 9:06 PM

Answers

  • Hello, I think this is not a problem in most cases. You should provide documentation for your service (or use the default help page generated by .NET 4). And as long as the service API is designed with common conventions (for example, POST means update and PUT means create), Client developers should understand the meaning of the APIs. You can further return 400 if you find the client adds additional parts in the URI. WCF doesn't do it automatically, but WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri gives you the detail URI client requests.
    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    Windows Azure Technical Forum Support Team Blog
    • Marked as answer by Yi-Lun Luo Friday, April 8, 2011 9:54 AM
    Tuesday, April 5, 2011 1:30 AM