1
Reply

Help for creating a generic method for CustomBodyWrier inherited from BodyWriter

Anilal Sambasivan

Anilal Sambasivan

Mar 1 2011 2:26 AM
2k
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

Answers (1)