Use the Service Interface pattern to describe the behavior of a service and the messages required to interact with that service. The service interface does the following:
- It describes the operations that your service provides that a client can use.
- It describes the format for information being passed to and from operations.
- It describes the message exchange patterns that the service implements, such as request/reply, one-way, and duplex.
Design your application as a collection of software services, each with a service interface through which consumers of the application may interact with the service.
A software service is a discrete unit of application logic that exposes a message-based interface that is suitable for being accessed by other applications. Each software service has an associated interface that it presents to the consumers. This interface defines and implements a contract between the consumers of the service and the provider of the service. This contract and its associated implementation are referred to as a service interface.
Figure 1 illustrates a service gateway consuming a service provided by a service interface. The collaboration between these two elements is governed by a contract.
Figure 1
Service elements
As Figure 1 shows, the service interface provides an entry point that consumers use to access the functionality exposed by the application. The service interface is usually network addressable, meaning that it is capable of being accessed by the consumer over some sort of communication network. The network address can be a well-known location or it can be obtained from a service directory, such as Universal Discovery Description Integration (UDDI).
A key aspect of the design of a service interface is to decouple the implementation needed to communicate with other systems from the application's business logic. The service interface provides a coarse-grained interface and preserves the semantics and finer granularity of the application logic. It also provides a barrier that enables the application logic to change without affecting the consumers of the interface.
The service interface implements the contract between the consumer and provider. This contract allows them to exchange information even if they are on different systems. The service interface is responsible for all of the implementation details needed to perform this communication. Such details include, but are not limited to, the following:
- Network protocol. The service interface should encapsulate all aspects of the network protocol used for communication between the consumer and service. For example, suppose that a service is exposed to consumers through HTTP over a TCP/IP network. You can implement the service interface as an ASP.NET component published to a well-known URL. The ASP.NET component receives the HTTP request, extracts the information needed by the service to process the request, invokes the service implementation, packages the service response, and sends the response back to the consumer as an HTTP response. From the service perspective, the only component that understands HTTP is the service interface. The service implementation has its own contract with the service interface and should have no dependencies on the specifics of the technology that consumers use to communicate with the service interface.
- Security. The service interface should be considered its own trust boundary. Consumers of the interface may need to be authenticated by the service and roles associated with consumers can be used to control access to different operations exposed by the service.
- Service level agreements. The service interface has a significant role in ensuring that the service meets its service level commitments to a specific set of consumers. Service interfaces may implement caching to increase response time and reduce bandwidth consumption. Multiple instances of a service interface may be deployed across a load-balanced set of processing nodes to achieve scalability, availability, and fault-tolerance requirements.