Reader Level:
ARTICLE

WCF Serialization Part 1: Day 6

Posted by Akshay Patel Articles | WCF with C# October 05, 2012
In this article we will discuss serialization in WCF. Default serializer in WCF and different kinds of serializers WCF supports.
  • 11
  • 0
  • 24512

This is Day 6 article. If you have not read previous articles, please go through following articles:

  1. Day 1 - WCF Introduction and Contracts
  2. Day 2 - WCF Fault Contracts
  3. Day 3 - WCF Message Exchange Patterns
  4. Day 4 - WCF DataContract
  5. Day 5 - WCF Difference between service application and service library

Introduction

In this article we will discuss serialization in WCF, including the default serializer in WCF and the various kinds of serializers that WCF supports.

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-Serialization.jpg

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.

Usage

  • Used by default unless we specify otherwise.

  • It is the best choice for code-first designs.

In my next article we will see step-by-step implementation of DataContractSerializer and code demonstration.

Article Extensions
Contents added by gkvnadh viswanadh on Oct 07, 2012
Hi!  Akshay Patel 


I read your serices of WCF with C# articles

Day-1 to Day-7 till now

please be hits for 

In each MEPs[when to use which pattern].
as well
In each Serializers[which serializer which senerios]

such information also ...gives more helpful in realtime service implementation.


I am excepting more articles...Day-8............to...............Day-50.

waiting for your further articles
COMMENT USING

Trending up