Answered Serialization issue

  • Thursday, August 27, 2009 9:36 AM
     
     
    I get the following error after
    adding the IsReference = true property on a datacontract attribute

    'CCWin.Shared.CCVTypes.ContractItemType.LineNumber' has the IsRequired setting of 'True. However, 'CCWin.Shared.CCVTypes.ContractItemType' has the IsReference setting of 'True', because either it is set explicitly, or it is derived from a base class. Set IsRequired on 'CCWin.Shared.CCVTypes.ContractItemType.LineNumber' to false, or disable IsReference on 'CCWin.Shared.CCVTypes.ContractItemType'.

    does any one know what cuases this or how to solve it?

All Replies

  • Thursday, August 27, 2009 3:52 PM
    Moderator
     
     Answered

    when you make your datacontract referable using [DC(IsReference=true)] )] the framework is not going to allow the use of [DM(IsRequired=true)] on members of that DC.
    The runtime exception which you are receiving is by-design, since this prevents the serialization stack to artificially emit dummy values for those members for schema validation purposes.

    It should work if you remove the IsRequired property from your datamember


    Amit Sharma
  • Friday, August 28, 2009 1:07 AM
     
     

    Does that mean there is an error with the sample shown on the msdn website http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.isreference(VS.95).aspx

    Below is the sample i am talking about. Is this just a mistake with the documentation or is there a way to have isreference = true and isrequired=true
        ' Define the data contract.
        <DataContract(Name := "Customer", Namespace := "http://www.contoso.com", IsReference := True)> _
        Public Class User
            Private privateName As String
            <DataMember(Name := "Last", EmitDefaultValue := True, IsRequired := True, Order := 2)> _
            Public Property Name() As String
                Get
                    Return privateName
                End Get
                Set(ByVal value As String)
                    privateName = value
                End Set
            End Property

            Private privateAge As Integer
            <DataMember(Order := 1)> _
            Public Property Age() As Integer
                Get
                    Return privateAge
                End Get
                Set(ByVal value As Integer)
                    privateAge = value
                End Set
            End Property

            Public Sub New()
            End Sub

            Public Sub New(ByVal newName As String, ByVal newAge As Integer)
                Name = newName
                Age = newAge
            End Sub
        End Class

  • Friday, August 28, 2009 5:24 PM
    Moderator
     
     
    That is correct.  Unfortunately, the doc has the incorrect example.  I have notified the appropriate group to get this example fixed.  Thanks

    Amit Sharma