Thursday 26 May 2016

What is DataContact in WCF? Why use DataContract in WCF?



DataContract is a formal agreement between a service and client that abstractly describe the data to be exchanged.
DataContract are used to describe the data types used by service.
DataContract can be explicit or implicit.

It is not necessary to use DataContract if you are using simple/implicit data type i.e. int, string .
DataContract is necessary when you want to use complex data type.

Example:-
[DataContract]
public class CompositeType
{
          bool boolValue = true;
          string stringValue = "Hello ";

          [DataMember]
          public bool BoolValue
          {
                   get { return boolValue; }
                   set { boolValue = value; }
          }

          [DataMember]
          public string StringValue
          {
                   get { return stringValue; }
                   set { stringValue = value; }
          }
}

In the above example if you did not mention DataMember attribute on Property then it will throw error at compile time.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.