Search code examples
c#jsonwcfsilverlightria

WCF / WCF Ria Service returns relational data in "IncludedResults"


WCF/ WCF ria services return proper data when we call it in a silverlight application using invoke an operation, but when we use this service in angular or call it from postman returns relational data outside the RootResults with separate "IncludedResults".

Service/API method -

public IQueryable<viewauthor> getauthors(int author_id)
        {
            IQueryable<viewauthor> lstAuthor = null;
            lstAuthor = this.ObjectContext.books.Include("books").Where(p => p.author_id == author_id).AsQueryable();
            IEnumerable<viewauthor> lstResult = lstAuthor.ToList().Trim();
            return lstResult.AsQueryable();
        }

Entity framework metadata "author.metada.cs" -

[MetadataTypeAttribute(typeof(ViewAuthor.ViewAuthorMetadata))]
    public partial class ViewAuthor
    {
    public string AUTOR_ID { get; set; }
    ....
    ....

    [Include]
    public EntityCollection<books> Books { get; set; }
    ....
    ....
    }

Actual JSON Response from "getauthors" API -

{
  "SelectDocDetailsByMattterIDResult": {
    "TotalCount": 1,
    "IncludedResults": [
      {
        "__type": "books:#library.Web.Data",
        "book_id": 1,
        "title": "Test book 1"
      },
      {
        "__type": "books:#library.Web.Data",
        "book_id": 2,
        "title": "Test book 2"
      },

    ],
    "RootResults": [
      {
        "author_id": 1,
        "ADD_TIME": "\/Date(1559300437353+0530)\/",
        "name": "test author"
      }
    ]
  }
}

Expected JSON -

{
  "SelectDocDetailsByMattterIDResult": {
    "TotalCount": 1,
    "RootResults": [
      {
        "author_id": 1,
        "ADD_TIME": "\/Date(1559300437353+0530)\/",
        "name": "test author",
        "books": [
          {
            "__type": "books:#library.Web.Data",
            "book_id": 1,
            "title": "Test book 1"
          },
          {
            "__type": "books:#library.Web.Data",
            "book_id": 2,
            "title": "Test book 2"
          },

        ]
      }
    ]
  }
}

I need the same JSON response as SilverLight application, What I did wrong? Please help.


Solution

  • How did you define the service interface, and which binding the server uses? Please refer to my definition.

            [OperationContract]
            [WebInvoke(ResponseFormat =WebMessageFormat.Json,RequestFormat =WebMessageFormat.Json)]
            List<Product> SayHello();
    
        public class Product
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<Animal> Animals { get; set; }
    
    
        }
        public class Animal
        {
            public int ID { get; set; }
            public string Name { get; set; }
    
    }
    

    Result.
    enter image description here
    Besides, the default serializer is XMLSerializer, we could use DataContractSeriailizer to simplify the serialization, please refer to below link.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-known-types
    Feel free to let me know if there is anything I can help with.