Understand WCF: Part 9: Data Contracts in WCF

This is the Understand WCF article series. It is all about WCF applications, we have covered a few very important topics in this series, you can get them here:
This presentation explains Data Contracts in WCF applications. It is one more fundamental concept of WCF applications.
 

What is Data Contract?

 
Generally speaking, a Data Contract is nothing but a class definition of a WCF application. This is the conceptual agreement of the format and structure of data that will be exchanged between client and server. The data will be passed in the form of SOAP messages.
 
Let's try to understand Data Contracts with a realasitic example.
 
Create WCF application
 
Here is our example code form of a Service Contract (Interface) and we have implemented a Data Contract just below the interface definition. As we said earlier, the Data Contract is nothing but a simple class and here we have defined a Person class with name and surname data members.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.    
  9. namespace TestServive  
  10. {  
  11.     [ServiceContract]  
  12.     public interface IService1  
  13.     {  
  14.         [OperationContract]  
  15.         string AddName(Person obj);  
  16.     }   
  17.     //Create data contract  
  18.     [DataContract]  
  19.     public class Person  
  20.     {  
  21.         [DataMember]  
  22.         public string name;  
  23.         [DataMember]  
  24.         public string surname;  
  25.     }  
  26. }  
Define the class and implement an interface for it.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.    
  9. namespace TestServive  
  10. {  
  11.     public class Service1 : IService1  
  12.     {  
  13.         public string AddName(Person obj)  
  14.         {  
  15.             return obj.name + obj.surname;  
  16.         }  
  17.     }  
  18. }  
So, we have set up the service and now we need to consume it by the client. So, create a console application and provide the reference.
 
Create console client
 
Create one console application and a reference to it. Once you go to the Add reference option you will encounter the following window that will show the services available in this application.
 
services in wcf application 
 
Press the "OK" button and the reference will added to the solution. You can find the following screen in the solution.
 
add reference in wcf application 
 
Modify the client code as in the following:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. using System.Text;  
  6. using System.Diagnostics;  
  7. using System.Collections;  
  8. using Client;  
  9. using System.ServiceModel;  
  10. using System.ServiceModel.Description;  
  11. using Client.ServiceReference1;  
  12.    
  13. namespace Client  
  14. {  
  15.     class Program  
  16.     {  
  17.         static void Main(string[] args)  
  18.         {  
  19.             Person obj = new Person();  
  20.             obj.name = "Sourav";  
  21.             obj.surname = "Kayal";              
  22.             Service1Client objclient = new Service1Client();  
  23.             string FullName = objclient.AddName(obj);  
  24.             Console.WriteLine(FullName);  
  25.             Console.ReadLine();  
  26.         }  
  27.     }  
  28. }  
We can see that we are sending one object of the person class to the service method and the service will return the addition of name and surname.
 

Why Data Contract?

 
The question may occur to you, "what are the exact uses of a Data Contract?". We can find that in the client part we have created an object of the person class and set values into it. This is possible because we have defined the person class as a Data Contract.
 
Here is sample output
 
wcf Data Contract 
 
Conclusion
 
In this article, we have learned the concept of Data Contracts and their use in applications. Hope you have understood the concepts. In a future article, we will explain various attributes of Data Contracts. Happy learning.
 


Similar Articles