Monday, May 30, 2011

Eliminate Server Polling with WCF Callbacks


Client applications that share centralized information typically poll a central service to get updated information—a pull model. But by using WCF callbacks, the central service can push updated information to the clients in near-real time. 



he traditional ASMX web service request/response communication model is limiting, because it is passive—in other words, web services return results only when called by client code. For example, imagine a scenario in which a particular cinema operator deploys a web service so users can purchase tickets online. The cinema's branches have systems that connect to the web service to obtain the current seat allocation and that sell tickets to cinema-goers. Using this model, the branch systems have to poll the web service regularly to get updated seat information. The model also makes it very likely that more than one branch system will attempt to book the same seat(s) at the same time.


A better approach would be for the web service to notify all the branches whenever seat status changes and a seat has been reserved. That way, all the branches would have the latest seat information, and remove the need to poll the web service, reducing the overall load. However, to accomplish this, you would need a communication model where the clients remain connected to the service, so they can be notified when an event occurs. Using WCF, you can implement this communication model using callbacks.

A callback allows a service to call back its clients. The roles of the service and the client are now reversed—the client becomes a service and the service becomes a client.

Build a WCF Ticketing Service
In this section you'll see how to build a WCF ticketing service that allows clients to book tickets. When multiple clients are connected to the service, the system will broadcast seats booked by any client to all the connected clients. Figure 1 shows the system flow.

Figure 1. System Flow: In this system, seats booked by any client are broadcast to all other connected clients.
Figure 2WCF Ticketing Web Service: Create a new WCF Service Library project and name it WcfTicketingService.

First, you need to create the WCF service that allows clients to book cinema tickets. Launch Visual Studio 2008 and create a new WCF Service Library project. Name it WcfTicketingService(see Figure 2).

In this example, the WCF service will be hosted by the WCF Service Host, a utility provided by Visual Studio 2008. When you create the project, you'll see an IService1.cs file. Open that file and define the following service and data contracts (note that you can download the code for this project):

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Runtime.Serialization;
   using System.ServiceModel;
   using System.Text;
   
   namespace WcfTicketingService
   {
      [ServiceContract(
         Name = "TicketingService,"
         Namespace = "http://www.learn2develop.net/,"
         CallbackContract = typeof(ITicketCallBack),
         SessionMode = SessionMode.Required)]
      public interface ITicketService
      {
         [OperationContract(IsOneWay = true)]
         void SetSeatStatus(string strSeats);
   
         [OperationContract(IsOneWay = true)]
         void RegisterClient(Guid id);
   
         [OperationContract(IsOneWay = true)]
         void UnRegisterClient(Guid id);
      }
   
      public interface ITicketCallBack
      {
         [OperationContract(IsOneWay = true)]
         void SeatStatus(string message);
      }
   
      //---each client connected to the service has a GUID---
      [DataContract]
      public class Client
      {
         [DataMember]
         public Guid id { get; set; }
      }
   }
The ITicketService interface defines three operations:

  • SetSeatStatus: Allows clients to book seats. This operation accepts a string value containing the seats to be booked.
  • RegisterClient: Registers a client when it connects to the service. This operation accepts a GUID that uniquely identifies each client.
  • UnRegisterClient: Unregisters a client when it disconnects from the service. This operation accepts the client GUID.
Note that The ITicketService interface is decorated with the [ServiceContract] attribute. The CallbackContract property specifies the interface that defines the callback operation. In the preceding code, the SessionMode property is set to Required, indicating that state must be maintained between the service and client.

The other interface defined in the preceding code, the ITicketCallBack interface, contains only one operation—SeatStatus, which allows the service to initiate a callback to the client. The callback updates the client, letting it know which seats have been booked by other clients.

Finally, the Client class defines the data contract. Its single member holds the GUID of a client connecting to the service.



No comments: