WCF: Basics of DataContract

Posted by in WCF category on for Beginner level | Points: 250 | Views : 65215 red flag
Rating: 5 out of 5  
 3 vote(s)

DataContract will come under namespace System.Runtime.Serialization. During design time, DataContract Attribute is used to indicate which class should be represented as XSD. DataMember Attribute indicates which class member to be used in external representation.


DataContract will come under namespace System.Runtime.Serialization.  During design time, DataContract Attribute is used to indicate which class should be represented as XSD. DataMember Attribute indicates which class member to be used in external representation.

During my discussion with many people, they thought about DataContract in a different passion. They use to say DataContract is used for communicating with a client and service. Do you think answer is correct? Partially answer is correct, because in .net serialization and deserialization will happen fast that’s why we use DataContract to interact with Client and Service.   

The Syntax of DataContract is Shown in a below figure

By looking at the figure, we can define DataContract attribute to Class, Struct and Enum. DataContract attribute contains two get and set properties as Name and Namespace.

Below figure shows how .net implementation to be used in XML scheme

Now it’s time to understand the properties DataContract and DataMember attribute.

Below Table shows DataContract Properties

DataContract Property



It is used to get or set the namespace for the DataContract attribute of this type


It is used to get or set the name for DataContract attribute of this type.

Below table shows DataMember properties

DataMember Property



It is used to get or set the order of serialization and deserialization of a member


It instructs the serialization engine that member must be present while reading or deserializing.


It gets or sets the DataMember name


It will specify whether the default value to be serialized.

Now, we understood about DataContract attribute theoretically. Now the time has come to use this attribute.

Let’s go through this DataContract demo step by step


             Open VS (Visual Studio) ->Add New Project->Create Console Application->name it WCFService.


             Add reference to System.ServiceModel (WCF stuff) and System.Runtime.Serialization (DataContract).

              Add a new interface called IService, a class called Service and a class called GeneralInformation.  


              Now let’s write some piece of code in GeneralInformation class which contains properties like FirstName, LastName, Email and Phone.

Here I set the DataContract attribute with namespace (DataContractDemo.DataContract.com) and to the FirstName I set ISRequired to true. Here in this example FirstName field is mandatory. If I don’t give any value then WCF service will throw an error.

Now, let’s write some simple lines of code in IService interface.

In this Service class, I am just printing the value which is coming as input.
Now in program class.

 We are using this program class to host the application. Here we are using a hosting mechanism called self-hosting. 

Now let’s see the configuration (app.config)


             Now create one more Console Application->name it as WCFClient->Add Reference to System.ServiceModel and System.Runtime.Serialization.


               Now we will create a proxy by using a tool called svcutil. Open VS (visual Studio) command prompt and type this line

 Before entering into command prompt makes sure that your service is running (I mean WCFService). In C drive, you will find proxy.cs and app.config files. Copy those files and paste it in WCFClient project.


                 Now let’s write a client code.

Here I am just creating a proxy and entering some information like FirstName, LastName & So on and calling a method called Display which will print the information.

 Note: I highly suggest using EmitDefaultValue as false in DataContract attribute which is true by default. When EmitDefaultvalue is true then properties within DataContract class will be Serialize and Deserialize for all the properties. Now when you set EmitDefaultValue as false then only those properties are serialized and Deserialized which are in use.

For Example: Consider above DataContract Class. Suppose if I pass only FirstName and LastName then only these properties are serialized & Deserialized.   

By using EmitDefaultValue=”false”, the performance of the application will increase up to 30%.

I hope you people like this article.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: anup hosur
Member Level:
Member Status: Member
Member Since: 8/1/2009 2:37:23 AM
Country: India
He is the .net developer with around 8 years of experience in technologies like c#, WCF, WPF and Asp.net. https://wordpress.com/pages/anuphosur.wordpress.com

Login to vote for this post.

Comments or Responses

Posted by: Chvrsri on: 12/8/2010 | Points: 25
If u can share your code set, it would help many community members.
Posted by: Bokbob on: 12/10/2010 | Points: 25

when i do svcutil /out:Proxy.cs /config:app.config it gives me error like **the file name, directory name, or volume label syantax is incorrect***

please help me with this.

Posted by: Anup1252000 on: 12/12/2010 | Points: 25
Hi Bokbob,

you need to specify base address when using svcutil tool( svcutil /out:Proxy.cs /config:app.config baseaddressuri). In my case baseaddressuri is http://localhost:2222/service.
Posted by: Anup1252000 on: 12/12/2010 | Points: 25
Hi Chvsri,

I don't think source code is required for this topic. If you needed it very badly then i will add no issues.
Posted by: Anup1252000 on: 12/12/2010 | Points: 25
You can do one more thing. Run your service and in the client application right click->add service reference.
Posted by: Bokbob on: 12/13/2010 | Points: 25
one thing i know is that my service name is sample service with a space between the sample and service. do you think that could have messed up ?

Posted by: Bokbob on: 12/13/2010 | Points: 25
thank you for responding, i appriciate your help.

and do i need to have my svcutil file in C:\\ (at root ?)before i specify that in comand prompt ?

Posted by: Syedshakeer on: 11/26/2011 | Points: 25
Your article on DataMebers is fine.Can you update more code where you define the class GenerelInformation .

Posted by: Vijayakumarp on: 7/13/2012 | Points: 25
Nice article brother....

Vijayakumar Pa

Login to post response

Comment using Facebook(Author doesn't get notification)