Basics of DataContract



DataContract:

DataContract is 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 think about DataContract with a different passion. They say DataContract is used for communicating with a client and service. Do you think that answer is correct? The answer is partially correct, because in .Net serialization and deserialization will happen quickly and that's why we use DataContract to interact with Client and Service.

The Syntax of DataContract is shown in the following figure

data1.gif

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.

The following figure shows how the .Net implementation is used in a XML scheme

data2.gif

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

The following Table shows DataContract Properties

DataContract Property Description
Namespace It is used to get or set the namespace for the DataContract attribute of this type.
Name It is used to get or set the name for DataContract attribute of this type.

The following table shows DataMember properties
DataMember Property Description
Order It is used to get or set the order of serialization and deserialization of a member.
ISRequired It instructs the serialization engine that member must be present while reading or deserializing.
Name It gets or sets the DataMember name.
EmitDefaultValue 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

Step1:

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

Step2:

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.

Step3:

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

data3.gif


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.

data4.gif

In this example, I am using the return type as void and sending the parameter of GeneralInformation as Info.

Let's use the interface IService in Service class.

data5.gif

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

Now in program class.

data6.gif

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)

data7.gif


Step4:

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

Step5:

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

data8.gif

Before entering into command prompt make 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.

Step6:

Now let's write a client code.

data9.gif

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.

erver'>
Up Next
    Ebook Download
    View all
    Learn
    View all