0
Answer

[OnSerializing] and [OnSerialized] marked method not getting called

Deepak Pathak

Deepak Pathak

14y
9.5k
1
Hi,

I have a DataContract class, on which I have defined 4 event handler methods as shown below:

DataContract class

 [DataContract]
    public class Customer
    {
        [DataMember]
        public int customerID;

        [DataMember]
        public string customerName;

        [DataMember]
        public long phoneNumber;

        [OnSerializing]
        private void OnSerializing(StreamingContext context)
        {
            //This is not getting called.
        }

        [OnSerialized]
        private void OnSerialized(StreamingContext context)
        {
            //This is not getting called.
} [OnDeserializing] private void OnDeserializing(StreamingContext context) {
	    //I know this gets override by OnDeserialized method.
            customerName = "This is set again in OnDeserializing.";
        }

        [OnDeserialized]
        private void OnDeserialized(StreamingContext context)
        {
	    //This is working fine.
            customerName = "This is set again in OnDeserialized.";
        }
    }


Service class
 public class Booking : IBooking
    {
        public string bookTicket(Customer cust)
        {
            return String.Format("Dear {0}, your order has been received by us. " +
              "You will receive a confirmation SMS shortly on your phone no- {1}", cust.customerName, cust.phoneNumber);
        }
    }


Any ideas why the above 2 Serialization methods are not getting called when invoked through client.


Regards,
Deepak