1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Serialization and Encoding in WCF

Discussion in 'ASP.NET' started by MinalS, Sep 25, 2014.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    31
    Trophy Points:
    0
    The term serialization is defined as the process of converting the objects and data into a common format for storing the values. Depending on the system, the format can be changed.

    The term encoding is defined as the process of converting the serialized data into a set of bytes. The objects of both the serialized and encoded objects can be sent form one application to another. The process of converting the serialized objects into its original format is known as deserialization.

    The following diagram shows the serialization and encoding process in WCF.

    [​IMG]

    There are classes provided by the WCF for serializing the data. They are as follows:
    1. DataContractSerializer: It is the default serialization class in the WCF application. It is available in System.Runtime.Serialization namespace. It provides default mapping for all the fields and data members. It provides versioning support.
    2. NetDataContractSerializer: It the class available in System.Runtime.Serialization namespace. It serializes and deserializes the object into an XML format. Hence, it can be used in both serialization and deserialization of objects. Same CLR types are shared for both the process.
    3. XmlSerializer: It is the class used to serialize and deserialize the object into XML format. The object built in the .NET framework 2.0 is used for the process. The XMLSerializer class is used for formatting the data.
    4. DataContractSerializer: It is used to serialize and deserialize an object into JavaScript Object Notation (JSON ) format. It can be used to serialize and deserialize the object that is accessed by the web application.

    Custom Serialization



    The process of controlling the serialization and deserialization of an object of a type or class is known as custom serialization. The original functionality of the class is not affected. The compatibility of the application is preserved.

    To make sure that the compatibility exists between two versions of an application, user can customize and serialize the object. The following two ways are used for customizing the data
    1. Custom XmlSerialization by using attributes
    2. Custom XmlSerialization by using IXmlSerializable interface
    1. Custom XmlSerialization by using attributes.

    User can customize the serialization of the object into an XML format by using the attributes provided by the .NET framework. The System.Xml.Serialize namespace contains the attributes.

    The following code snippet demonstrates the custom XmlSericalization of an object.

    Code:
    
    using System.Text;
    using System.Xml.Serialization;
    
    [ServiceContract]
    public interface IService
    {
    	[OperationContract]
    	string ShowOutput();
    }
    
    [DataContract]
    [XmlTypeAttribute]
    public class Class1
    {
    	[DataMember]
    	[XmlAttribute]
    	public string msg;
    }
    
    
    In the above code, the [XmlTypeAttribute] is used to serialize the Class1 into an XML format. The [XmlAttribute] is used for serializing the msg variable.

    When the user accesses the Class1 from the client application, the class object is created. It is serialized into XML format which is readable by the client application. The data stored in the msg variable is serialized and sent to the client application. The client application deserializes the data and displays the value.

    2. Custom XmlSerialization by using IXmlSerializable Interface

    The IXmlSerializable interface is used for customizing the serialization. The interface is used to control the serialization of the object. It consists of methods as GetSchema(), ReadXml() and WriteXml(). They are used for serialize and deserialize the object into an XML format.

    The example to demonstrate the xml serialization is as shown below:

    Code:
    
    using System.xml;
    using System.xml.Serialization;
    using System.xml.Schema;
    
    [ServiceContract]
    public interface IService
    {
    	[OperationContract]
    	string Displayvalue();
    }
    
    [DataContract]
    public class Department: IXmlSerializable
    {
    	int DeptID;
    	string DeptName;
    	
    	public Department()
    	{
    		this.DeptID=deptID;
    		this.DeptName=deptName;
    	}
    	
    	[DataMember]
    	public int DeptID
    	{
    		get { return deptID;}
    		set { deptID=value;}
    	}
    
    	[DataMember]
    	public int DeptName
    	{
    		get { return deptName;}
    		set { deptName=value;}
    	}
    
    	public XmlSchema GetSchema()
    	{
    		return null;
    	}
    	
    	public void ReadXml ( XmlReader reader )
    	{
    		reader.ReadStartElement(“Department”);
    		reader.MoveToAttribute (“ID”);
    		this.DeptID=reader.ReadContentAsInt();
    		reader.MoveToAttribute (“Name”);
    		this.DeptName=reader.ReadContentAsString();
    		reader.ReadEndElement();
    	} 
    	public void WriteXml ( XmlWriter writer)
    	{
    		writer.WriteStartElement(“Department”);
    		writer.WriteStartAttribute(“ID”);
    		writer.WriteString(this.DeptID.ToString());
    		writer.WriteEndAttribute();
    		writer.WriteStartAttribute(“Name”);
    		writer.WriteString(this.DeptID.ToString());
    		writer.WriteEndAttribute();
    		writer.WriteEndElement();
    	}
    }
    
    In the above code, the Department class is inherited from the IXmlSerializable interface present in the System.Xml.Serialization namespace. The DeptID and DeptName properties are created. The GetSchema() method of the interface is defined. The ReadXml() method generates an object from the XML representation. The WriteXml() method converts the object into XML representation.

    Encoding Types



    There are four encoding types provides by the WCF. They are used for converting the object into a set of bytes. They are explained as follows:

    1. Text: It is an encoding type for creating the encode character data. The data consists of sequence of alphabets, numbers, or alphanumeric characters. It is used to store the text in computers and transfer over the network. The example of text encoding is ASCII.

    The following code snippet demonstrates to encode data through Text encoding type.

    Code:
    
    <system.serviceModel>
    	<bindings>
    	<customBindings>
    		<binding name=”binding1”>
    		<textMessageEncoding/>
    		</binding>
    	<customBindings>	
    </bindings>
    </system.serviceModel>
    
    
    2. MTOM: MTOM stands for Message Transmission Optimization Mechanism. It is encoding type used when the interoperability is required and large binary data is to be sent. It is not a default binding to be used. It is used when the binary data size exceeds particular limit.

    The following code snippet demonstrates the MTOM encoding type.

    Code:
    
    <system.serviceModel>
    	<bindings>
    	<customBindings>
    		<binding name=”binding 2”>
    		<mtomMessageEncoding/>
    		</binding>
    	<customBindings>	
    </bindings>
    </system.serviceModel>
    
    
    3. WebMessageEncoder: It is encoding type that supports the JavaScript Object Notation ( JSON) and also Plain Old XML (POX) encoding. It is combined set of encoding styles used by the developer.

    The following code snippet demonstrates the Web encoding type.

    Code:
    
    <system.serviceModel>
    	<bindings>
    	<customBindings>
    		<binding name=”binding 3”>
    		<webMessageEncoding/>
    		</binding>
    	<customBindings>	
    </bindings>
    </system.serviceModel>
    
    
    4. Binary: It is used when a WCF service is communicating with a new WCF service. It used the Binary XML data format. The size of data encoding is smaller as compared to the text.

    The following code snippet demonstrates the Binary encoding type.

    Code:
    
    <system.serviceModel>
    	<bindings>
    	<customBindings>
    		<binding name=”binding 4”>
    		<binaryMessageEncoding/>
    		</binding>
    	<customBindings>	
    </bindings>
    </system.serviceModel>
    
    
     
    Last edited by a moderator: Jan 21, 2017

Share This Page