MSDN Documentation

Windows Communication Foundation (WCF) Architecture

Windows Communication Foundation (WCF) is a unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transactional, and interoperable distributed applications that can be managed easily. WCF abstracts away many of the complexities of distributed programming, allowing developers to focus on business logic.

Conceptual Diagram of WCF Architecture Components

WCF Architecture Diagram

View detailed diagram

Core Concepts and Components

WCF is built upon several key abstractions and components that work together to facilitate communication between distributed applications.

1. Service Contracts

A service contract defines the operations that a service exposes to its clients. It's an interface that specifies what a service can do. This contract is crucial for enabling interoperability, as clients and services can be developed independently as long as they adhere to the contract.


[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    double Add(double a, double b);

    [OperationContract]
    double Subtract(double a, double b);
}
            

2. Data Contracts

Data contracts define the structure of the data that is exchanged between services and clients. They ensure that data can be serialized and deserialized across different platforms and languages.


[DataContract]
public class Order
{
    [DataMember]
    public int OrderId { get; set; }

    [DataMember]
    public string CustomerName { get; set; }

    [DataMember]
    public DateTime OrderDate { get; set; }
}
            

3. Service Implementation

This is the class that implements the service contract. It contains the actual business logic for each operation defined in the contract.


public class CalculatorService : ICalculator
{
    public double Add(double a, double b)
    {
        return a + b;
    }

    public double Subtract(double a, double b)
    {
        return a - b;
    }
}
            

4. Endpoints

An endpoint represents the address, binding, and contract that define how a client can communicate with a service. Each service can expose multiple endpoints.

5. Bindings

Bindings are crucial for configuring how messages are sent and received. WCF provides a set of standard bindings, and developers can also create custom bindings.

6. Hosting

Services need to be hosted to be accessible. WCF supports several hosting options:

Message Exchange Patterns

WCF supports various message exchange patterns:

Security and Reliability

WCF offers robust support for security and reliability through its binding configurations. This includes:

Extensibility

WCF is highly extensible. Developers can extend WCF by:

Example: Configuring a Service Endpoint (App.config)


<configuration>
  <system.serviceModel>
    <services>
      <service name="MyNamespace.MyService" behaviorConfiguration="mexBehavior">
        <endpoint address="net.tcp://localhost:8080/MyService"
                  binding="netTcpBinding"
                  contract="MyNamespace.IMyService" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
                

Understanding the WCF architecture is fundamental to designing and implementing robust, scalable, and interoperable distributed systems. The model provides a powerful yet flexible foundation for modern service-oriented development.