Click here to Skip to main content
14,098,213 members
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 7 Aug 2007
Licenced CPOL

Introduction to WCF Programming

, 7 Aug 2007
Rate this:
Please Sign up or sign in to vote.
This is a beginner article for programming WCF

WCF Introduction

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.

WCF Evolution

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.

WCF OverView

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.

Core Concepts

A WCF Service is composed of three components parts viz,

  1. Service Class - A WCF service class implements some service as a set of methods.
  2. 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.
  3. 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.

Screenshot - WCF_Arch.gif

Contracts in Detail

There are three types of contracts namely,

  1. Service Contracts - Describes the operations a service can perform. Maps CLR types to WSDL.
    // This is the way a service contract is defined.
    // This exposes a service to the client
    public interface IMath 
        // This explains how to expose the methods of the service.
        // Need to mark all the methods that you like to expose
        // Methods that are not marked with this attribute will 
        // will not be available to the client (in this case Divide)
        double Add(double x, double y); 
        double Subtract(double x, double y); 
        double Multiply(double x, double y); 
        double Divide(double x, double y); 
  2. Data Contracts - Describes a data structure. Maps CLR types to XSD.
    // Defining a data contract
    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; 
  3. 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; 
        // Constructors… 


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.

<?xml version="1.0" encoding="utf-8" ?> 
<configuration xmlns=""> 
            <service type="CalculatorService"> 
                <endpoint address=http://localhost/calculator 

		binding="basicHttpBinding" contractType="ICalculator" 

                bindingConfiguration="Binding1" /> 
                <binding configurationName="Binding1" 

		hostNameComparisonMode="StrongWildcard" sendTimeout="00:10:00" 

		maxMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" 


Using the Code

You can find a fully functioning code in the zip (WCF 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:

  1. WCFServiceLibrary2 - This is the web service.
  2. ManualHosting - This is a console application to host the web service.
  3. WebClientByManual - This is also a console application. It's the client.

The build order would be Web WCFServiceLibrary2, 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 (


  • 7th August, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Radhakrishnan R
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralCommunicating between Silverlight and wcf service using MessageHeaders Pin
elizas16-Feb-10 23:28
groupelizas16-Feb-10 23:28 
GeneralYou ruined my day! Pin
Bit-Smacker27-Mar-09 10:22
memberBit-Smacker27-Mar-09 10:22 
Thanks for a great explanation of WCF! Now that I know what it can do, I'm going to stop my current service project and redesign it for WCF.
GeneralMy vote of 1 Pin
Member 8914244-Mar-09 9:51
memberMember 8914244-Mar-09 9:51 
GeneralNice Article Pin
Vimalsoft(Pty) Ltd29-Oct-08 8:35
professionalVimalsoft(Pty) Ltd29-Oct-08 8:35 
Generalinteractive window station under non-system account Pin
masaniparesh29-Nov-07 4:14
membermasaniparesh29-Nov-07 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190518.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Radhakrishnan R
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid