Reputation: 1354
I'm writing a server using ASP.Net WebAPI pre-release bits in VS2012 (I'm happy to use VS2013 although I don't think it will help at the moment). In it I have a bunch of OData controllers exposing some entitites. I'm looking to set up some OData actions on those entities, much like the ODataActionsSample from the ASP.Net samples available on CodePlex.
My first client is a C#-based WPF app. Querying via WCF Data Services is fine.
What I'd like to be able to do is invoke the actions that appear in the OData metadata and then parse their results.
It seems that the native action invocation isn't available in WCF Data Services Client so I need to do it myself using HttpClient. Is that right? If so, I've gone some way down that road already. If I want to invoke an action on an entity, rather than hardcode the URL I simply build a LINQ query that would fetch me that entity, such as (from that ODataActionSample)
Movies.Where(m=>m.Id == 1)
And instead of invoking it I do a .ToString() to get the URL that would be called. Then I can manipulate that URL to add parameters and/or an action.
Now, assuming that works, I get back some JSON. For the ODataActionSample I can check out a movie by POSTing to the URL http://localhost.fiddler:8708/odata/Movies(3)/CheckOut
That gives back JSON like
{
"odata.metadata":"http://localhost:8708/odata/%24metadata#Movies/@Element","#CheckOut":{
},"ID":3,"Title":"Fatal Vengeance 2","Year":2012,"DueDate":"2013-07-24T16:33:14.0699789+10:00"
}
How do I turn that back into my typed Movie object that WCF Data Services has already made for me? I have a feeling it's possible and have played a bit with the DataContractJsonSerializer class in LinqPad but so far haven't had much success.
Also, is there anything I might be able to do with T4 templates or the like to help scaffold out some of these action calls in a type-safe way, perhaps as methods in a partial class extending my client-side entity code?
Upvotes: 1
Views: 2047
Reputation: 393
Currently what we call the 'materializer' in the WCF DS client isn't really public, so I don't think there is an easy way to go from the payload you have -> parse with ODatalib -> Materialize to the 1st class object that was generated.
Instead, I would use the Execute (or Begin/End Execute) methods on your DataServiceContext to invoke the action. Yes, this means you will need to construct a URL, but that shouldn't be a big deal in most cases.
See this article on invoking actions from WCF DS.
Upvotes: 1