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!