---
description: Windows Communication Foundation
---

# WCF

{% embed url="https://learn.microsoft.com/en-us/dotnet/framework/wcf/" %}

A unified programming model (framework) for building service-oriented applications. WCF is used to send data as asynchronous messages from one service endpoint to another.

## WCF Architecture

{% embed url="https://learn.microsoft.com/en-us/dotnet/framework/wcf/media/wcf-architecture.gif" %}
WCF Architecture
{% endembed %}

### Contracts

Contracts define various aspects of the message system.&#x20;

* The **data contract** describes every parameter that makes up every message that a service can create or consume. The message parameters are defined by XML Schema definition language (XSD) documents, enabling any system that understands XML to process the documents.&#x20;
* The **message contract** defines specific message parts using SOAP protocols, and allows finer-grained control over parts of the message, when interoperability demands such precision.&#x20;
* The **service contract** specifies the actual method signatures of the service, and is distributed as an interface in one of the supported programming languages.
* **Policies and bindings** stipulate the conditions required to communicate with a service. \
  For example, the binding must (at a minimum) specify the transport used (for example, HTTP or TCP), and an encoding.

### Service runtime

The service runtime layer contains the behaviors that occur only during the actual operation of the service, that is, the runtime behaviors of the service.&#x20;

Service runtime layer is primarily related to the processing the contents of message bodies.

* **Throttling** controls how many messages are processed, which can be varied if the demand for the service grows to a preset limit.&#x20;
* An **error behavior** specifies what occurs when an internal error occurs on the service. \
  For example, by controlling what information is communicated to the client. (Too much information can give a malicious user an advantage in mounting an attack.)&#x20;
* **Metadata behavior** governs how and whether metadata is made available to the outside world.&#x20;
* **Instance behavior** specifies how many instances of the service can be run.\
  For example, a singleton specifies only one instance to process all messages.&#x20;
* **Message inspection** is the facility to inspect parts of a message.&#x20;
* **Transaction behavior** enables the rollback of transacted operations if a failure occurs.&#x20;
* **Dispatch behavior** is the control of how a message is processed by the WCF infrastructure.
* **Parameter filtering** enables preset actions to occur based on filters acting on message headers.

### Messaging

The messaging layer is composed of _channels_. \
A channel is a component that processes a message in some way, for example, by authenticating a message. A set of channels is also known as a _channel stack_.&#x20;

Channels operate on messages and message headers.

There are two types of channels: transport channels and protocol channels.

* **Transport channels** read and write messages from the network (or some other communication point with the outside world). Some transports use an encoder to convert messages (which are represented as XML Infosets) to and from the byte stream representation used by the network. \
  Examples of transports are HTTP, named pipes, TCP, and MSMQ. \
  Examples of encodings are XML and optimized binary.

The **encoders** present a variety of encodings that can be used to suit the needs of the message. The **HTTP** channel specifies that the HyperText Transport Protocol is used for message delivery. The **TCP** channel similarly specifies the TCP protocol. The **Transaction Flow** channel governs transacted message patterns. The **Named Pipe** channel enables interprocess communication. The **MSMQ** channel enables interoperation with MSMQ applications.

* **Protocol channels** implement message processing protocols, often by reading or writing additional headers to the message. \
  Examples of such protocols include WS-Security and WS-Reliability.

**WS-Security** is an implementation of the WS-Security specification enabling security at the message layer. The **WS-Reliable Messaging** channel enables the guarantee of message delivery.

### Activation and Hosting <a href="#hosting-and-activation" id="hosting-and-activation"></a>

Services can also be _hosted_, or run in an executable managed by an external agent, such as IIS or Windows Activation Service (WAS).&#x20;

* **WAS** enables WCF applications to be activated automatically when deployed on a computer running WAS.&#x20;
* Services can also be manually run as executables (**.exe** files).&#x20;
* A service can also be run automatically as a **Windows service**.
* **COM+** components can also be hosted as WCF services.
