Wednesday, 9 April 2014

WCF Serializer

WCF provides a message-oriented programming framework. We use WCF to transmit messages between applications. Internally WCF represents all the messages by a Message class. When WCF transmits a message it takes a logical message object and encodes it into a sequence of bytes. After that WCF reads those bytes and decodes them in a logical object. The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding .Net objects. This process is called serialization.

WCF supports three basic serializers:

  • XMLSerializer
  • NetDataContractSerializer
  • DataContractSerializer
WCF deserializes WCF messages into .Net objects and serializes .Net objects into WCF messages. WCF provides DataContractSerializer by default with a servicecontract. We can change this default serializer to a custom serializer like XMLSerializer.
[XmlSerializerFormat]
[ServiceContract]
public interface IService1
{
    [OperationContract]
    void AddAuthor(Author author);
}The XmlSerializerFormat attribute above the ServiceContract means this serializer is for all operation contracts. You can also set a separate contract for each operation.

Each serializer calls different algorithms for mapping between .Net object and WCF messages. And each serializer produces a slightly different message.

We can use SvcUtil.exe to move between .Net types and XSD. We can export an XSD that describes what the serializer expects and can import an XSD to product types for a specific serializer.

XMLSerializer

We can find XMLSerializer in the System.Xml.Serialization namespace. WCF supports this serialization from .Net 1.0. It is used by default with the ASP.Net webservices (ASMX).

Usage
  • We can use this serializer whenever we want to get backward compatibility with ASMX.
  • It can also be used when integrating with non WCF Services.
NetDataContractSerializer

NetDataContractSerializer is analogous to .Net Remoting Formatters. It implements IFormatter and it is compatible with [Serializable] types. It is not recommended for service oriented design.

Usage
  • It is used when we have WCF services at both sides.
  • It is easier when we want to migrate .Net remoting applications.
DataContractSerializer

DataContractSerializer is a default serializer for WCF. We need not to mention DataContractSerializer attribute above the service contract. 

Serializing and Encoding
Windows Communication Foundation supports three serializers: XmlSerializer, DataContractSerializer, and NetDataContractSerializer. Each of these comes with different mapping algorithms and customization techniques. (See Figure 1 for a comparison.) Nevertheless, each performs the same fundamental task—mapping between .NET objects and XML Infosets.

Feature XmlSerializer DataContractSerializer NetDataContractSerializer
Explicitness Opt-out Opt-in *
Opt-out **
Default mapping Public fields/props All [DataMember]s *
All fields **
Attribute required No Yes
Default order Same as class Alphabetical
XML Schema Extensive Constrained
Code generator Xsd.exe SvcUtil.exe
Override IXmlSerializable ISerializable
Type fidelity No NetDataContractSerializer
Versioning support No Yes
Initialization Constructor Callbacks
Compatibility ASMX .NET Remoting
* Using [DataContract] ** Using [Serializable]

DataContractSerializer is the default and is always used unless specified otherwise. You can choose a different serializer by annotating the service contract with an attribute, as shown here:
[XmlSerializerFormat]
[ServiceContract]
public interface IEchoService
{
    [DataContractFormat]
    [OperationContract]
    Person EchoPerson(Person person);

    [OperationContract]
    Address EchoAddress(Address address);

    [OperationContract]
    Phone EchoPhone(Phone phone);
}

difference between XMLSerializer and the DataContractSerializer?




a. DataContractSerializer is the default serializer fot the WCF
b. DataContractSerializer is very fast.
c. DataContractSerializer is basically for very small, simple subset of the XML infoset.
d. XMLSerializer is used for complex schemas.


[2] XmlSerializer cannot serialize private members while the DataContractSerializer serializes private members also.

[3] XmlSerializer can serialize only public types. If you are trying to serialize a class that is marked private by InvalidOperation Exception will be thrown by the serializer. DataContractSerializer can serialize both private and public types.

[4] For any types that needs to be serialized by XmlSerializer must have a default constructor. While DataContractSerializer don't needs that.

[5] XmlSerializer gives more control over the generated xml structure compared to the DataContractSerializer.
For ex, if a field should come as an attribute or element.

[6] XmlSerializer cannot able to serialize types that implements IDictionary, for ex. Dictionary type cannot be serialized. DataContractSerializer can do that.

[7] You can serialize a type marked with [DataContract] attribute with XmlSerializer but the serializer don't care about the [DataMember] attribute it serializes all the public members whether they are marked with [DataMember] or not and don't serializes private members.

[8] You can serialize a type that marked with [Serializable] attribute with DataContractSerializer. It serializes all the members (private, public) even they are marked with [XmlIgnore].


DataContractSerializer -vs- XmlSerializer


 Advantages:
1. Opt-in rather than opt-out properties to serialize. This mean you specify what you want serialize
2. Because it is opt in you can serialize not only properties, but also fields.  You can even serialize non-public members such as private or protected members. And you dont need a set on a property either (however without a setter you can serialize, but not deserialize)
3. Is about 10% faster than XmlSerializer to serialize the data because since you don’t have full control over how it is serialize, there is a lot that can be done to optimize the serialization/deserialization process.
4. Can understand the SerializableAttribute and know that it needs to be serialized
5. More options and control over KnownTypes

No comments:

Post a Comment