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;
- }
- }

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