Home > DeveloperSection > Articles > WCF (Windows Communication Foundation) – Overview and Sample Code in C# .NET

WCF (Windows Communication Foundation) – Overview and Sample Code in C# .NET

2 Comment(s)
 18309  View(s)
Rate this:

WCF (Windows Communication Foundation) – Overview and Sample Code in C# .NET

What Is Windows Communication Foundation (WCF)

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. A few sample scenarios include:

  • A secure service to process business transactions.
  • A service that supplies current data to others, such as a traffic report or other monitoring service.
  • A chat service that allows two people to communicate or exchange data in real time.
  • A dashboard application that polls one or more services for data and presents it in a logical presentation.
  • Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.
  • A Silverlight application to poll a service for the latest data feeds.

While creating such applications was possible prior to the existence of WCF, WCF makes the development of endpoints easier than ever. In summary, WCF is designed to offer a manageable approach to creating Web services and Web service clients.

- It combines functionalities of Asp.Net Web services, remoting, MSMQ and Enterprise Services.
- It’s more reliable and secured then Web services.
- All the major changes can be done in the configuration file instead of code file.



What are the differences between WCF and Web Service


WCF has
[ServiceContract] attribute attached with the class.
WS has
[WebService] attribute attached with the class.
WCF has
[OperationContract] attribute attached with methods.
WS has
[WebMethod] attribute attached with methods

WCF can be hosted on IIS, WAS, of self-hosting.
WS can be hosted on IIS.

WCF uses
using System.Runtime.Serialization; namespace for serialization.
WS uses
using System.Xml.Serialization; namespace for serialization.

WCF support HTTP, TCP, MSMQ, P2P transport protocols.
WS support Http protocol.

WCF supports XML, MTOM (Messaging Transmission Optimization Mechanism), Binary.
WS supports XML, MTOM, DIME (Direct Internet Message Encapsulation).

WCF provides reliable security for messaging and transactions over net.
WS doesn’t provide method level security & message delivery isn’t assured & can be lost without acknowledgement.


Few Important points in WCF


WCF service location that client can use to expose its features

Configuration for Transport protocol, security and encoding mechanism.

It defines type of data and type of operations, a client application can use from WCF service.

Service Contract in WCF

It has 2 attributes:
[ServiceContract] and [OperationContract]



    public interface IService1



        string GetData(int value);



        CompositeType GetDataUsingDataContract(CompositeType composite);


        // TODO: Add your service operations here


Data Contract in WCF

It has 2 attributes:
[DataContract]and [DataMember]


// Use a data contract as illustrated in the sample below to add composite types to service operations


    public class CompositeType


        bool boolValue = true;

        string stringValue = "Hello ";



        public bool BoolValue


            get { return boolValue; }

            set { boolValue = value; }




        public string StringValue


            get { return stringValue; }

            set { stringValue = value; }




WCF configurations in config file

It declares information about endpoint name, address, binding and contract.


<?xml version="1.0" encoding="utf-8" ?>



    <compilation debug="true" />


  <!-- When deploying the service library project, the content of the config file must be added to the host's

  app.config file. System.Configuration does not support config files for libraries. -->



      <service name="WcfServiceLibrary1.Service1"behaviorConfiguration="WcfServiceLibrary1.Service1Behavior">



            <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />



        <!-- Service Endpoints -->

        <!-- Unless fully qualified, address is relative to base address supplied above -->

        <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">


              Upon deployment, the following identity element should be removed or replaced to reflect the

              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity




            <dns value="localhost"/>



        <!-- Metadata Endpoints -->

        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->

        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->

        <endpoint address="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>





        <behavior name="WcfServiceLibrary1.Service1Behavior">

          <!-- To avoid disclosing metadata information,

          set the value below to false and remove the metadata endpoint above before deployment -->

          <serviceMetadata httpGetEnabled="True"/>

          <!-- To receive exception details in faults for debugging purposes,

          set the value below to true.  Set to false before deployment

          to avoid disclosing exception information -->

          <serviceDebug includeExceptionDetailInFaults="False" />








By John Smith on   5 years ago
Uttam,Thanks for the detailed information about WCF.
Really useful article

Don't want to miss updates? Please click the below button!

Follow MindStick