WCF错误序列周期的参考 [英] WCF error serializing cycle reference

查看:65
本文介绍了WCF错误序列周期的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图去返回到另一个对象,反之亦然引用对象列表。



我只希望延迟加载获得第一级的孩子 ,我的意思是,如果我有一个地方属性人的对象,我要加载的地方数据,但需要加载没有地方对象中的每个对象......因为这会提前一个循环参考...



我读过,我可以通过使用中的每个对象[DataContract(IsReference = TRUE)做到这一点。



我已经设置在与装饰(由EF自动生成)模型中的每个对象,但试图把它送回来给服务调用者时,它仍然失败。



我缺少什么?先谢谢了。


解决方案

我的意思是,如果我有一个人对象
,其中A地点的属性,我要加载的
处的数据,但不是在地方对象每
对象必须是
加载中...




使用迟缓装载时这是不可能的。一旦实体被序列化的序列化器将访问的每一个属性。每次访问导航属性将触发延迟加载和序列化器将继续加载性能=>它总是会完全序列化对象图。在您的情况这可能意味着:




  1. 串行器将开始与一个具有导航属性为地点

  2. 延迟加载将加载地点和串会序列化。

  3. 如果在地点有导航属性到所有延迟加载将触发并加载所有的人引用地点

  4. 串行器将启动序列的每一个加载 - 如果 IsReference 设置为您将获得与对象图周期异常。如果不是,将创造巨大的信息。



也就是说,如果你尝试使用延迟加载时,序列化对象发生了什么非常基本的解释。如果您的实体有其他导航属性相同的作用会发生他们。在最坏的情况下,你可以很容易地建立一个操作,它会试图从数据库提取并序列的所有数据。这很可能会导致超时。



有更多一个问题延迟加载。序列化需要的经营范围之外的地方。所以,如果你关闭/处置的ObjectContext 在操作时,实体触发延迟加载,你会得到一个异常。



在WCF暴露的实体时,请勿使用延迟加载或使用DTO来控制哪些数据应该从操作进行传递。


I'm trying to return lists of objects that have references to another objects and viceversa.

I only want the lazy load to get the "first level children", I mean, if I have a "Person" object with a "Place" property, I want the place data to be loaded but not every object in "Place" object needs to be loaded... because this would ahead to a cyclic reference...

I've read that I could do this by using [DataContract(IsReference = true)] on every Object.

I've set every object in the model (auto-generated by EF) with that decoration but it's still failing when trying to send it back to the service caller.

Am I missing anything? Thanks in advance.

解决方案

I mean, if I have a "Person" object with a "Place" property, I want the place data to be loaded but not every object in "Place" object needs to be loaded...

That is not possible when using lazy loading. Once the entity gets serialized the serializer will access every single property. Accessing every navigation property will trigger lazy loading and serializer will continue with loaded properties => it will always serialize complete object graph. In your scenario it can mean:

  1. Serializer will start with a Person which have navigation property to Place
  2. Lazy loading will load the Place and serializer will serialize.
  3. If the Place have navigation property to all Persons lazy loading will trigger and load all persons referencing the Place!
  4. Serializer will start serializing every single loaded Person - if IsReference is set to false you will get exception with cycles in object graph. If not it will create huge message.

That is very basic explanation what happens if you try to serialize object when using lazy loading. If your entities have other navigation properties the same effect will happen for them. In the worst case you can easily build an operation which will try to pull and serialize all data from the database. That will most probably lead to timeout.

There is one more problem with lazy loading. Serialization takes place outside of operation scope. So if you close / dispose ObjectContext in the operation you will get an exception when entity triggers lazy loading.

Don't use lazy loading when exposing entities on WCF or use DTO to control what data should be passed from the operation.

这篇关于WCF错误序列周期的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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