Deserializing derived type in WCF

Last post 05-13-2008 10:38 AM by tortuga. 1 replies.

Sort Posts:

  • Deserializing derived type in WCF

    05-13-2008, 8:06 AM
    • Loading...
    • tortuga
    • Joined on 02-15-2008, 5:31 AM
    • Posts 24

    Hi,

    I've got a javascript function that passes a JSON object to a method on a .svc endpoint. The object is sends would look like this

    obj = { "Name" : name, "Longitude" : longitude, "Latitude" : latitude };

    so to use the service I would use

    MyApp.Web.PlaceService(obj, successCallback)

    as a bare mininum. Problem is that I would sometimes like to pass

    obj = { "Name" : name, "Longitude" : longitude, "Latitude" : latitude, "Radius" : radius };

    These correspond to two c# types I have (LocationPoint, and LocationPointWithRadius) and, guess what, the radius one derives from the LocationPoint. The problem I'm having is that I really pass

    obj = { Place : new Array({ "Name" : name, "Longitude" : longitude, "Latitude" : latitude, "Radius" : radius }...) } (with or without the radius)

    I've tried adding the __type attribute to the item in 'Place,' when I want to add a location point with a radius but the serialiser doesn't make sense of it. I have tried adding the known type attributes all over the place but still the deserialiser cant sort it out. Can anyone give me a few pointers, even if it is just some ways to debug as this all seems to be taken care of before my devenv can spot it.

    Many thanks,

    tortuga

  • Re: Deserializing derived type in WCF

    05-13-2008, 10:38 AM
    Answer
    • Loading...
    • tortuga
    • Joined on 02-15-2008, 5:31 AM
    • Posts 24

    Ok, so I was doing a couple of things wrong.

    First up, when I create the object in javascript, it's no use trying to fudge the __type attribute. What you end up with is a LocationPointWithRadius that got instantiated as a LocationPoint. The key is to look at the js output of the svc file.

    Some way down the generated script is a var "gtc" and below this is the registration of the types that your endpoint exposes. This is why specifying ServiceKnownTypeAttribute makes a difference, you can easily see the type that is going to be required when you run the service. So I stopped trying to hack it and rewrote the javascript to create the type at runtime like so;

    var test = new MyCompany.Project.Web.Services.LocationPointWithRadius(); //go on to poulate the fields of this object.

    or;

    var test = new MyCompany.Project.Web.Services.LocationPoint(); //go on to poulate the fields of this object.

    I have to decorate the service with the derived type in ServiceKnownTypeAttribute(LocationPointWithRadius), as the method only exposes the LocationPoint. In my case the Place object contains a List<LocationPoint>, so this is how the service javascript knows about the base type, the decoration tells it to expect the derived type too. 

    Now I can see how this works it is the coolest thing, but I know my understanding is very basic. However I can build my place object in javascript containing a collection of LocationPoints with or without radius, and it works, so happy days. Please add to this thread if you can help me and others to understand.

    Thanks,

    tortuga

Page 1 of 1 (2 items)