Saturday, June 09, 2007

DataContract Vs. Service Contract

Hi All,
In WCF,Some new terminolgies are come up for .Net Developers,but not for COM,DCOM developers,Since the developers who are familiar with developing Dist. Application used to divide their code to interface and implementations.

The same concept is exist on WCF Tech.,Since the devleopers should divide his servce to :
1- Data 2- Operations.

**Data represented by creating DataContract which expose the data which will be transefered /consumend from the serive to its clients.

**Operations which is the functions provided by this service.

To write an operation on WCF,you have to write it as an interface,This interface contains the "Signature" of the methods tagged by ServiceContract attribute,and all methods signature will be impelemtned on this interface tagged with OperationContract attribute.

and to implement these serivce contract you have to create a class which implement the interface and the actual implementation will be on that class.

Code Below show How to create a Service Contract:

Public Interface IEmpOperations
Decimal Get EmpSal(int EmpId);


Class MyEmp: IEmpOperations
Decimal Get EmpSal()
// Implementation of this method.

Hope now you can differentiate between Data Contract,Service Contract and OperationContract.

Moustafa arafa


Sebastián Gómez said...

Hey dude! I Googled for DataContract vs. ServiceContract and this post came up first. I read it but you didn't post an example of a DataContract. I understand that the interface must be set as ServiceContract, but where in my code I need to write the DataContract signature?

Unknown said...

Hi Sebestian,

thanks for your question,you can write the DataContract Class on the same file along with your ServiceContract.

C# Code :
[DataContract(Name = "Customer", Namespace = "")]
class Person : IExtensibleDataObject
// To implement the IExtensibleDataObject interface, you must also
// implement the ExtensionData property.
private ExtensionDataObject extensionDataObjectValue;
public ExtensionDataObject ExtensionData
return extensionDataObjectValue;
extensionDataObjectValue = value;

[DataMember(Name = "CustName")]
internal string Name;

[DataMember(Name = "CustID")]
internal int ID;

public Person(string newName, int newID)
Name = newName;
ID = newID;


Anonymous said...

Here's a beginners question:

How would I consume (display) the DataContract in say a aspx form?


kunal said...

Question: Does datacontract have public method which can be exposed to service.

Unknown said...

Data Contract only contains data that needs to be transmitted over.

Service Contract contains the methods that will exposed by the service.