0

I have an MVC Web App and a Web API Service that I am trying to retrieve information from. The problem is in the CapitalMailOrders entity collection is missing items when it is deserialized on the web side.

The Service uses the below to retrieve the information

 var result = db.Contacts
        .Include(a => a.IDXPageLinks)
        .Include(b => b.ReboGatewayLoginInfoes)
        .Include(c => c.SocialMedias)
        .Include(d => d.WebSiteInfoes)
        .Include(e => e.ContactImages)
        .Include(f => f.RealtorSetUpProcesses.Select(f1 => f1.CapitalMailOrders)
        .Include(g => g.Contact_CarrierCode_Assignments)
        .FirstOrDefault(c => c.ContactID == id);

This code is good and returns the below on the service side. The below image shows 3 CapitalMailOrders which is what there should be.

enter image description here

But when it's deserialized on the Web side I only get 2 the 3rd is null

enter image description here

here is the Web Side Repository Code

   public Contact Get(int id)
    {
        var responseStream =
            requestMethod.GetResponseStream(
                requestMethod.getRequest("GET", "application/json",
                    string.Format("{0}/api/contact/{1}", restService, id)).GetResponse());

        var contacts = deSerialize<Contact>(responseStream) as Contact;
        return contacts;
    }

deSerialize is in the base repository class

public class BaseRepository
{
    protected readonly string restService = ConfigurationManager.AppSettings["restService"];

    protected readonly RequestMethod requestMethod = new RequestMethod();
    protected ISerialization _serializer;

    protected BaseRepository()
    { }

    protected object deSerialize<T>(Stream stream)
    {
        var retval = _serializer.DeSerialize<T>(stream);
        return retval;
    }

    protected string serialize<T>(T value)
    {
        var retval = _serializer.Serialize<T>(value);
        return retval;
    }
}


public class JsonNetSerialization : ISerialization
{
    public string Serialize<T>(object o)
    {
        return JsonConvert.SerializeObject((T)o);
    }

    public object DeSerialize<T>(Stream stream)
    {
        return JsonConvert.DeserializeObject<T>(new StreamReader(stream).ReadToEnd());
    }
}

Any ideas? Thanks

Tim
  • 1,249
  • 5
  • 28
  • 54

1 Answers1

0

This post pointed me to the problem and suggested solution. In a nutshell the poster @Darin advised to return the data collection from the web api using models rather than the entity collection. Since there is no databinding to the db context when serializing the data back to the web app theres really no reason to carry the overhead and problems of trying to serialize the entity collection. This blog post goes into more detail.

Community
  • 1
  • 1
Tim
  • 1,249
  • 5
  • 28
  • 54