WCF Learning Series: Part 1

WCF Introduction

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.

WCF = WebService + .Net Remoting + MSMQ + COM+

Scenarios where WCF must be used:

  1. A secure service to process business transactions.

  2. A service that supplies current data to others, such as a traffic report or other monitoring service.

  3. A chat service that allows two people to communicate or exchange data in real time.

  4. A dashboard application that polls one or more services for data and presents it in a logical presentation.

  5. Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.

  6. A Silverlight application to poll a service for the latest data feeds.

Features of WCF

  • Service Orientation
  • Interoperability
  • Multiple Message Patterns
  • Service Metadata
  • Data Contracts
  • Security
  • Multiple Transports and Encodings
  • Reliable and Queued Messages
  • Durable Messages
  • Transactions
  • AJAX and REST Support
  • Extensibility

The advantages of WCF

  • WCF can be configured to work independently of SOAP and use RSS instead.

  • WCF is one of the fastest communication technologies and offers excellent performance compared to other Microsoft specifications.

  • To improve communication, transmission speed needs to be optimized. This is done by transmitting binary-coded XML data instead of plain text to decrease latency.

  • Object life-cycle management and distributed transaction management are applicable to any application developed using WCF.

  • WCF uses WS specifications to provide reliability, security and transaction management.

  • Messages can be queued using persistence queuing. As a result, no delays occur, even under high traffic conditions.

Now we will learn the basics of WCF:

  • The following are the ABCs of Windows Communication Foundation:

      "A" stands for Address: Where is the service?
      "B" stands for Binding: How do I talk to the service?
      "C" stands for Contract: What can the service do for me?

  • "A" stands for Address: as expressed in the wsdl:service section and links wsdl:binding to a concrete service endpoint address.

  • "B" stands for Binding: as expressed in the wsdl:binding section and binds a wsdl:portType contract description to a concrete transport, an envelope format and associated policies.

  • "C" stands for Contract: as expressed in the wsdl:portType, wsdl:message and wsdl:type sections and describes types, messages, message exchange patterns and operations.

Address

The address specifies the location of the service that will be exposed for clients that will use it to communicate with the service. The address's protocol that WCF can provide:

  • HTTP
  • TCP
  • NamedPipe
  • Peer2Peer
  • MSMQ

Binding

Specifies how a service is accessible. In other words, how the two parties will communicate in terms of transport (HTTP, TCP, NamedPipe, Peer2Peer, MSMQ), encoding (text, binary and so on) and protocols (like transactional support or reliable messaging).

The following are the Bindings supported by WCF 4.0:

  • basicHttpBinding
  • wsHttpBinding
  • wsDualHttpBinding
  • wsFederationHttpBinding
  • netTcpBinding
  • netNamedPipeBinding
  • netMsmqBinding
  • netPeerTcpBinding
  • msmqIntegrationBinding
  • basicHttpContextBinding
  • netTcpContextBinding
  • webHttpBinding
  • wsHttpContextBinding

Contract

A contract is an agreement between two parties. It defines how a client should communicate with your service.

The following are the possible types of contracts in WCF:

  • Service Contract
  • Data Contract
  • Fault Contract
  • Message Contract

Now we will learn this by creating a simple HelloWCF program.

Open Visual Studio and select "File" -> "New" -> "Project...".

New Project
                                                                           Image 1.

Select WCF Service Application. Give it a name.

WCF Service Application
                                                                                 Image 2.

Remove IService1.cs and Service1.svc file from here. Now right-click on Solution Explorer and select Add new item.

add new item
                                                                                 Image 3.

Now open IMyService.cs and add the following method:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace Hello_WCF_Service  
  9. {  
  10.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMyService" in both code and config file together.  
  11.     [ServiceContract]  
  12.     public interface IMyService  
  13.     {  
  14.         [OperationContract]  
  15.         string AddText(string Name);  
  16.     }  
  17. }  
Now open the MyService.cs file:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace Hello_WCF_Service  
  9. {  
  10.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MyService" in code, svc and config file together.  
  11.     // NOTE: In order to launch WCF Test Client for testing this service, please select MyService.svc or MyService.svc.cs at the Solution Explorer and start debugging.  
  12.     public class MyService : IMyService  
  13.     {  
  14.         public string AddText(string Name)  
  15.         {  
  16.             return "Welcome " + Name;  
  17.         }  
  18.     }  
  19. }  
Now run your service.

processing
                                                                              Image 4.

You can test your method.

add text
                                                                              Image 5.

You can view the config file like the following.

config file
                                                                           Image 6.

See your service in the browser.

service in browser
                                                                                 Image 7.

Now we will consume this service in our web application. So right-click on Solution Explorer and Add a new project.

add new project
                                                                              Image 8.

Select Web -> ASP.NET Web Application.

Web Application
                                                                              Image 9.

Now add the WCF Service reference. Right-click on your project then seelct Add Service reference.

Add Service reference
                                                            Image 10.

my service
                                                                                Image 11.

Now open your Default.aspx page:
  1. <table cellpadding="10" cellspacing="10" width="90%" style="border: solid 4px green;">  
  2.       <tr>  
  3.           <td>Enter Name #  
  4.           </td>  
  5.           <td>  
  6.               <asp:TextBox ID="txtName" runat="server" Width="250px"></asp:TextBox>  
  7.            </td>  
  8.       </tr>  
  9.       <tr>  
  10.           <td></td>  
  11.           <td>  
  12.               <asp:Button ID="btnCallService" runat="server" Text="Click Me" OnClick="btnCallService_Click" />  
  13.           </td>  
  14.       </tr>  
  15.       <tr>  
  16.           <td></td>  
  17.           <td>  
  18.               <asp:Label ID="lblMessage" runat="server" Font-Bold="true" Font-Size="16pt" ForeColor="Red"></asp:Label>  
  19.           </td>  
  20.       </tr>  
  21.  </table>  
Now open the Default.aspx.cs page and call your WCF Service method:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace MyProject  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.   
  15.         }  
  16.   
  17.         protected void btnCallService_Click(object sender, EventArgs e)  
  18.         {  
  19.             MyWCFService.MyServiceClient myService = new MyWCFService.MyServiceClient();  
  20.             lblMessage.Text = myService.AddText(txtName.Text);  
  21.         }  
  22.     }  
  23. }  
Now run you application.

my first wcf application
                                                                              Image 12.

welcome
                                                                                 Image 13.

Up Next
    Ebook Download
    View all
    Learn
    View all