MSDN Documentation

Microsoft Developer Network

Introduction to Windows Communication Foundation (WCF) APIs

Windows Communication Foundation (WCF) is a Microsoft technology that provides a unified programming model for building service-oriented applications. It enables developers to build secure, reliable, and transactional solutions that can interoperate across different platforms and applications.

WCF simplifies the development of distributed applications by abstracting away the complexities of underlying communication protocols, message formats, and network programming. It supports a wide range of communication patterns and protocols, including:

  • HTTP (RESTful and SOAP)
  • TCP
  • MSMQ (Message Queuing)
  • Named Pipes

This documentation provides a comprehensive overview of WCF APIs, covering core concepts, service development, hosting, security, and interoperability aspects.

Core Concepts

Understanding the fundamental building blocks of WCF is crucial for effective development:

  • Service: A unit of functionality exposed to clients.
  • Endpoint: The address and contract of a service, including the binding information.
  • Contract: Defines the operations a service exposes and the messages it exchanges.
  • Binding: Specifies how a client and service communicate (protocol, encoding, security).
  • Hosting: The environment in which a service runs (e.g., IIS, self-hosted).

Service Development

Developing a WCF service involves defining contracts, implementing the service logic, and configuring endpoints.

Service Contracts

Service contracts are defined using the [ServiceContract] attribute and specify the operations available to clients. Methods intended to be exposed as operations are marked with the [OperationContract] attribute.


[ServiceContract]
public interface ICalculatorService
{
    [OperationContract]
    double Add(double operand1, double operand2);

    [OperationContract]
    double Subtract(double operand1, double operand2);
}
                        

Data Contracts

Data contracts define the structure of messages exchanged between clients and services. They are defined using the [DataContract] attribute and members are marked with the [DataMember] attribute.


[DataContract]
public class UserInfo
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Age { get; set; }
}
                        

Service Host

The ServiceHost class is used to instantiate and manage a WCF service. It's responsible for creating endpoints and starting the service.


using (ServiceHost host = new ServiceHost(typeof(MyService)))
{
    host.Open();
    Console.WriteLine("Service is running...");
    Console.ReadLine();
}
                        

Bindings

Bindings configure the transport, encoding, and protocols used for communication. Common built-in bindings include:

BasicHttpBinding

Provides a basic HTTP communication for interoperability with non-WCF clients. It supports SOAP 1.1, HTTP transport, and various encoding options.

WsHttpBinding

Offers more advanced WS-* standards support, including WS-Security, WS-ReliableMessaging, and WS-AtomicTransaction. It's generally preferred for WCF-to-WCF communication when advanced features are needed.

NetTcpBinding

A WCF-optimized binding that uses TCP for high-performance communication. It's suitable for intranet scenarios and provides features like reliability and security.

Hosting WCF Services

WCF services can be hosted in various environments:

  • Internet Information Services (IIS): A common web server environment.
  • Windows Process Activation Service (WAS): For non-HTTP protocols like NetTcp.
  • Self-Hosting: Hosting the service within a custom executable application using ServiceHost.

Security in WCF

WCF provides robust security features to protect services and data:

  • Transport Security: Uses protocols like HTTPS (TLS/SSL) or IPSec.
  • Message Security: Encrypts and signs individual messages using WS-Security.
  • Authentication: Verifies the identity of clients (e.g., Windows credentials, certificates, custom providers).
  • Authorization: Controls access to service operations.

Interoperability

WCF is designed for interoperability. By using standard protocols like HTTP and SOAP, WCF services can be consumed by clients built on different technologies and platforms.