Windows Communication Foundation
Prerequisites
* Visual Studio 2005 Professional Edition
* Microsoft Pre-Release Software Microsoft .NET Framework 3.0 - Release Candidate
* Microsoft Visual Studio Code Name "Orcas" Community Technology Preview - Development Tools for .NET Famework 3.0
* Microsoft® Windows® Software Development Kit for RC 1
Introduction to WCF
WCF is a set of .NET Framework-based technologies for building and running services. WCF is the next step in the evolutionary path that started with COM, COM+, and MSMQ, and continues through .NET Remoting, ASMX, System. Messaging and .NET Enterprise Services. WCF combines these technologies and offers a unified programming model for developing services using any CLR-compliant language. The below screenshot provides a conceptual representation of the different components that make up the WCF architecture.

As you see from the above, the major subsystems of WCF are the Service model, the connector framework, hosting environments, and system and messaging services. The WCF service model makes service-oriented development explicit and simple from any CLR-targeted language. You use declarative attributes to mark up which aspects of their type should form the external contract of a service. The service model supports different types of contracts including service contracts, operation contracts and so on, which will be discussed later in this article. The service model also provides instance and context management for a service. WCF routes incoming messages to instances of user-defined service types. As a developer, you can leverage the declarative attributes to control how instances are associated with incoming messages as well as how the session management features of Indigo routes multiple messages to a common session object. Finally, the service model provides declarative behaviors that automate security and reliability. All of the above functionalities of service model are contained in the System.ServiceModel namespace.
Key Components of a WCF Service
A WCF service program contains four elements:
Contract definitions - A service must have at least one service contract, and it might contain multiple service contracts, data contracts, or message contracts
Endpoint definitions - One or more address-binding-contract endpoints must be declared
Hosting code - Some code is needed to create and start the service
Implementation code - The service contracts in a service need code to implement their service operations
Let us look at each of these components in detail.
Understanding Contracts
Contracts are one of the fundamental concepts in WCF. They allow clients and services to have a common understanding of available operations, data structures, and message structures while remaining loosely coupled and platform independent. WCF includes four kinds of contracts:
Service contract - Describes the operations a service can perform. A service contract is defined with the [ServiceContract] and [OperationContract] attributes. Binding requirements can be specified for the contract with a [BindingRequirements] attribute.
Data contract - Describes a data structure. A data contract is defined primarily with the [DataContract] and [DataMember] attributes.
Message contract - Defines what goes where in a message. A message contract is defined primarily with the [MessageContract], [MessageBodyMember], and [MessageHeader] attributes.
Fault contract - Allows you to document the errors that WCF code is likely to produce. A fault contract is specified along with the operation contract at the time of declaring the method. A fault contract is defined using the [FaultContract] attribute.
All four types of contracts translate between Microsoft .NET types used internally and the XML representations shared externally:
> A service contract converts between the CLR and Web Services Description Language (WSDL)
> A data contract converts between the CLR and XML Schema Definition (XSD)
> A message contract converts between the CLR and Simple Object Access Protocol (SOAP)
> A fault contract converts the CLR exceptions and to SOAP faults
You define contracts by using familiar object-oriented constructs: interfaces and classes. By decorating interfaces and classes with attributes, you create contracts.
Understanding Service Contracts
A service contract describes the operations a service can perform. A service must have at least one service contract, and it can have more than one. You can think of a service contract as follows:
* It describes the client-callable operations (functions) exposed by the service
* It maps the interface and methods of your service to a platform-independent description
* It describes message exchange patterns that the service can have with another party. Some service operations might be one-way; others might require a request-reply pattern
* It is analogous to the element in WSDL
You define a service contract by annotating an interface (or class) with the [ServiceContract] attribute. You identify service operations by placing [OperationContract] attributes on the methods of the interface. The following service contract defines only one service operation.
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string HelloWorld (string input);
}
Once you define a service contract using an interface, you can write a class to implement the interface. For example:
Public class HelloWorldService : IHelloWorldService
{
public string HelloWorld(string input) { return "Hello " + input; }
}
It is also possible for you to define the service contract directly against the implementation class and skip the interface altogether. The following class both defines and implements a service contract.
[ServiceContract]
public class HelloWorldService
{
[OperationContract]
public string HelloWorld(string input) { return "Hello " + input; }
}
Although this approach works, it is not a recommended way to create services as interfaces allow you to separate the implementation from the definition of a service.
Understanding Bindings
The bindings of a WCF service define how an endpoint will communicate with an external client. A binding has several characteristics, including the following:
* Transport protocols - Some of the choices include HTTP, Named Pipes, TCP, and MSMQ.
* Encoding - Three types of encoding are available-Text, Binary, or Message Transmission Optimization Mechanism (MTOM). MTOM is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K).
* Security - Includes wire security (SSL) or schema-defined security (WS-Security).
Bindings can also determine if you are using sessions or a transacted communications channel. You have the choice of creating custom channels or using prebuilt bindings. WCF comes shipped with nine built-in bindings. The following table lists the WCF build-in bindings and their associated features.

You can choose the right binding to use by mapping the features they support to your requirements.
Hosting WCF Services
As for where to put your code and what it compiles to, you have some choices. You can host your service in Internet Information Services (IIS), or you can write a small amount of extra code to host a service yourself. You can self-host a service from just about any environment that supports managed code, including a WinForms application, console application, library assembly (DLL), or Windows Service controlled through SCM (Service Control Manager). The following lists show the common hosting mechanisms for WCF services.
* IIS (Internet Information Services) - IIS provides a number of advantages if the service uses HTTP as its transport. The nice thing about using IIS is that you don't have to write any hosting code as part of the application since IIS automatically activates service code as required. Services also benefit from IIS features such as process lifetime management and automatic restart after configuration changes. To run services using IIS, you create the service code along with its configuration file and simply save them in an IIS virtual directory.