Click here to Skip to main content
15,868,340 members
Articles / Programming Languages / C#

Introduction to WCF Programming

Rate me:
Please Sign up or sign in to vote.
3.32/5 (15 votes)
7 Aug 2007CPOL3 min read 69.4K   373   38   5
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.
    C#
    // 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.
    C#
    // 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.
    C#
    [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
<?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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCommunicating between Silverlight and wcf service using MessageHeaders Pin
elizas16-Feb-10 23:28
elizas16-Feb-10 23:28 
GeneralYou ruined my day! Pin
Bit-Smacker27-Mar-09 10:22
Bit-Smacker27-Mar-09 10:22 
GeneralMy vote of 1 Pin
Member 8914244-Mar-09 9:51
Member 8914244-Mar-09 9:51 
Lack of explanation on a complex subject.
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
masaniparesh29-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.