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.
- 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);
- }
-
- [DataContract]
- public class Person
- {
- [DataMember]
- public string name;
- [DataMember]
- public string surname;
- }
- }
Define the class and implement an interface for it.
- 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;
- }
- }
- }
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.
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();
- }
- }
- }
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
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.