WCF provides a runtime environment for developing distributed services that are interoperable, secure, and reliable. A service is a reusable unit of functionality that performs a particular task.
A WCF Service exposes Web Service Definition Language (WSDL) file contains the available functionality and the communication methods supported by the service. A client can import this WSDL file. After reading this file, client knows all the available functionality and communication methods supported by the service. When client send message to the service, it converts the message to SOAP format and send it to service. When service receives the SOAP file, it parses the SOAP file and knows the operation the client wants to perform on the service. After performing the operation, service convert the response into SOAP format and return back to the client.
A service must be hosted in some place. WCF supports IIS, WAS, and Self-Hosting. A client must know the endpoint of the service to communicate with the service. An endpoint is a combination of Address, Binding and Contract.
ABC of WCF
A stands for Address, B stands for Binding, and C stands for Contract.
- Address: Address identifies the service address.
- Binding: Binding specifies how to communicate with the service.
- Contract: Contract list the operations supported by the service.
WCF Architecture consists of five layers: Application, Contracts, Service Runtime, Messaging, Activation and hosting.
The Contracts Layer defines a list of agreements between a client and a service.
Service Contract lists the operations and its signatures supported by the service. Service contract also has operation contract which describes aspect of the operation whether the operation support request-reply or one-way pattern, async pattern.
Data Contract defines the data types that a service can consume or return to the client. Data contract includes all parameters or return type of all the operations of the service.
Message Contract gives complete control over the structure of the SOAP message. Message contract is used in interoperability cases.
Policies and Bindings define the environment required to communicate with the service. Policies are the guidelines for implementing the service. For example, if policy defines message security should be used in the service, then message security must be used in a service. Bindings are used to define the communication requirements that must be met to connect to the service. For example, Binding must specify the transport protocol and encoding mechanism used in a service.
The Service Runtime is composed of set of behaviors that define the runtime operation of the service.
Throttling Behavior manages the messages load on the service. Throttling controls the maximum number of concurrent sessions, maximum number of concurrent calls, and the maximum number of concurrent instances. If number of messages are exceed your configured limit, this behavior places the pending messages in a queue.
Error Behavior manages the error information send to the client. For example, if any internal error occurs during the processing the operation of service, will the runtime send the full error message and stack trace send to client or return any custom exception?
Metadata Behavior manages the metadata of the service. It decides whether metadata is available to the client or not?
Instance Behavior manages the number of service instances available for handling client requests. It determines whether service will use existing instance or create new instance of the service for client request. For example, if the service is configured with PerCall instance mode, service will create new instance for each client request. If the service is configured with Single instance mode, then only one single instance is used for all client requests.
Message Inspection behavior manages the message inspectors. A Message inspector can inspect and change messages before receiving or sending the request.
Transaction Behavior manages the transaction flow for the service operation. It specifies whether transaction is attached to the service operation or not. If any operation fail in the transaction, then this behavior try to rollback the transacted operation of the service.
Concurrency Behavior controls the number of threads active at one time in the service operation. For example, if service is configured with Single concurrency mode, service disallows the concurrent calls.
Parameter Filtering behavior apply filters on the parameters of operations. For example, you can validate the parameters passed to a method before or after the call.
Dispatch Behaviors are responsible for message encoding, filtering, operation selection and operation invocation on the client and the service side.
The messaging layer concerned about sending the message from the client to the service. Client and server both have components that process a message. These components are called channels. A combination of one and more channels is called channel stack. Channels works on messages and message headers.
Channels are of two types:
- Transport Channel
- Protocol Channel
Transport Channel works at the network level. It read and writes messages from the network. Some transports are HTTP, TCP, Named Pipe, and MSMQ. These channels serialize the outgoing message and passed it to the network. On the incoming side, it de-serializes the incoming message and passes it to the protocol channel.
Protocol Channel read and writes additional headers to the message.
- WS-Security channel provides security by adding additional headers to message.
- WS-Reliable Messaging channel allows messages by guarantee and in order.
- Encoders provide multiple encodings formats like binary, MTOM, Text.
- HTTP channel indicates that the HTTP protocol is used for message delivery.
- TCP channel indicates that the TCP protocol is used for message delivery.
- Transaction Flow channel enables transacted messages patterns.
- Named Pipe channel indicates inter-process communication.
- MSMQ channel enables interoperation with MSMQ applications.
Hosting and Activation
A service must be hosted in some process. WCF runtime provides four options for activation and hosting of services.
- Windows Activation Service (WAS) is a system service that automatically activates WCF service when client request the service and WAS can host services on multiple transport protocols. It also provides application pooling, recycling, idle time management, identity management, and isolation.
- .EXE: A service can be self-hosted means developer is responsible for starting and stopping the service.
- Window Services: Window service is a system service that runs in the background and a WCF service can be hosted as a Windows Service.
- COM+: WCF runtime provides an option to host COM+ components as a WCF Service. COM+ is an extension of COM technology and provides transactional components, queue components, role-based security, application pooling, SOAP services and object pooling features.