none
Strange WCF exception: NetDataContractSerializer asking for "Setter" in properties that already has it! RRS feed

  • Question

  • I have something similar to the flowing two simple and similar classes:

    <DataContract> _ Public Class C1 Public Sub New(x_ As Int64) _x= x_ End Sub <DataMember()> _ Public Property x As Int64 End Class <DataContract> _ Public Class C2 Public Sub New(y_ As string) _y= y_ End Sub <DataMember()> _ Public Property y As string End Class

    and the following structure that made of both:

    <DataContract, StructLayout(LayoutKind.Sequential), _
     KnownType(GetType(C1)), KnownType(GetType(C2))> _
    Public structure S1
        <DataMember, MessageHeaderArray> _
        Public Second() As C1
        <DataMember> _
        Public First As C2
    End structure

    Finally, I'm calling a sub from a server's interface that sends an array of those structures as follows:

    <ServiceContract(CallbackContract:=GetType(IClient))> _
    Public Interface IServer
        <OperationContract> _
        Sub SendToServer(ByVal S()As S1)
    End Interface

    The problem is when I'm trying to pass this using WCF I'm getting a strange exception of the following format:


    "The use of type 'S1' as a get-only collection is not supported with NetDataContractSerializer. Consider marking the type with the CollectionDataContractAttribute attribute or the SerializableAttribute attribute or adding a setter to the property."

    I tried to use the two suggested attributes, but I found that they should be used on declaring new class collection of types public class xx(Of T), not for my case (an exception telling that when used), I really can't understand about which get-only property he speaking about, because I have none, anyway, removing Public Second() As C1 from the structure, or passing one instance of the structure instance of an array, fixes the problem.

    So I tried to change S1 to a class instead of structure, and the array into a List with get & set parts, but nothing helps, any clue what is going here?




    • Edited by TMSxDN Friday, August 1, 2014 8:40 AM
    Friday, August 1, 2014 8:34 AM

All replies

  • I believe what is happening here is that with the DataContractSerializer, the EntitySet<T> is recognized as a collection type, and the serializer has special rules in order to handle that, even if the underlying type is not marked with DataContract. However, with NetDataContractSerializer, that handling is not available, so it is expecting EntitySet<T> itself to be marked as serializable, which it is not. If you look in the error, it is complaining about the EntitySet<T>, which is not attributed with DataContractAttribute.

     

    See the following topic for more info on NetDataContractSerializer and collection types:

    http://msdn.microsoft.com/en-us/library/aa347850.aspx

     

    The above topic says that with the NetDataContractSerializer, collection types not marked with CollectionDataContractAttribute lose their special meaning. I think this is referring to the special handling that is done in order to recognize that something is a collection, regardless of how it is attributed.

    Monday, August 4, 2014 11:18 AM