IsReference DataContract property with WCF and DataContractSerializer
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&g开发者_开发知识库t;
<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?
精彩评论