WCF - Service Contract


 WCF - Service Contract   

  • Service contracts informs about the operational tasks of that particular service. 
  • For instance, a Service contract is a service that can provide latitude and longitude based on the city name. 
  • A Service can contain one or more service contracts.
  • A service must surely have one service contract to name it as a service. 
  • Services are created with Service and Operation Contract attributes. 
  • [ServiceContract] attribute is equal to [WebServcie] attribute given to a class in the WebService.
  • [OpeartionContract] attribute is equal to the [WebMethod] attribute given to a method in WebService.
  • The client which access the exposed service, sends a message via a proxy through the communication channel established between the service and the client. 
  • The request message from client is transformed into binary data and sent to the server as shown below.

Message exchange between a Client and a Service

Service exposed via endpoints

Sample Code :

[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

More on Service Contract :

  • Records the interface and methods of the service through platform-independent narration. 
  • Informs about the message exchange patterns of a service as either it is one-way or two-way with a request-reply methodology.

Code Explanation :


  The WCF Library application automatically creates a Service Interface IService1.cs file on opting a new project.

  The file IService1.cs contains an interface service indicated by the [ServiceContract] attribute.

 The operations exposed by the service are presented with the  [OperationContract] attribute.