need to poll the Web Service at regular intervals, thereby relieving the Web Service of the additional load. To accomplish this, you need a communication model in which the client is always connected to the service and is notified when an event occurs. Using WCF, this communication model can be implemented by using callbacks. A callback allows a service to call back its clients. The roles of the service and the client are now duplicated — the client is also the service, and the service is also the client.

This section of the chapter leads you through building a WCF ticketing service that allows clients to book tickets. When multiple clients are connected to the service, a seat booked by one client is broadcast to all the connected clients. Figure 20-37 illustrates the flow of the system. It shows four cinema branches using the client to connect to the WCF ticketing service. Once seats are selected (represented by the yellow buttons), a client will click on the Book Seats button to send the reservation to the WCF service. The WCF service will then broadcast the booked seats to all connected clients, which will then set the booked seats in red.

Figure 20-37

Building the Service

The WCF service that allows clients to book cinema tickets needs to come first. Launch Visual Studio 2008 and create a new WCF Service Library project. Name the project WcfTicketingService (see Figure 20-38).

Figure 20-38

In this example, the WCF service will be hosted by the WCF Service Host, a utility provided by Visual Studio 2008.

In the IService1.cs file, define the following service and data contracts:

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, which are described in the following table.

Operation Description
SetSeatStatus Allows clients to book seats. Takes in a string containing the seats to be booked.
RegisterClient Registers a client when it connects to the service. Takes in a GUID so that the service can uniquely identify a client.
UnRegisterClient Unregisters a client when it disconnects from the service. Takes in the client's GUID.

The ITicketService interface is also prefixed with the [ServiceContract] attribute. Specifically, note the CallbackContract property, which specifies the interface that defines the callback operation. The SessionMode property is set to Required, indicating that state must be maintained between the service and client.

The ITicketCallBack interface contains one operation — SeatStatus, which allows the service to initiate a callback to the client, thereby updating the client about the latest seat status (that is, which seats have been booked by other clients).

The Client class defines the data contract. It contains the GUID of a client connecting to the service.

All the operations in these two interfaces are defined as one-way operations. To understand why this is so, assume that all the operations use the default request/response model. When the SetSeatStatus() method is called to book seats, it waits for a response from the service. However, the service now invokes the SeatStatus callback on the client (the service informs all clients about the seats booked) and waits for a reply from the client. A deadlock occurs because the client is waiting for a response from the service while the service is waiting for a response from the client after invoking the callback. By defining the operations as one-way, the service can invoke the callback on the client without waiting for a reply from the client, preventing a deadlock from happening.

In the Service1.cs file, define the SeatStatus class:

using System;

using System.Text;

using System.Timers;

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату