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.
To create a service contract you define an interface with related methods representative of a collection of service operations, and then decorate the interface/class with the ServiceContract Attribute to indicate it is a service contract. Methods in the interface that should be included in the service contract are decorated with the OperationContract Attribute.
How to define a Service Contract
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.