ARTICLE

WCF - Data Contract - Day 4

Posted by Akshay Patel Articles | WCF with C# October 02, 2012
This article explains about DataContract, DataContractAttribute and DataMemberAttribute. It also explains the properties of DataContractAttribute and DataMemberAttribute.
Reader Level:

Before reading this article, please go through the following articles:

  1. Day 1 - WCF Introduction and Contracts
  2. Day 2: WCF Fault Contacts
  3. Day 3: WCF Message Exchange Patterns

DataContract

Data contracts are the conceptual agreement of the format and structure of the data in the message exchanged between a service and client. It is a format of data passed to and from the service operations. It defines the structure and types of data exchanged in service messages.

DataContractAttribute

The DataContractAttribute is defined in the System.Runtime.Serialization namespace. It is used to declare a type as a DataContract.

Properties of DataContractAttribute

DataContractAttribute-in-wcf.gif

  • Name : It determines the type name as it is generated in the resulting schema.
  • Namespace : It sets the target namespace of the schema.

DataMemberAttribute
 
The DataMemberAttribute is also the part of the System.Runtime.Serialization namespace. It is applied to the members and it is used to declare that the members should be included in the serialization.

Properties of DataMemberAttribute

DataContractAttribute-in-wcf1.gif

  • EmitDefaultValue : If you want to add default values in the serialization then set the EmitDefaultValue to true else EmitDefaultValue to false. By default it is true.
  • IsRequired : It controls the minOccurs attribute for the schema element. The default value is false.
  • Name : It creates the schema element name generated for the member.
  • Order : It maintains the order of each element in the schema. By default it appears alphabetically.

Note

  • For example you set the EmitDefaultValue to false and IsRequired to true and you are not assigning any value, at that time it creates a problem. Because the serializer emits the default value and we are not passing any value, on the other side it is required. That is why it throws an error.
  • If you apply a data member attribute to both property and field, then it generates duplicate members in the schema.

Sample Code

  • Create one DataContract for Place. Add the data members PlaceCode and PlaceName.
  • Set the order for the data members and set IsRequired to true for PlaceCode.
  • Create one OperationContract with return type Place.

    namespace DataContractServiceApp
    {
        [ServiceContract]
        public interface
    IService1
        {
            [OperationContract]
            Place GetPlace(string PlaceCode);
        }
     
        [DataContract(Name ="Place", Namespace ="")]
        public class
    Place
        {
            [DataMember(Name ="PlaceCode", Order = 1, IsRequired=true)]
            public string PlaceCode;
            [DataMember(Name ="PlaceName", Order = 2)]
            public string PlaceName;
        }
    }
     
  • In the service class, the GetPlace method is implemented and in that we create an object of place, assign some values to it and return a Place object.

namespace DataContractServiceApp

{

    public class Service1 : IService1

    {

        public Place GetPlace(string PlaceCode)

        {

            Place place = new Place();

           

            // Get placename by placecode from database.

           

            return place;

        }

    }

}



Recommended Readings

Before reading this article, please go through the following articles:

Login to add your contents and source code to this article
post comment
     

@Gaurav - Technically, no. This is dependent on whether you use the DataContractSerializer or not. But you should use.

Posted by Akshay Patel Oct 03, 2012

nice explanation....

Posted by Swati Agarwal Oct 03, 2012

very knowledgible... looking forward to see some more......

Posted by Gaurav Chauhan Oct 03, 2012

Is it neccessary to use DataContractAttribute when we can use DataContract without it?

Posted by Gaurav Gupta Oct 03, 2012

Thanks for this article.. very knowledgeable...

Posted by Varesh Tuli Oct 03, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.