Service contract means the collective mechanisms by which a service's
capabilities and requirements are specified for its consumers. We must say that
it defines the operations that a service will perform when executed. It tells
more things about a 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.
- ServiceContractAttribute
- OperationContractAttribute
- 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 only be applied on methods. It is used
to declare methods which belong to a Service Contract. It controls 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 can't be used as programming language.
[OperationContract]
[return
: MessageParameter(Name="reswait")]
string MyOp([MessageParameter(Name="string")]string
s);
Simple Program:
[ServiceContract]
public interface IMathOp
{
[OperationContract]
double AddNum(double A, double B);
[OperationContract]
double Multiply (double A, double B);
}
// Implement the IMath contract in the MathService class.
public class MathService : IMathOp
{
public double AddNum (double A, double B) { return A + B; }
public double Multiply (double A, double B) { return A * B; }
}