blogspot visit counter

Friday, 10 May 2013

Data Contracts and Service Contracts (WCF)

Data Contracts and Service Contracts (WCF)
Introduction : -

A WCF Service is comprised of the following major components. The diagram below shows how the components are related to each other:
  • Service Contract
  • Operation Contract
  • Data Contract
  • Data Member


ServiceContract

A ServiceContract is a method that is exposed in a WCF Service, which a client calls to execute the service. The ServiceContract almost always has a return value exposed to the client to check if it was successful, or to see if there are any results. A ServiceContract uses Data Contracts to transfer data to and from the client. It can also use primitive data types to transfer data.



// Student ServiceContract
[ServiceContract]
public interface IStudentService
{
// Define the OperationContact here….
}

Operation Contract

An operation contract defines the methods of the service that are accessible by external systems. The OperationContract attribute needs to be applied for all these methods, these are also like web methods in a web service. Operation contracts are defined as follows:




// Student ServiceContract
[ServiceContract]
public interface IStudentService
{
//Define the GetStudentFullName OperationContact here
[OperationContract]
String GetStudentFullName (int studentId); 
 
//Define the GetStudentInfo OperationContact here….
[OperationContract]
StudentInformation GetStudentInfo (int studentId);

}


Data Contract

A DataContract is a formal agreement between a service and a client on how data will be exchanged. It also describes how data is to be serialized. A DataContract is used when complex data types are needed to be exchanged between the server and client. A class has to be marked with the DataContract attribute for it to be serialized and exposed in the WCF Service. Any class that does not have the DataContract attribute is not accessible in the WCF Service.
Data Contracts are used by Service Contracts like primitive data types. DataContract objects can be passed as parameters for Service Contracts or returned as values for the Service Contracts.
For more Example you can refer to the article: Using Data Contracts




[DataContract]
public class StudentInformation
{
// Define the Datamembers here….
}


Data  Member

A data member specifies the type which is part of a data contract used as a composite type member of the contract. To define a data member, apply the DataMember attribute to the fields that must be serialized. The DataMember attribute can be applied to private properties, but they will be serialized and deserialized, and will be accessible to the user or process. The code below shows how to define a data member in a data contract:



[DataContract]
public class StudentInformation
{
_studentId = studId;

[DataMember]
public int StudentId
{
get { return _studentId; }
set { _studentId = value; }
}

}


Creating WCF Applications with Visual Studio 2010

1 comment:

Related Posts Plugin for WordPress, Blogger...