0

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

tom redfern
  • 30,562
  • 14
  • 91
  • 126
Wasif
  • 13
  • 3
  • Why do you need it to be marked serializable? – tom redfern Jan 20 '20 at 13:02
  • @tomredfern as we need to implement out proc session mode and according to recommendation, object should be in serialize form while assigning object to out proc session type. – Wasif Jan 20 '20 at 13:05
  • If a type is serializable, it is not because the Serializable attribute is used to decorate the type, it is because the type is inherently serializable. If your type is inherently serializable, then you don't need to use the Serializable attribute. – tom redfern Jan 20 '20 at 13:09
  • See this here https://stackoverflow.com/questions/2595104/why-do-you-have-to-mark-a-class-with-the-attribute-serializable – tom redfern Jan 20 '20 at 13:11

1 Answers1

0

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.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22