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.
- CLR built-in types
- Byte array, DateTime, TimeSpan, GUID, Uri, XmlQualifiedName, XmlElement and XmlNode array [This includes XElement and XNode array from .NET 3.5]
- Enums
- Types marked with DataContract or CollectionDataContract attribute
- Types that implement IXmlSerializable
- Arrays and Collection classes including List<T>, Dictionary<K,V> and Hashtable.
- Types marked with Serializable attribute including those that implement ISerializable.
- 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
Anonymous
May 10, 2006
Awesome (and short) post by Sowmy Srinivasan on the serialization model in WCF (formerly known as "Indigo")...Anonymous
May 14, 2006
Thanks to everyone that attended yesterday's Atlanta Code Camp 2006 session on "What's New in ASMX 2.0".&nbsp;...Anonymous
May 14, 2006
I left one thing unsaid in the serialization rules&nbsp;and&nbsp;Aaron's sharp eyes caught it promptly.&nbsp;As...Anonymous
May 16, 2006
The following links to .NET resources have been collated over time with the assistance of
colleagues.&nbsp;...Anonymous
June 12, 2006
Sowmy Srinivasan has a great post on various serialization options in WCF that covers some interesting...Anonymous
July 31, 2006
One of WCF&#39;s goals is interoperability with standard protocol stacks, like WSE or other WS-* implementations.Anonymous
July 31, 2006
One of WCF's goals is interoperability with standard protocol stacks, like WSE or other WS-* implementations....Anonymous
August 02, 2006
As we all know, IDictionaries aren&#39;t serializable. This is has been a cause of much concern and consternationAnonymous
August 20, 2006
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());
}
}
}Anonymous
September 11, 2006
Just a quick note for everyone doing first steps in WCF service contract design.Usually you get introduced...Anonymous
January 11, 2007
PingBack from http://www.wcfblog.com/2007/01/02/wcf-serialization-rules/Anonymous
January 19, 2007
WCF doesn't always serialize custom collections of objects the way that you would like. For example,Anonymous
February 20, 2007
In the second part of our ongoing examination / diary of WCF we look at the new DataContract attribute vs using the traditional Serializable attribute.Anonymous
May 31, 2007
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.Anonymous
December 19, 2007
Why is DataMember Attribute not working for System.TypeAnonymous
July 18, 2008
pingback from geeks-squad.blogspot.com --JeffreyAnonymous
August 27, 2008
PingBack from http://evacion.wordpress.com/2008/08/28/wcf-serialization-programming-model/Anonymous
October 04, 2008
I left one thing unsaid in the serialization rules and Aaron's sharp eyes caught it promptly. As he mentionedAnonymous
April 01, 2009
I am curious if POCO support for data contracts in .NET 3.5 SP1 affected coding guidelines. In brief