Introduction to WCF

In this article we learn what is WCF, why we use it and what are the features of WCF.

WCF stands for Windows Communication Foundation. WCF is a framework for building service oriented applications. Most of WCF functionality is included in a single assembly called System.ServiceModel.dll, located in the System.ServiceModel namespace.

Why WCF?
WCF is a generic communication mechanism that allows you to setup generic host communication between two clients. Let me explain what I am trying to say. Suppose I have three clients and we have to implement service for them but all three clients have different implementation.

  1. First client is using PHP/Java. So client wants response/message in xml format.
  2. Second is using .Net but service execution time is high so client wants response/messages in binary format and protocol to be TCP.
  3. Third is using COM+

For first client we have to use webservice, for second we use .Net remoting and for third we use COM+.

Webservice, .Net remoting and COM+ are different technology. So developer have to learn these technology. But WCF bring all the technology under one umbrella.

WCF = Webservice + .Net remoting + MSMQ + COM+


WCF support following features.

  1. Service Oriented Architecture
  2. Interoperability
  3. Multiple Message Pattern
  4. Service Metadata
  5. Data Contracts
  6. Security
  7. Multiple Transport and Encodings
  8. Reliable and Queued Messages
  9. Durable Messages
  10. Transaction
  11. Ajax and REST support
  12. Extensibility

We will talk these topics in later article series.


ABC stands for Address, Binding and Contracts respectively.
Address is a url which points to location of the service. In WCF every service has unique address. Address provides two important things. First location of that service and second is transport protocol. WCF supports the following transport but we implement HTTP transport.

  1. Http/Https
  2. TCP
  3. IPC
  4. Peer newtwork
  5. MSMQ
  6. Service bus

Suppose I made a WCF application and name it myfirstapp and using basicHttpBinding then url should be like http://localhost:8080/myfirstapp.


Bindings expose service that which can be access using http or MSMQ.There are multiple aspects of communication with any given service and there are many possible communication pattern. WCF 4.0 support following bindings.

  1. basicHttpBinding
  2. wsHttpBinding
  3. wsDualHttpBinding
  4. wsFederationHttpBinding
  5. netTcpBinding
  6. netNamedPipeBinding
  7. netMsmqBinding
  8. netPeerTcpBinding
  9. msmqIntegrationBinding
  10. basicHttpContextBinding
  11. netTcpContextBinding
  12. webHttpBinding
  13. wsHttpContextBinding


Contract is an agreement between two parties. It define how a client should communicate with your service. In other word contract is a platform and standard way of describing what the service does. There are four type of contract in WCF.

  1. Service Contract
  2. Data Contract
  3. Fault Contract
  4. Message Contract

Let’s make a WCF application. In this application we are trying to add two number using WCF service. I am using Visual Studio 2012. Open Visual Studio and go to File → New → Project → WCF and select WCF Service Application and name it myFirstApp.

Visual Studio provides some auto generate code. Let’s delete IService1.cs and Service1.svc file.

Add a wcf service file and name it MyService.svc and write following code. It add two files.

  1. IMyService.cs
  2. MyService.svc

Delete all code of ImyService and write following code.

using System.ServiceModel;

namespace myFirstApp{


public interface IMyService{

int AddTwoNo(int intFirstNo, int intSecondNo);}}

ServiceContract describe which operation the client can perform on the service.

OperationContract define within a ServiceContract. It define the parameters and return type of operation.

Now implement the ImyService interface in MyService.svc.cs. Let’s delete all code first from MyService.svc.cs and write the following

namespace myFirstApp{

public class MyService : IMyService{

public int AddTwoNo(int intFirstNo, int intSecondNo){

return intFirstNo + intSecondNo;}}}

Hit F5 for run this application.


After running successfully double click on AddTwoNo() and fill the value in intFirstNo and intSecondNo respectively. I am putting 5 and 6 and hit on invoke button.




Note: Default binding in WCF is basicHttpBinding. We can see all details of this service at http://localhost:36246/myservice.svc which is mention in above image.

Now I am calling this WCF service through application.

Open a new Visual Studio instance and go to File → New → Project → Web and select Asp.Net Empty Web Application and name it WCFClientApp.

Add a webform and name it default.aspx and add the service reference.

Right click on project(WCFClientApp)


click on add service reference and fill the address and click go. After that change namespace as you want and click ok. I am keeping WCFReference namespace.


In default.aspx


<tr><td>First No</td><td><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox></td></tr>

<tr><td>Second No</td><td><asp:TextBox ID="txtSec" runat="server"></asp:TextBox></td></tr>

<tr><td colspan="2"><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td></tr>

<tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>


In default.aspx.cs

using System;

namespace WCFClientApp{

public partial class _default : System.Web.UI.Page{

protected void btnAdd_Click(object sender, EventArgs e){

int intFirstNo = 0, intSecNo = 0, intResult = 0;

intFirstNo = Convert.ToInt16(txtFirst.Text);

intSecNo = Convert.ToInt16(txtSec.Text);

WCFReference.MyServiceClient client = new WCFReference.MyServiceClient();

intResult = client.AddTwoNo(intFirstNo, intSecNo);

lblResult.Text = "Result is :"+intResult;


Run the application



Tags: , , , , ,