Click here to Skip to main content
Click here to Skip to main content

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
    [ServiceContract()] 
    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)
        [OperationContract]           
        double Add(double x, double y); 
        [OperationContract] 
        double Subtract(double x, double y); 
        [OperationContract] 
        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
    [DataContract]
    public class ComplexNumber 
    { 
        [DataMember] 
        public double Real = 0.0D; 
        [DataMember] 
        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.
    [MessageContract] 
    public class ComplexProblem 
    { 
        [MessageHeader] 
        public string operation; 
        [MessageBody] 
        public ComplexNumber n1; 
        [MessageBody] 
        public ComplexNumber n2; 
        [MessageBody] 
        public ComplexNumber solution; 
        // Constructors… 
    } 

Bindings

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="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 
    <system.serviceModel> 
        <services> 
            <service type="CalculatorService"> 
                <endpoint address=http://localhost/calculator 
		binding="basicHttpBinding" contractType="ICalculator" 
                bindingConfiguration="Binding1" /> 
            </service> 
        </services> 
        <bindings> 
            <basicHttpBinding> 
                <binding configurationName="Binding1" 
		hostNameComparisonMode="StrongWildcard" sendTimeout="00:10:00" 
		maxMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" 
                </binding> 
            </basicHttpBinding> 
        </bindings> 
    </system.serviceModel> 
</configuration> 

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:

  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 (http://msdn2.microsoft.com/en-US/library/aa702581.aspx).

History

  • 7th August, 2007: Initial post

License

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

Comments and Discussions

 
GeneralNice Article PinmemberVuyiswa Maseko29-Oct-08 8:35 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Radhakrishnan R
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid