The choice between ODataLib and WCF Data Services (which depends on ODataLib) as a client often boils down to the need for control. If your use case is straightforward and you need the most common functionality, WCF DS is probably a good fit. If you need advanced functionality or precise control over how a payload is read, ODataLib may be the better match. All that said, Vitek has already covered conceptually how you would read a service operation using ODataLib.
WCF DS will make reading JSON Light much easier in version 5.1. I will blog about this sometime this week, but the sample you referenced is what you'll need to do for this RC. There is only one new call in the sample I put together - the call to context.Format.UseJson(Func<Uri,ModelResolverResult>)
. Let's talk about why this call is necessary first. OData (at least in the Microsoft world) plays nice with strong typing. $metadata
is a good example of OData describing the data model it is working with. With an Atom payload or even a JSON Verbose payload, much of that type information was carried in the payload. With JSON Light the goal was to strip as much of that metadata out of "transport" payloads as possible.
If the metadata isn't available in-band, it must be made available out-of-band. This is the fundamental requirement behind the signature of the call to UseJson
. In essence, whenever WCF DS needs metadata it will go look up the model for a given URI. If it can't find that model it will eventually dial up the metadata to full. We want to preempt that as it will bloat the payload. We can preempt it by telling WCF DS how to resolve the model ahead of time. This is what the majority of the sample is doing.
Walking through the sample logically (yes, I know there's a number of other optimizations that could have been made - the sample was mostly optimized for readability):
- If the model hasn't been resolved in the past
- Construct a new
XmlReader
for the call to EdmxReader.TryParse
- Name some values for the out params in
EdmxReader.TryParse
- Invoke
EdmxReader.TryParse
- If the call succeeded, cache the parsed model in a local dictionary (parsing is an expensive operation right now)
- If the call failed, put together the errors and throw
- Grab the correct model from the cached models and return it in a
ModelResolverResult
wrapper
Hopefully that makes sense. If it doesn't, I'd love to hear why so that I can make the blog post clearer. And remember, this will get much simpler by the time we release these bits to production.