WCF is a new communication subsystem to enable applications, in one machine or across multiple machines connected by a network, to communicate. WCF applications can be developed in any language which can target the .NET runtime. It is a part of .NET framework 3.0. WCF stands for Windows Communication Foundation.
Software has seen different generations. The first generation, i.e., late 1980s Object oriented programming(OOP) gave a new shape to computer programming. It introduced concepts like Polymorphism, Encapsulation, Subclassing, etc. The second generation started in mid 1990s and marks the introduction of OS with GUI and this demanded for developing software with extensive reusability. It introduced Component Oriented programming with new concepts like Location Transparency, Tight Coupling, Runtime Metadata, etc. The third generation is called internet era started in the early 2000s. It aimed at developing distributed applications. It took the software development towards a Service Oriented direction. It concentrated mainly on the transmission/packaging of message. It came up with the concepts like Schema, Contract, Policy which are all message based.
The WCF programming model unifies Web Services, .NET Remoting, Distributed Transactions, and Message Queues into a single Service-oriented programming model for distributed computing. WCF uses SOAP messages for communication between two processes, thereby making WCF-based applications interoperable with any other process that communicates via SOAP messages.
A WCF Service is composed of three components parts viz,
- Service Class - A WCF service class implements some service as a set of methods.
- Host Environment - A Host environment can be a Console application or a Windows Service or a Windows Forms application or IIS as in case of the normal asmx web service in .NET.
- Endpoints - All communications with the WCF service will happen via the endpoints. The endpoint is composed of 3 parts (collectively called as ABC's of endpoint) as defined below:
- Address: The endpoints specify an Address that defines where the endpoint is hosted.
- Contract: The endpoints specify a Contract that defines which methods of the Service class will be accessible via the endpoint; each endpoint may expose a different set of methods.
- Binding: The endpoints also define a binding that specifies how a client will communicate with the service and the address where the endpoint is hosted. Various components of the WCF are depicted in the figure below.
Contracts in Detail
There are three types of contracts namely,
- Service Contracts - Describes the operations a service can perform. Maps CLR types to WSDL.
public interface IMath
double Add(double x, double y);
double Subtract(double x, double y);
double Multiply(double x, double y);
double Divide(double x, double y);
- Data Contracts - </strong />Describes a data structure. Maps CLR types to XSD.
public class ComplexNumber
public double Real = 0.0D;
public double Imaginary = 0.0D;
public ComplexNumber(double r, double i)
this.Real = r;this.Imaginary = i;
- Message Contracts - Defines the structure of the message on the wire. Maps CLR types to SOAP messages.
public class ComplexProblem
public string operation;
public ComplexNumber n1;
public ComplexNumber n2;
public ComplexNumber solution;
Bindings can be defined in the config file as well as programmatically. Here is an example of how binding is defined through a config file.
maxMessageSize="65536" messageEncoding="Text" textEncoding="utf-8"
Using the Code
You can find a fully functioning code in the zip (WCF Explained.zip) file which has a manually configured Client. The language used is C# 2.0. You need .NET Framework to work with WCF. There is a service with simple behaviors. There are 3 projects in the solution:
WCFServiceLibrary2 - This is the web service.
ManualHosting - This is a console application to host the web service.
WebClientByManual - This is also a console application. It's the client.
The build order would be Web
ManualHosting and then
Client. In this example, host and client are simple console applications. Just play around with the things, will learn a lot more.
Points of Interest
This example is designed in such a way that the host listens to multiple endpoints. The client is also written in such a way that it queries to multiple endpoints of the host. The binding is done programmatically in the example rather that using a config file. Proxy is not created here, instead interfaces are manually created in the client to give a much deeper understanding of what happens when a proxy is created. Proxy can be created using svcutil.exe (http://msdn2.microsoft.com/en-US/library/aa702581.aspx).
- 7th August, 2007: Initial post