vb.netwcfwcf-data-servicesdatacontractserializerdatacontract

Data Contract not marked as serializable while creating WCF proxy


I have marked my data contract as serializable, below I am attaching my sample code :

Imports System
Imports System.Runtime.Serialization
Imports System.Xml
Imports System.Xml.Serialization

<DataContract()>
<Serializable()>
Public Class USR_USER_CONTRACT
<DataMember()>
Public Property USR_USERID() As Global.System.String
<DataMember()>
Public Property USR_LOGINID() As Global.System.String
End Class

After creating WCF proxy class my data contract not marked as serializable as below:

<System.Diagnostics.DebuggerStepThroughAttribute(),
System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0"),
System.Runtime.Serialization.DataContractAttribute(Name:="USR_USER_CONTRACT", [Namespace]:="http://schemas.datacontract.org/2004/07/Ebix.Evolution.EntityContract")>
Partial Public Class USR_USER_CONTRACT
Inherits Object
Implements System.Runtime.Serialization.IExtensibleDataObject

Private extensionDataField As System.Runtime.Serialization.ExtensionDataObject

Private CLM_BUDGETTIMEField As System.Nullable(Of Short)

Private PropertyUSR_DEFAULTASSFILESField As String

Please help.

Thanks


Solution

  • In WCF, the default class can be serialized by DataContractSerializer. Therefore, there is no need to decorate the class with the Serializable attribute. Generally, there are two ways to serialize a class in WCF. One is DataContractSerializer, which requires the data class decorated with DataContract or without any decorations. The other is XML serializer, it requires that we add XmlSerializerFormatAttribute on the Operation Contract or ServiceContract, and then we could use XMLAttribute/XMlElementAttribute to control the serialization.

    [ServiceContract,XmlSerializerFormat]
    public interface IService
    {
        [OperationContract]
        string Test(Product p);
    }
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    Please refer to the official documentation.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-the-xmlserializer-class
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
    Feel free to let me know if there is anything I can help with.