WCF REST服务不会返回Entities的子级 [英] WCF REST service won't return children of Entities

查看:125
本文介绍了WCF REST服务不会返回Entities的子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用REST模板编写了WCF服务,该模板的defaultOutgoingResponseFormat设置为Json.在此之下,我使用Entity Framework和ObjectContext构建了一个简单的实体模型,以便传递自定义POCO实体.

I have written a WCF service with the REST template that has the defaultOutgoingResponseFormat set to Json. Under that, I have built a simple entity model using Entity Framework and ObjectContext, in order to pass around custom POCO entities.

如果我传递单个实体,则系统将按预期工作.如果我将子代添加到实体,则REST响应为空.在调试器中,正确填充了实体,但是服务本身根本不返回任何信息.

If I pass a single entity, the system works as expected. If I add children to the entity, the REST response is blank. In the debugger, the entity is populated correctly, but the service itself returns nothing at all.

例如,我有一个Trip.Get()方法. WCF代码如下所示:

So, for instance, I have a Trip.Get() method. The 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在工作版本中如下所示:

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)/","Id":"d6413d96-fe1f-4b1c-ae7a-3bbf516cdc2f", 名称":测试123",照片":null,"PlacesOfInterest":null,"WhereTo":"Orlando,FL"}

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

但是,如果我更改Library方法来添加子级,则REST服务将返回一个空白值.没事,娜达.

If I change the Library method to add in the children, however, the REST service returns a blank 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;
    }
}

在WCF服务的return语句中,调试器显示该实体已完全正确地填充.

The debugger, in the WCF service on the return statement, shows that the entity is fully and correctly populated.

我确定我只是缺少一些魔法属性,希望以前拥有圆顶的人可能能够帮助我!

I am certain that I am just missing some magic attribute, and am hoping that someone who has dome this before might be able to help me out!

推荐答案

根据您的删除反向跟踪导航属性的小测试,您对序列化为JSON遇到了问题.默认序列化无法跟踪对象引用,因此当它开始序列化Trip时,它会遵循导航属性到达感兴趣的点,并且首先找到对Trip的引用.因为它不跟踪引用,所以它遵循导航属性并再次序列化行程(并再次遵循其导航属性)=>无限循环.

According to your small test with removing back tracking navigation property you have problem with serialization to JSON. Default serialization is not able to track object references so when it starts serializing your Trip it follows navigation property to points of interest and in first of them it finds reference to Trip. Because it doesn't track references it follows the navigation property and serializes trip again (and again follows his navigation properties) => infinite loop.

您必须像在测试中一样删除您的回溯导航属性,或者必须告诉序列化程序跟踪引用或将该属性排除在序列化之外(嗯,我不确定在使用JSON的情况下第一个选项会做什么) ).我猜您正在使用默认的WCF序列化,所以要么:

You must either remove your back tracking navigation property as you did in test or you must tell serializer either to track references or to exclude that property from serialization (well I'm not sure what the first option will do in case of JSON). I guess you are using default WCF serialization so either:

  • [DateContract(IsReference = true)]标记每个实体,并用[DataMember]属性标记每个序列化的属性以开始跟踪引用.
  • 或使用[IgnoreDataMember]属性标记回溯导航属性,以将该属性排除在序列化之外
  • Mark each entity with [DateContract(IsReference = true)] and each serialized property with [DataMember] attributes to start tracking references.
  • Or mark back tracking navigation property with [IgnoreDataMember] attribute to exclude the property from serialization

这篇关于WCF REST服务不会返回Entities的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆