Data Contract Serialization Events in WCF

In WCF we use the DataContract to serialize and deserialize the custom data. Sometime you may come across a scenario do some work before or after the data gets serialized or deserialized. These are following four events provided on the DataContract.

image

You are free to give any method name provided method should return void and takes StreamingContext as input parameter. In a block diagram we can show events sequence as follows:

image

You can create these events inside the class decorated with DataContract. Now let us see some real time examples. Let us say you have a DataContract as follows:

 


[DataContract]
    public class Product
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public double Price { get; set; }
     }
}


We have a very simple ServiceContract defined as follows:

 


[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        Product GetProduct(Product p);
      
    }


Service is implemented as follow which is basically echoing the data provided by the client.

 


public class Service1 : IService1
    {


        public Product GetProduct(Product p)
        {
            Product p1 = new Product { Name = p.Name, Price = p.Price };
            return p1; 
        }
    }


So far great now let is consume the service and examine the sequence of the serialization and deserialization events. Service is consumed as follows

 


Service1Client proxy = new Service1Client();
            Product p = new Product { Price = 300, Name = "Pen" };
            var result = proxy.GetProduct(p);
            Console.WriteLine(result.Name + result.Price);
            Console.ReadKey(true);


Now let us go ahead and modify the data contract class by adding the events. Modified class will look like as follows:

 


using System.Runtime.Serialization;

namespace DataContractEventsDemo
{
    [DataContract]
    public class Product
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public double Price { get; set; }

        [OnSerializing]
        void OnSerializing(StreamingContext context)
        {

        }
        [OnSerialized]
        void OnSerialized(StreamingContext context)
        {

        }
        [OnDeserializing]
        void OnDeserializing(StreamingContext context)
        {

        }
        [OnDeserialized]
        void OnDeserialized(StreamingContext context)
        {

        }
    }
}

To find out sequence in which events get executed put a break point on the events. You will find events get executed in following sequence

image

Now consider a real time scenario that if client is not passing price of the product then service should provide a default price value. You can do that in OnDeserialized event as follows:

 

[OnDeserialized]
        void OnDeserialized(StreamingContext context)
        {
            if(Price==0)
            {
                Price = 99; 
            }
           
        }

Now if modify the client and not provide price as shown below

 


  static void Main(string[] args)
        {

            Service1Client proxy = new Service1Client();
            Product p = new Product { Name = "Pen" };
            var result = proxy.GetProduct(p);
            Console.WriteLine(result.Name + result.Price);
            Console.ReadKey(true);
        }

You will get output with default price as follows

clip_image002These events are useful to provide default values, closing connections to data base etc.Hope you find the post useful. Happy Coding.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com