A fine site


Leave a comment

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.


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

Properties of DataContractAttribute

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


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

  • 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.


  • 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
    public interface 
    Place GetPlace(string PlaceCode);

    [DataContract(Name =”Place”, Namespace =””)]
    public classPlace
    [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;





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s