Search code examples
.netwcfwcf-client

duplicate properties created for class with attributes "DataContract" and "Serializable"


I have a class with 2 attributes on it, "DataContract" and "Serializable". This class is being used in a service. This service is being consumed by a project. I have added the reference of the service in the project by using the "add service reference" functionality of VS (Right Click on references -- Add -- Service Reference). Whenever I add the reference of the service, duplicate properties of the class is created in the Reference.cs file of the service reference, 1 with "DataMember" attribute and other with "Serializable" attribute. This is causing a build error. If I do not put the Serializable attribute, the service reference works fine with just 1 set of properties of the class with "DataMember" attribute.

Any idea why is this happening and what can we do to avoid this? Please note that both the attributes are required for the class i.e. I cannot remove any of the attributes.


Solution

  • Can you explain why you need both the attributes?

    DataContractSerializer works fine with Serializable attribute.

    But, for BinaryFormattor to work, you need Serializable attribute.

    Find a way to get rid of DataContract attribute.

    --EDIT--

    WCF uses DataContractSerializer by default to serialize/deserialize objects over channels. According to MSDN documentation,

    @ DataContractSerializer Class

    // You must apply a DataContractAttribute or SerializableAttribute
    // to a class to have it serialized by the DataContractSerializer.
    

    So, for WCF to work, you can get rid of DataContracts.