WCF Serialization Programming Model


DataContract is the default serialization programming model for WCF. However WCF supports more than just the types marked wth DataContract attribute. It supports serialization of the following kinds of types in the default mode.



  1. CLR built-in types

  2. Byte array, DateTime, TimeSpan, GUID, Uri, XmlQualifiedName, XmlElement and XmlNode array [This includes XElement and XNode array from .NET 3.5]

  3. Enums

  4. Types marked with DataContract or CollectionDataContract attribute

  5. Types that implement IXmlSerializable

  6. Arrays and Collection classes including List<T>, Dictionary<K,V> and Hashtable.

  7. Types marked with Serializable attribute including those that implement ISerializable.

  8. Types with none of the above attributes (POCO) but with a default constructor (can be non-public). [This is supported only from .NET 3.5 SP1]

 Some types may implement more than one of the above programming model. In such cases a programming model is chosen based on its priority as given by the above list. For example, Hashtable is a collection class but also implements ISerializable and it is serialized as a collection type. DataSet implements both IXmlSerializable and ISerializable and it is serialized as IXmlSerializable.


 


Here is a DataContract class that no one will ever see in real world. Figuring out which programming model is implemented by these types is left as an exercise to the readers.


 


[DataContract]


public class ComplexDataContract


{


    [DataMember]


    string name;


    [DataMember]


    System.Version version;


    [DataMember]


    System.IO.FileInfo fileInfo;


    [DataMember]


    System.Collections.Generic.List<System.Uri> uriList;


    [DataMember]


    System.Data.SqlTypes.SqlInt32 sqlInt;


    [DataMember]


    System.Drawing.KnownColor knownColor;


}

Comments (21)

  1. Awesome (and short) post by Sowmy Srinivasan on the serialization model in WCF (formerly known as &quot;Indigo&quot;)…

  2. Thanks to everyone that attended yesterday’s Atlanta Code Camp 2006 session on &quot;What’s New in ASMX 2.0&quot;.&amp;nbsp;…

  3. I left one thing unsaid in the serialization rules&amp;nbsp;and&amp;nbsp;Aaron’s sharp eyes caught it promptly.&amp;nbsp;As…

  4. The following links to .NET resources have been collated over time with the assistance of

    colleagues.&amp;nbsp;…

  5. Sowmy Srinivasan has a great post on various serialization options in WCF that covers some interesting…

  6. One of WCF&amp;#39;s goals is interoperability with standard protocol stacks, like WSE or other WS-* implementations.

  7. One of WCF’s goals is interoperability with standard protocol stacks, like WSE or other WS-* implementations….

  8. As we all know, IDictionaries aren&amp;#39;t serializable. This is has been a cause of much concern and consternation

  9. ranjitjc says:

    I Created a WCF Service with default serialization. But when i put the DataContract object into ViewState, it gives me an error as "…is not marked as serializable"

    do i need to serialize expliciltly or WCF has any Attribute ?

    My Samples is as follows:

    I. WCF Service

    [ServiceContract]

    public interface IUser

    {

          [OperationContract]

          User FindUserByID(int userID);

    }

    [DataContract]

    public class User

    {

        [DataMember]

        public int UserID;

        [DataMember]

        public string UserName;

    }

    public class UserService : IUser

    {

           public User FindUserByID(int userID)

           {

                 User user = new User();

                 user.UserId =100;

                 userName ="Ranjit";

           }

    }

    II. Web Application

    public partial class TestUserService : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

       {

           if (!IsPostBack)

           {

               try

               {

                   Proxy.UserService UserProxy = new Proxy.UserService();

                   Proxy.User user = UserProxy.FindUserById(100);

                   

                   ViewState.Add("User", user);

               }

               catch (Exception ex)

               {

                   Response.Write("Exception : " + ex.ToString());

               }

           }

       }

  10. Just a quick note for everyone doing first steps in WCF service contract design.Usually you get introduced…

  11. WCF doesn&#39;t always serialize custom collections of objects the way that you would like. For example,

  12. In the second part of our ongoing examination / diary of WCF we look at the new DataContract attribute vs using the traditional Serializable attribute.

  13. PoojaPuri says:

    It was a useful article to know what can be serialised. I have a WCF service where one of the Operationcontract has a XMLDocument  parameter. I guess i cannot make it a DataContract as it cannot be serialized. Any suggestions as to how i can pass a XMLDocument as a parameter.

    Any help would be appricaited.

  14. Robin Anil says:

    Why is DataMember Attribute not working for System.Type

  15. jeffrey25 says:

    pingback from geeks-squad.blogspot.com

    –Jeffrey

  16. I left one thing unsaid in the serialization rules and Aaron’s sharp eyes caught it promptly. As he mentioned

  17. I am curious if POCO support for data contracts in .NET 3.5 SP1 affected coding guidelines. In brief

Skip to main content