WCF 和 DataContractSerializer 的 IsReference DataContract 属性
我有一个从 POCO/EF4 构建的相当大的对象图,我希望使用 WCF 将其交付给客户端应用程序。我已将所有内容连接起来并且工作得很好,即我已:
确保 POCO 装饰有 DataContract(IsReference=true)
使用 DataMember 属性修饰的属性
关闭了 EF4 数据上下文的LazyLoading和代理创建
在我的 LINQ 查询中使用 EagerLoading 来选择加载哪些关联对象
增加了 WCF maxBufferSize 和 maxReceivedMessageSize 以确保传输大图
我构建了一个小型测试 WCF 控制台应用程序,它执行服务方法并接收对象图。然后,应用程序遍历图表并打印图表中各个对象的属性。
问题:许多属性在不应该为 null 的情况下却被报告为 null
调查:我直接针对 EF4 datacontext 测试了相同的查询,而没有使用 WCF 服务,并且图表没有对关联对象的空对象引用。
进一步调查:我使用 WCF 服务跟踪工具来检查通过网络传输的 XML,一切看起来都很好,即 IsReference 属性确保引用对象在 XML 中正确编码,就像“PupilSet”一样下面的集合:
<d4p1:Type></d4p1:Type>i758</d4p1:Personal>
<d4p1:PupilId>769375</d4p1:PupilId>
<d4p1:RollNo>BENNAW</d4p1:RollNo>
<d4p1:Sets>
<d4p1:PupilSet z:Ref="">i616</d4p1:PupilSet>
<d4p1:PupilSet z:Ref="">i47</d4p1:PupilSet>
<d4p1:PupilSet z:Ref="">i691</d4p1:PupilSet>
结论:所有带有 z:Ref="" 的 XML 条目在反序列化过程中都没有被重新构造回对象引用 - 因此为 null。
解决方法:我从 PupilSet DataContract 中删除了“IsReference=true”,并且一切正常
问题:何时适合使用“IsReference=true” “?为什么 DataContractSerializer 无法使用正确的对象引用而不是 null 反序列化 XML?
I have a fairly large object graph build from POCOs/EF4 which I wish to deliver to client apps using WCF. I have everyting wired up and working quite well, i.e. I have:
ensured that the POCOs are decorated with DataContract(IsReference=true)
decorated properties with the DataMember attribute
turned off LazyLoading and Proxy creation for the EF4 data context
used EagerLoading in my LINQ queries to select which associated objects are loaded
increased the WCF maxBufferSize and maxReceivedMessageSize to ensure the large graph would be transmitted
I have build a small test WCF console app which executes a service method and receives the object graph. The app then walks through the graph and prints out properties of the various objects in the graph.
Problem : many properties were reported as null when they shouldn't be
Investigation: I tested the same query directly against EF4 datacontext without using the WCF service and the graph did not have null object references to associated objects.
Further Investigation: I used the WCF Service Trace Tool to inspect the XML going over the wire and it all looked fine, i.e. the IsReference property ensured that reference objects were correctly encoded in the XML as with the "PupilSet" collection below:
<d4p1:Type></d4p1:Type>i758</d4p1:Personal>
<d4p1:PupilId>769375</d4p1:PupilId>
<d4p1:RollNo>BENNAW</d4p1:RollNo>
<d4p1:Sets>
<d4p1:PupilSet z:Ref="">i616</d4p1:PupilSet>
<d4p1:PupilSet z:Ref="">i47</d4p1:PupilSet>
<d4p1:PupilSet z:Ref="">i691</d4p1:PupilSet>
Conclusion: all XML entries with z:Ref="" were not being reconstituted back to object references during de-serialization - hence the null.
Workaround: I removed "IsReference=true" from the PupilSet DataContract and all worked fine
Question: When is it appropriate to use "IsReference=true"? Why was the DataContractSerializer unable to deserialize the XML with the correct object reference rather than null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论