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:
- Understand WCF: Part 1: What is SOA, Services and Messages
- Understand WCF: Part 2: Understand RESTful Service
- Understand WCF: Part 3: Create simple WCF Service
- Understand WCF: Part 4: What is Address, Binding, and Contract
- Understand WCF: Part 5: Exception handling in WCF
- Understand WCF: Part 6: Function overloading in WCF application
- Understand WCF: Part 7: Self Hosting in WCF application
- Understand WCF: Part 8: Observe SOAP message in WCF communication
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace TestServive
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- string AddName(Person obj);
- }
- //Create data contract
- [DataContract]
- public class Person
- {
- [DataMember]
- public string name;
- [DataMember]
- public string surname;
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace TestServive
- {
- public class Service1 : IService1
- {
- public string AddName(Person obj)
- {
- return obj.name + obj.surname;
- }
- }
- }
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.
Press the "OK" button and the reference will added to the solution. You can find the following screen in the solution.
Modify the client code as in the following:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.Collections;
- using Client;
- using System.ServiceModel;
- using System.ServiceModel.Description;
- using Client.ServiceReference1;
- namespace Client
- {
- class Program
- {
- static void Main(string[] args)
- {
- Person obj = new Person();
- obj.name = "Sourav";
- obj.surname = "Kayal";
- Service1Client objclient = new Service1Client();
- string FullName = objclient.AddName(obj);
- Console.WriteLine(FullName);
- Console.ReadLine();
- }
- }
- }
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
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.

Pankajkumar PatelPosted Sep 11, 2019, 11:49 PM
Nice article