Hi,

I have created an application to send data from WCF server application to my client side application. For this I have added a custom serialization instead of the default serialization. And now its sending data to the client as stream and I am able to de-serialize the object back in client application. But now my function only work for specific type. For instance if I am returning Class of type Customer its serializing and de-serializing based on the class I am specifying. I need to write a generic class which will accept any type of object for serialization and de-serialization. How can I do this?

Following is the piece of code that I want to generalize.

public class CustomBodyWriter : BodyWriter
    {
        private IEnumerable<List<Customer>> Customers;

        public CustomBodyWriter(IEnumerable<List<Customer>> customers)
            : base(false) // False should be passed here to avoid buffering the message 
        {
            this.Customers = customers;
        }

        protected override void OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Customer>));
            writer.WriteStartElement("Customers");
            foreach (List<Customer> customer in Customers)
            {
                serializer.Serialize(writer, part);
            }
            writer.WriteEndElement();
        }
    }

public IEnumerable<List<Customer>> GetAllCustomersImpl()
        {
            TestStreamingBL TestStreamingBL = new TestStreamingBL();

            List<Customer> list = new List<Customer>();

            int count = 1;

            foreach (Customer customer in TestStreamingBL.GetAllCustomers())
            {
                list.Add(customer);
                if (count == 100)
                {
                    count = 1;

                    yield return list;

                    list.Clear();
                }
                count++;
            }
            yield break;
        }

In above code when ever the "yield return list" execute OnWriteBodyContents will get called

Any idea???

Anilal

You'll have to make heavy use of reflection and make sure you get the attributes (XmlAttribute, XmlElement, XmlRoot, XmlIgnore, etc.) and execute the appropriate methods (OnDeserializing, OnDeserialized, OnSerializing, OnSerialized).

You'll need to determine what order your serializer will place attributes (alphabetical, as coded order, as specified by attributes) and if you are going to serialize just public or include private values.

Just some of the stuff I can think of off the top of my head, I'm sure there are things I've left out :)

Hi Momerath,

Thanks for your reply..... I could solve the problem. Following is the Generic class for my custombodywriter....

public class CustomBodyWriter<T> : BodyWriter 
{
   private IEnumerable<List<T>> serializableObjects;

   public CustomBodyWriter(IEnumerable<List<T>> serializableObjects)
           : base(false) // False should be passed here to avoid buffering the message
   {
      this.serializableObjects = serializableObjects;
   }

   protected override void OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer)
   {

     XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
     string startElement = typeof(T).Name + "s";
     writer.WriteStartElement(startElement);

     foreach (List<T> serializableObject in serializableObjects)
     {
        serializer.Serialize(writer, serializableObject); 
     }

     writer.WriteEndElement();
   }
}

Once again thanks for the support.....

Anilal

Not really writing your own, you've just placed a wrapper around calls to XmlSerializer :)

Yea thats what I want. I am returning objects as separate junks to the client to speed up the data grid loading.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.