//...
}
}
The SeatStatus()
method is invoked when the service calls the client's callback. Here, you call the static SeatsOccupied()
function to update the seats status.
Code the Form1_Load
event handler as follows:
private void Form1_Load(object sender, EventArgs e) {
}
These statements basically create an instance of the InstanceContext
class by passing it an instance of the SeatStatusCallback
class. Then an instance of the WCF client is created using the constructor that requires an InstanceContext
object. In addition, the form is dynamically populated with Button controls representing the seats in a cinema. Each Button control's Click
event is wired to the btn_Click
event handler.
Define the btn_Click
event handler as follows:
void btn_Click(object sender, EventArgs e) {
if (((Button)sender).BackColor == Color.White) {
((Button)sender).BackColor = Color.Yellow;
} else if (((Button)sender).BackColor == Color.Yellow) {
((Button)sender).BackColor = Color.White;
}
}
This event handler toggles the color of the seats as users click on the Button controls. White indicates that the seat is available; yellow indicates that the seat has been selected for booking.
Code the Book Seats button as follows:
private void btnBookSeats_Click(object sender, EventArgs e) {
}
To specify the seats that are selected for booking, a string is created to containing the seats to be booked in the following format:
<column>-<row>,<column>-<row>,...
Finally, code the Form1_FormClosing
event as follows:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
_client.UnRegisterClient(_guid);
}
To test the application, press F5 to debug and launch the service. Once this is done, you can debug the client. Right-click the Client
project in Solution Explorer, and select Debug→Start New Instance (see Figure 20-43).

Figure 20-43
Run a few instances of the client and you can start to book cinema tickets. As one client books the seats, the other clients are automatically updated.
Calling WCF Services from an AJAX Page
Visual Studio 2008 includes the new AJAX-enabled WCF Service template that enables you to consume WCF services, using AJAX. To try it out, use Visual Studio 2008 to create a new ASP.NET Web Application project. Name the project AJAXWCF
(see Figure 20-44).

Figure 20-44
Right-click the project name in Solution Explorer, and select Add New Item (see Figure 20-45).

Figure 20-45
Select the AJAX-enabled WCF Service template (see Figure 20-46), name it Service.svc
, and click Add.

Figure 20-46
Notice that Visual Studio 2008 automatically inserts the <system.serviceModel>
element into the Web.config
file:
...