Search code examples
.netwcfdatacontractserializer

z:Id = "" in WCF Message log. DataContractSerializer throws exception when Deserializing


I'm trying to deserialize messages from a captured WCF Message log (svclog) using the DataContractSerializer.

Some objects throw an exception when try to call dataContractSerializer.ReadObject()

The exception thrown is: "Invalid Id ''. Must not be null or empty"

On further investigation I found the problematic object in the svclog.

<personHeader xmlns:d4p1="http://schemas.datacontract.org/2004/07/Contosso.BusinessObjects"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<EntityKey xmlns:d5p1="http://schemas.datacontract.org/2004/07/System.Data" i:nil="true"
xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"></EntityKey>
<UId xmlns="http://schemas.datacontract.org/2004/07/Contosso.BusinessObjects">F62C446B-C74E-4272-8338-7AF3D2957AC6</UId>
<d4p1:CustomerType>ABC</d4p1:CustomerType>
<d4p1:Screen>ConfigurePeople</d4p1:Screen>
<d4p1:ShowAllDisplayToPublic>true</d4p1:ShowAllDisplayToPublic>i1</personHeader>

What might be the real issue and how do I work around it?


Solution

  • It seems that ID attribute/field is marked as Required and in the above xml value of Id is null/empty (z:Id="").

    Workaround

    1. If you'd like to deserialize only above xml, add some dummy value to id
    2. Modify dataconract defination to not to mark ID field as Required i.e. IsRequired="false".

    HTH...