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

The advantages of WCF

Now we will learn the basics of WCF:

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:

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:

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:

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. namespace Hello_WCF_Service
  8. {
  9. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMyService" in both code and config file together.
  10. [ServiceContract]
  11. public interface IMyService
  12. {
  13. [OperationContract]
  14. string AddText(string Name);
  15. }
  16. }
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. namespace Hello_WCF_Service
  8. {
  9. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MyService" in code, svc and config file together.
  10. // 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.
  11. public class MyService : IMyService
  12. {
  13. public string AddText(string Name)
  14. {
  15. return "Welcome " + Name;
  16. }
  17. }
  18. }
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. namespace MyProject
  8. {
  9. public partial class WebForm1 : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void btnCallService_Click(object sender, EventArgs e)
  15. {
  16. MyWCFService.MyServiceClient myService = new MyWCFService.MyServiceClient();
  17. lblMessage.Text = myService.AddText(txtName.Text);
  18. }
  19. }
  20. }
Now run you application.

my first wcf application
Image 12.

welcome
Image 13.