0

I have the following scenario:

public class WidgetBaseDTO
{
    public int WidgetID
    {
        get;
        set;
    }
}

public class WidgetTypeA : WidgetBaseDTO
{
    public string SomeProperty1
    {
        get;
        set;
    }
}


public class WidgetTypeB : WidgetBaseDTO
{
    public int SomeProperty2
    {
        get;
        set;
    }
}

and my web service returns the following dashboard object whereas the Widgets collection could be of either type A or B:

public class DashboardDTO
{
    public List<WidgetBaseDTO> Widgets
    {
        get;
        set;
    }
}

my problem is that although the client receives correct JSON content, which is dependent on the Widget type, when reading the response content, they are all being translated to WidgetBaseDTO. what is the correct way to convert these objects to the relevant types?

this is how the response is being read:

string relativeRequestUri = string.Format("api/dashboards/GetDashboard?dashboardID={0}", dashboardID);
using (var client = new HttpClient())
{
    // set client options
    client.BaseAddress = this.BaseUri;
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // make request               
    HttpResponseMessage response = client.GetAsync(relativeRequestUri).Result;
    if (response.IsSuccessStatusCode)
    {                   
        DashboardDTO dashboard = response.Content.ReadAsAsync<DashboardDTO>().Result;
    }
2
  • 1
    Can you share how you are reading the response at the client side? Commented Mar 30, 2014 at 15:24
  • just shared the piece of code. please see above. Commented Mar 31, 2014 at 2:05

1 Answer 1

2

I believe after receiving the response you are probably trying to cast WidgetBaseDTO to either WidgetTypeA or WidgetTypeB and you are seeing null? if yes, then you can try after making the following setting to the Json formatter on the server...make sure to make this setting on the client side's json formatter too.

config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;

The above setting will cause the type information of WidgetTypeA or WidgetTypeB to be put over the wire which gives a hint to the client as to the actual type of the object being deserialized...you can try looking at the wire format of the response to get an idea...

Client side:

JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
jsonFormatter.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
WidgetBaseDTO baseDTO = resp.Content.ReadAsAsync<WidgetBaseDTO>(new MediaTypeFormatter[] { jsonFormatter }).Result;
Sign up to request clarification or add additional context in comments.

2 Comments

Added this on the server, which seem to return the object type in the response correctly but not so clear where to add this line on the client. especially since my client does not have a reference to system.web.http.GlobalConfiguration. was also wondering if there might be a way to do this on a per call basis instead of globally...
I updated my answer with how you would do it on the client side. ReadAsAsync has overloads which allows you to explicitly supply a formatter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.