DataContract will come 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.
DataContract will come under namespace
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
thought about DataContract in a different passion. They use to say DataContract
is used for communicating with a client and service. Do you think answer is
correct? Partially answer is correct, because in .net serialization and
deserialization will happen fast that’s why we use DataContract to interact
with Client and Service.
The Syntax of DataContract is Shown in a
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.
Below figure shows how .net implementation
to be used in XML scheme
Now it’s time to understand the properties
DataContract and DataMember attribute.
Below Table shows DataContract Properties
It is used to get or set the namespace for the
DataContract attribute of this type
It is used to get or set the name for DataContract
attribute of this type.
Below table shows DataMember properties
It is used to get or set the order of serialization
and deserialization of a member
It instructs the serialization engine that member
must be present while reading or deserializing.
It gets or sets the DataMember name
It will specify whether the default value to be
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
Open VS (Visual Studio) ->Add New Project->Create Console
Application->name it WCFService.
Add reference to System.ServiceModel (WCF stuff) and System.Runtime.Serialization
Add a new interface called
IService, a class called Service and a class called GeneralInformation.
Now let’s write some piece of
code in GeneralInformation class which contains properties like FirstName,
LastName, Email and Phone.
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
Now, let’s write some simple lines of code
in IService interface.
In this Service class, I am just printing
the value which is coming as input.
Now in program class.
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
Now create one more Console Application->name it as WCFClient->Add
Reference to System.ServiceModel and System.Runtime.Serialization.
Now we will create a proxy by
using a tool called svcutil. Open VS (visual Studio) command prompt and type
entering into command prompt makes 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.
Now let’s write a client code.
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.
I highly suggest using EmitDefaultValue as false in DataContract attribute
which is true by default. When EmitDefaultvalue is true then properties within
DataContract class will be Serialize and Deserialize for all the properties.
Now when you set EmitDefaultValue as false then only those properties are
serialized and Deserialized which are in use.
For Example: Consider above DataContract
Class. Suppose if I pass only FirstName and LastName then only these properties
are serialized & Deserialized.
By using EmitDefaultValue=”false”, the
performance of the application will increase up to 30%.
I hope you people like this article.