Hi all!
I'm working with WCF and the DataContractSerializer to serializer a somewhat
deep tree of entities (less than 10 levels). I'm using the PreserveReferences
option as I've got entities that have a circular reference.
The application has been out for about 2 months, and it's been working just
fine. Two days ago, however, when deserializing a received entity tree, I've
started getting a SerializationException with the text : "Deserialized object
with reference id xxx not found in stream".
I've traced it, and found that it is caused by entities in the XML stream,
that due to the PreserveReferences option, are not serialized as themselves,
but only refer to one instance to that entity somewhere else in the stream.
That one instance comes up null when deserializing, when it really shouldn't,
as I can see that it has all the values it needs. It's also a very simple
entity, just a few fields and properties.
This has been happening in only that entity tree (I've got others that work
just fine), and it's only been happening in XML received from 4 computers out
of about 100 in the field.
If I remove the refering entities, and replace them with the direct fields
and properties of the one instance entity, then everything works just fine.
How can I debug or somehow attach to DataContractSerializer, and find out
why it returns null ? Or maybe tell DataContractSerizalizer to not store a
reference to those entities ,and just serialize them as they are ?
Nicholas Paldino [.NET/C# MVP] - 10 Jan 2008 21:03 GMT
Miki,
Can you show some of the XML for the serialized version of the type?
You should be able to get an XML representation of the instance, since it is
a DataContractSerializer.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi all!
>
[quoted text clipped - 35 lines]
> why it returns null ? Or maybe tell DataContractSerizalizer to not store a
> reference to those entities ,and just serialize them as they are ?
Miki Watts - 11 Jan 2008 07:56 GMT
Nicholas,
Thanks for the reply.
Here's the part that doesn't get deserialized:
<c:SalesPerItem z:Id="240">
<isInLoading
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord">false</is
InLoading>
<validationResults z:Id="241"
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord"
xmlns:d="http://schemas.datacontract.org/2004/07/Microsoft.Practices.EnterpriseLibrary.Va
lidation">
<d:validationResults z:Id="242" z:Size="0"></d:validationResults>
</validationResults>
<c:OwnerItem z:Ref="200" i:nil="true"></c:OwnerItem>
<c:OwnerItemID>7540</c:OwnerItemID>
<c:Sale1 z:Id="243"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales">
<isInLoading
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord">false</is
InLoading>
<validationResults z:Id="244"
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord"
xmlns:e="http://schemas.datacontract.org/2004/07/Microsoft.Practices.EnterpriseLibrary.Va
lidation">
<e:validationResults z:Id="245" z:Size="0"></e:validationResults>
</validationResults>
<d:BonusItem z:Ref="144" i:nil="true"></d:BonusItem>
<d:Code z:Id="246">1+1</d:Code>
<d:Description z:Id="247">1+1</d:Description>
<d:GeneralDiscountPercent>0</d:GeneralDiscountPercent>
<d:GeneralDiscountSum>0</d:GeneralDiscountSum>
<d:ItemAmountPerSale>2</d:ItemAmountPerSale>
<d:ItemsAmountInDiscount z:Id="248" z:Size="0"
xmlns:e="http://schemas.microsoft.com/2003/10/Serialization/Arrays"></d:ItemsAmountInDiscount>
<d:ItemsAmountInPrice z:Id="249" z:Size="0"
xmlns:e="http://schemas.microsoft.com/2003/10/Serialization/Arrays"></d:ItemsAmountInPrice>
<d:MessageToCashier z:Ref="23" i:nil="true"></d:MessageToCashier>
<d:MinimumTransactionAmount>0</d:MinimumTransactionAmount>
<d:MinimumTransactionSum>0</d:MinimumTransactionSum>
<d:MultipleTimesSaleCanRepeat>99</d:MultipleTimesSaleCanRepeat>
<d:Priority>1</d:Priority>
<d:SaleDiscountPercentForItem>100</d:SaleDiscountPercentForItem>
<d:SaleID>1</d:SaleID>
<d:SalePriceForItem>0</d:SalePriceForItem>
<d:SaleType z:Id="250">
<isInLoading
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord">false</is
InLoading>
<validationResults z:Id="251"
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord"
xmlns:e="http://schemas.datacontract.org/2004/07/Microsoft.Practices.EnterpriseLibrary.Va
lidation">
<e:validationResults z:Id="252" z:Size="0"></e:validationResults>
</validationResults>
<d:Code z:Id="253">102</d:Code>
<d:Priority>4</d:Priority>
<d:SaleTypeID>3</d:SaleTypeID>
<d:lastModifiedDate>10398240</d:lastModifiedDate>
</d:SaleType>
<d:SaleTypeLevelCode z:Id="254">R</d:SaleTypeLevelCode>
<d:allowMoreThanOneSaleOnItem z:Id="255"></d:allowMoreThanOneSaleOnItem>
<d:appliesForAllCustomers z:Id="256">Y</d:appliesForAllCustomers>
<d:appliesForAllItems z:Id="257"></d:appliesForAllItems>
<d:appliesToAllBranches z:Id="258">Y</d:appliesToAllBranches>
<d:canApplyMultipleLevels z:Id="259"></d:canApplyMultipleLevels>
<d:canBeAppliedForItemReturn z:Id="260">Y</d:canBeAppliedForItemReturn>
<d:dateForSendingToCashRegister>10517188</d:dateForSendingToCashRegister>
<d:endDate>10527840</d:endDate>
<d:endHourTime>0</d:endHourTime>
<d:isTestScenarioForSales z:Id="261"></d:isTestScenarioForSales>
<d:isValid z:Id="262"></d:isValid>
<d:lastModifiedDate>10529252</d:lastModifiedDate>
<d:shouldDisplayMessageToCashier z:Id="263"></d:shouldDisplayMessageToCashier>
<d:startDate>10516320</d:startDate>
<d:startHourTime>0</d:startHourTime>
</c:Sale1>
<c:Sale10 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale10>
<c:Sale2 z:Id="264"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales">
<isInLoading
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord">false</is
InLoading>
<validationResults z:Id="265"
xmlns="http://schemas.datacontract.org/2004/07/BusinessEntities.ActiveRecord"
xmlns:e="http://schemas.datacontract.org/2004/07/Microsoft.Practices.EnterpriseLibrary.Va
lidation">
<e:validationResults z:Id="266" z:Size="0"></e:validationResults>
</validationResults>
<d:BonusItem z:Ref="144" i:nil="true"></d:BonusItem>
<d:Code z:Id="267">1+0.5</d:Code>
<d:Description z:Id="268">1+שני בחצי מחיר</d:Description>
<d:GeneralDiscountPercent>0</d:GeneralDiscountPercent>
<d:GeneralDiscountSum>0</d:GeneralDiscountSum>
<d:ItemAmountPerSale>2</d:ItemAmountPerSale>
<d:ItemsAmountInDiscount z:Id="269" z:Size="0"
xmlns:e="http://schemas.microsoft.com/2003/10/Serialization/Arrays"></d:ItemsAmountInDiscount>
<d:ItemsAmountInPrice z:Id="270" z:Size="0"
xmlns:e="http://schemas.microsoft.com/2003/10/Serialization/Arrays"></d:ItemsAmountInPrice>
<d:MessageToCashier z:Ref="23" i:nil="true"></d:MessageToCashier>
<d:MinimumTransactionAmount>0</d:MinimumTransactionAmount>
<d:MinimumTransactionSum>0</d:MinimumTransactionSum>
<d:MultipleTimesSaleCanRepeat>999</d:MultipleTimesSaleCanRepeat>
<d:Priority>1</d:Priority>
<d:SaleDiscountPercentForItem>50</d:SaleDiscountPercentForItem>
<d:SaleID>3</d:SaleID>
<d:SalePriceForItem>0</d:SalePriceForItem>
<d:SaleType i:nil="true"></d:SaleType>
<d:SaleTypeLevelCode z:Id="271">R</d:SaleTypeLevelCode>
<d:allowMoreThanOneSaleOnItem z:Id="272"></d:allowMoreThanOneSaleOnItem>
<d:appliesForAllCustomers z:Id="273">Y</d:appliesForAllCustomers>
<d:appliesForAllItems z:Id="274"></d:appliesForAllItems>
<d:appliesToAllBranches z:Id="275">Y</d:appliesToAllBranches>
<d:canApplyMultipleLevels z:Id="276"></d:canApplyMultipleLevels>
<d:canBeAppliedForItemReturn z:Id="277">Y</d:canBeAppliedForItemReturn>
<d:dateForSendingToCashRegister>10003620</d:dateForSendingToCashRegister>
<d:endDate>11581920</d:endDate>
<d:endHourTime>0</d:endHourTime>
<d:isTestScenarioForSales z:Id="278"></d:isTestScenarioForSales>
<d:isValid z:Id="279">Y</d:isValid>
<d:lastModifiedDate>10530056</d:lastModifiedDate>
<d:shouldDisplayMessageToCashier z:Id="280"></d:shouldDisplayMessageToCashier>
<d:startDate>10529280</d:startDate>
<d:startHourTime>0</d:startHourTime>
</c:Sale2>
<c:Sale3 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale3>
<c:Sale4 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale4>
<c:Sale5 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale5>
<c:Sale6 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale6>
<c:Sale7 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale7>
<c:Sale8 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale8>
<c:Sale9 z:Ref="141" i:nil="true"
xmlns:d="http://schemas.datacontract.org/2004/07/BusinessEntities.Entities.Sales"></c:Sale9>
</c:SalesPerItem>
This is of course just a part of the xml and is contained in more xml
entities.
How does the DataContractSerializer handle forward/backward references to
entities? I'm just guessing, but could it be that the SalesPerItem type
doesn't get instantiated because it's waiting for other entity references to
be filled in, and in the meantime some other entity outside the tree I quoted
tries to get a reference to this SalesPerItem ?
I've also seen the DataContractSerializer uses
System.Runtime.Serialization.DiagnosticUtil for tracing information about the
de/serialization process. Is there any way I can activate it so I could see
what was going on ?
> Miki,
>
[quoted text clipped - 41 lines]
> > why it returns null ? Or maybe tell DataContractSerizalizer to not store a
> > reference to those entities ,and just serialize them as they are ?
Miki Watts - 11 Jan 2008 08:09 GMT
I should mention that the deserialization problem happens on the WCF server
side, and it happens due to XML coming from only 4 out of 100 computers that
we have in the field. I haven't been able to determine a cause or difference
between those 4 computers and the other computers that would cause this
problem.
Miki Watts - 11 Jan 2008 08:22 GMT
Btw, I've just cut out the SalesPerItem out the hierarachy, and after fixing
the external references, It got deserialized just fine. When it's part of the
hierarchy, it doesn't get deserialized at all.