StackOverflowException 尝试通过 WCF 服务以列表形式返回 Linq 查询结果
我在这方面遇到了很多问题,即使用一个更简单的例子也无法解决。
我有一个 WCF 服务,它返回对象列表(来自我的实体模型)。
该模型只有 2 个表: 人员,包含 id、姓名和 office_id(外键) 和 Office,使用 id 和地址,
我正在制作一个非常简单的 linq 查询,它返回这些人中的第一个。这工作正常,甚至“ToList()”方法也工作正常。
问题是,当我尝试通过服务返回它时,它会崩溃,并显示“mscorlib.dll 中发生了类型为‘System.StackOverflowException’的未处理异常”,
我认为它在尝试序列化 Person 对象时与导航属性有关。这些导航属性出现在 Person 和 Office 模型中,Person 表有一个“Office”导航属性,但 Office 表也有一个“Person”导航属性!
我认为这些导航属性对于其他场景可能很有用,但我找不到一种方法来表示不加载这些内容或至少不序列化并通过服务返回它!
这是 linq 查询(这有效,列表中确实有一个人):
var query = (from chosen in entities.person
select chosen).First<person>();
return query.ToList<person>();
有人见过这样的东西吗?我以前做过几个 WCF-Linq 项目,但从未遇到过类似的问题。
提前致谢。
I have had a lot of problems with this stuff and I can't solve it even with a simpler example.
I've a WCF service that returns a list of objects (from my Entity Model).
The model only has 2 tables:
Person, with id, name and office_id (foreign key)
and Office, with id and address
I'm making a really simple linq query that returns the first of these people. This is working ok, even the "ToList()" method works ok.
The problem is that when I try to return it through the service it crashes with a "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"
I think it's related to the Navigation Properties when it tries to serialize the Person object. These Navigation Properties appear in both the Person and Office model, the Person table has a "Office" Navigation Property, but Office table has a "Person" Navigation Property too!!
I think these navigation properties can be useful for other scenarios but I can't find a way to say not to load those contents or at least not to serialize and return it through the service!!
This is the linq query (this works, the list really has one person inside):
var query = (from chosen in entities.person
select chosen).First<person>();
return query.ToList<person>();
Does anybody has ever seen something like this? I have had several WCF-Linq projects before and I never had any similar issue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您有循环引用问题,请尝试用以下内容装饰您的 Person 和 Office 类:
It sounds like you have a circular reference problem, try decorating your Person and Office class with:
最后我解决了这个问题,在实体对象中将 ProxyCreationEnabled 和 LazyLoadingEnabled 设置为 false:
Finally I solved it setting both ProxyCreationEnabled and LazyLoadingEnabled as false in the entities object:
假设您的 WCF 服务使用默认的 DataContractSerializer,请尝试使用
IgnoreDataMemberAttribute
。Assuming that your WCF service uses the default
DataContractSerializer
, try decorating the navigation properties withIgnoreDataMemberAttribute
.