Friday, September 23, 2011

Service Contract in WCF

Service contract means the collective mechanisms by which a service’s capabilities and requirements are specified for its consumers. We must say that it define the operations that a service will perform when executed. It tells more thing about service like message data types, operation locations, the protocols the client will need in order to communicate with the service.
There are three types of attributes which are used to annotate these type of operations.
1. ServiceContractAttribute
2. OperationContractAttribute
3. MessageParameterAttribute.

ServiceContractAttribute :
It is used to declare the type as a Service Contract. It can be declared without any parameters but it can also take named parameters.

[ServiceContract(Name="MyService", Namespace="http://tempuri.org")]
public interface IMyService
{

[OperationContract]
int AddNum(string numdesc, string assignedTo);
}

OperationContractAttribute: It can be applied only on methods. It is used to declare methods which belongs to Service Contract. It controls over the service description and message formats.

MessageParameterAttribute.: It controls how the names of any operation parameters and return values appear in the service description. It controls how both the parameter and return values are serialized to XML request and response elements at the transport layer. We need to use the Name property because the variable name as it can’t be used as programming language.

[OperationContract]
[return : MessageParameter(Name="reswait")]
string MyOp([MessageParameter(Name="string")]string s);