Contract Serialization vs XML Serialization

7 באוקטובר 2008

אין תגובות

 

WCF Exposes complex data type with Contract Serialize by default. Contract serialization enables the developer to expose types in fast and easy way especially complex type, but it's limited when it comes to perform simple xml serialization tasks like changing the class member serialization from element to attribute.

 

Consider the following scenario:

  1. Customer class type:

namespace WcfServiceSerialization

{

    [DataContract]   

    public class Customer

    {

        [DataMember]

        public int ID { get; set; }

       

        [DataMember]

        public string Name { get; set; }

 

        [DataMember]

        public string Address { get; set; }

    }

}

 

 

  1. Service Contract:

namespace WcfServiceSerialization

{

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]

        Customer GetCustomerByID(int id);

    }

}

 

  1. Service Implementation:

namespace WcfServiceSerialization

{

     public class Service1 : IService1

    {

        public Customer GetCustomerByID(int id)

        {

            Customer c = new Customer() { ID = id,

Name = "avi",

Address = "TA" };

            return c;

        }

    }

}

 

This service exposes one method that returns the customer class type, the xml format of the customer data that returned in the soap message body:

<s:Body u:Id="_0">

    <GetCustomerByIDResponse xmlns="http://tempuri.org/">

      <GetCustomerByIDResult xmlns:a="…">

        <a:Address>TA</a:Address>

        <a:ID>0</a:ID>

        <a:Name>avi</a:Name>

      </GetCustomerByIDResult>

    </GetCustomerByIDResponse>

  </s:Body>

 

We can see that the public members Address, ID and name appears as elements, controlling this and changing the format of the response xml is limited by contract serialization, more complex and advanced xml formatting is possible by changing the default serialization method of the WCF to xml Serialization, and setting the type format according to the classical XML Serialization in .NET.

 

To change the default serialization of the WCF we add the [XmlSerializerFormat] attribute to the exposed contract:

 

namespace WcfServiceSerialization

{

    [ServiceContract]

    [XmlSerializerFormat]

    public interface IService1

    {

        [OperationContract]

        Customer GetCustomerByID(int id);

    }

}

 

And modify the complex type class to the required xml format, the following sample returns the ID and Name members as attribute in the soap message:

 

namespace WcfServiceSerialization

{

    public class Customer

    {

        [XmlAttribute]

        public int ID { get; set; }

       

        [XmlAttribute]

        public string Name { get; set; }

 

        public string Address { get; set; }

    }

}

 

The data result:

<s:Body u:Id="_0" xmlns:xsi="…">

    <GetCustomerByIDResponse xmlns="…">

      <GetCustomerByIDResult ID="2" Name="avi">

        <Address>TA</Address>

      </GetCustomerByIDResult>

    </GetCustomerByIDResponse>

  </s:Body>

In this we can control the serialization of the complex type xml format.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים