Serialization and Encoding in WCF
An important part of communication between WCF Applications is Serialization . Serialization and deserialization are processes involved in converting objects to stream of data and then retrieving objects .
DataContractSerializer
WCF provides new serialization engine called DataContractSerializer which performs serialization and deserialization .During serialization , DataContractSerializer converts data into XML Stream by using objects of XmlWriter class .During deserialization , DataContractSerializer reads XML Stream and retrieves data by using objects of XmlReader class.DataContractSerializer is the default and is always used unless specified otherwise.
NetDataContractSerializer
DataContractSerializer does not support sharing of CLR type information so WCF introduced NetDataContractSerializer to address this issue. NetDataContractSerializer includes CLR type information in the serialized XML
XmlSerializer
WCF also supports the XmlSerializer class. The XmlSerializer class is not unique to WCF. The XmlSerializer class does not support data contract types.
When using Svcutil.exe or the Add Service Reference feature in Visual Studio to generate client code for a third-party service, or to access a third-party schema, an appropriate serializer is automatically selected for you. If the schema is not compatible with the DataContractSerializer, the XmlSerializer is selected.
When to manually Switching to the XmlSerializer
When migrating an application from ASP.NET Web services to WCF, you may want to reuse existing, XmlSerializer-compatible types instead of creating new data contract types.
Web Services Description Language (WSDL) document is not available, for example, when creating a service with types that have to comply to a certain standardized, published schema that is not compatible with the DataContractSerializer.
When creating services that follow the legacy SOAP Encoding standard.
[ServiceContract]
[XmlSerializerFormat]
public class EmployeeService
{
[OperationContract]
public void AddEmployee(Employee emp)
{
// Code not shown.
}
}
//Employee is not a data contract class,
//but is an XmlSerializer-compatible class instead.
public class Employee
{
[XmlElement]
public int employeeCode;
[XmlElement]
public string employeeName;
[XmlElement]
public string employeeSalary;
[XmlElement]
public Manager manager;
}
//Notice that the Manager class must also be XmlSerializer-compatible.
No comments:
Post a Comment