Introduction to WCF: Part 1

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

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 set up a generic host communication between two clients. Let me explain what I am trying to say. Suppose I have three clients and we need to implement a service for them but all three clients have different implementations.

  1. The first client uses PHP/Java. So the client wants a response/message in XML format.

  2. The second is using .Net but the service execution time requirements are high so the client wants responses/messages in binary format and the protocol to be TCP.

  3. Third is using COM+.

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

Webservice, .Net remoting and COM+ are different technologies. So the developer must learn these technologyies But WCF unites all the technologies under one umbrella.

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

WCF

WCF supports the 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 about these topics in later articles of this series.

ABC of WCF

ABC stands for Address, Binding and Contracts respectively.

Address

An address is a URL that points to the location of the service. In WCF every service has a unique address. The address provides two important things. First is the location of that service and the second is the transport protocol. WCF supports the following transports but we implement the 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 the URL should be like http://localhost:8080/myfirstapp.

Bindings

Bindings expose a service that can be accessed using HTTP or MSMQ. There are multiple aspects of communication with any given service and there are many possible communication patterns. WCF 4.0 supports the 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

A contract is an agreement between two parties. It defines how a client should communicate with your service. In other words a contract is a platform and standard way of describing what the service does. There are the following four types of contracts in WCF:

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

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

Visual Studio provides some auto-generated code. Let's delete the IService1.cs and Service1.svc files. Add a WCF service file and name it MyService.svc and provide it the following code. It adds the following two files.

  1. IMyService.cs
  2. MyService.svc

Delete all code of ImyService and write the following code.

  1. using System.ServiceModel;  
  2.   
  3. namespace myFirstApp  
  4. {  
  5.    [ServiceContract]  
  6.    public interface IMyService  
  7.    {  
  8.       [OperationContract]  
  9.       int AddTwoNo(int intFirstNo, int intSecondNo);  
  10.    }  
  11. }  
ServiceContract describes which operation the client can perform on the service.

OperationContract is defined within a ServiceContract. It defines the parameters and return type of the operation.

Now implement the ImyService interface in MyService.svc.cs. Let's delete all the code first from MyService.svc.cs and write the following:
  1. namespace myFirstApp  
  2. {  
  3.    public class MyService : IMyService  
  4.    {  
  5.       public int AddTwoNo(int intFirstNo, int intSecondNo)  
  6.       {  
  7.          return intFirstNo + intSecondNo;  
  8.       }  
  9.    }  
  10. }  
Hit F5 to run this application.

WCF test client

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

design view

Output:

response

Note: The default binding in WCF is basicHttpBinding. We can see all details of this service at http://localhost:36246/myservice.svc that is shown in the preceding image.

Now I am calling this WCF service using an ASP.NET application.

Open a new Visual Studio instance and go to "File" -> "New" -> "Project..." 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 the project (WCFClientApp).

add service reference

Click on Add service reference and fill in the address and click go. Then change the namespace as you want and click OK. I am keeping the WCFReference namespace.

my service

In default.aspx
  1. <table>  
  2.    <tr><td>First No</td><td><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox></td></tr>  
  3.    <tr><td>Second No</td><td><asp:TextBox ID="txtSec" runat="server"></asp:TextBox></td></tr>  
  4.    <tr><td colspan="2"><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td></tr>  
  5.    <tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>  
  6. </table>  
In default.aspx.cs
  1. using System;  
  2.   
  3. namespace WCFClientApp  
  4. {  
  5.    public partial class _default : System.Web.UI.Page  
  6.    {  
  7.       protected void btnAdd_Click(object sender, EventArgs e)  
  8.       {  
  9.          int intFirstNo = 0, intSecNo = 0, intResult = 0;  
  10.          intFirstNo = Convert.ToInt16(txtFirst.Text);  
  11.          intSecNo = Convert.ToInt16(txtSec.Text);  
  12.          WCFReference.MyServiceClient client = new WCFReference.MyServiceClient();  
  13.          intResult = client.AddTwoNo(intFirstNo, intSecNo);  
  14.          lblResult.Text = "Result is :"+intResult;  
  15.       }  
  16.    }  
  17. }  
Run the application.

Output

output

I hope this article is helpful for you.

Next Article>> Introduction to Endpoint in WCF: Part 2