WCF REST service will not return children from Entities

I wrote a WCF service with a REST template that has the defaultOutgoingResponseFormat parameter set for Json. In doing so, I created a simple entity model using the Entity Framework and ObjectContext to pass custom POCO objects.

If I pass one object, the system works as expected. If I add children to the object, the REST response will be empty. In the debugger, the object fills correctly, but the service itself does not return anything.

So, for example, I have a Trip.Get () method. WCF code looks like this:

[WebGet(UriTemplate = "{id}", ResponseFormat = WebMessageFormat.Json)] public Model.Trip Get(string id) { Model.Trip fetchedTrip = null; try { fetchedTrip = Library.Trip.Get(new Guid(id)); } catch (Exception ex) { Debug.Write(ex.Message); } return fetchedTrip; } 

Library.Trip.Get looks like this in the working version:

 public static Model.Trip Get(Guid tripId) { using (Model.POCOTripContext context = new Model.POCOTripContext()) { var tripEntity = context.Trips.FirstOrDefault(c => c.Id == tripId) ?? new Model.Trip(); return tripEntity; } } 

This returns the expected result, which looks like this:

{"ArrivalDate": "/ Date (1334203200000-0400) /", "DepartureDate": "/ Date (1334721600000-0400) /", "Identifier": "d6413d96-fe1f-4b1c-ae7a-3bbf516cdc2f", "Name" : "Test 123", "Photos": null, "PlacesOfInterest": null, "WhereTo": "Orlando, FL"}

If I change the library method to add to child objects, the REST service will return a null value. Nothing, nada.

 public static Model.Trip Get(Guid tripId) { using (Model.POCOTripContext context = new Model.POCOTripContext()) { var tripEntity = context.Trips.Include("PlacesOfInterest").Include("Photos").Include("PlacesOfInterest.PoiAttributes").FirstOrDefault(c => c.Id == tripId) ?? new Model.Trip(); return tripEntity; } } 

The debugger in the WCF service in the return statement indicates that the object is fully and correctly populated.

I’m sure that I’m just missing some magical attribute, and I hope that someone who buys this sooner can help me!

+4
source share
1 answer

According to your little test with removing the tracking tracking property, you are having a problem with JSON serialization. By default, serialization cannot track links to objects, so when it starts to serialize your Trip , it follows the navigation property to the points of interest, and in the first one it refers to Trip . Since it does not track links, it follows the navigation property and serializes the trip again (and again follows its navigation properties) => infinite loop.

You must either remove the tracking tracking property back, as it was in the test, or say that the serializer should track links or exclude this property from serialization (well, I'm not sure what the first option will do in the case of JSON). I assume that you are using standard WCF serialization like this:

  • Check each [DateContract(IsReference = true)] object and each serialized property with [DataMember] to start tracking links.
  • Or, mark the tracking tracking tracking property with the [IgnoreDataMember] attribute to exclude the property from serialization
+6
source

Source: https://habr.com/ru/post/1395048/


All Articles