//...

 }

 public class SeatStatusCallback :

  ServiceReference1.TicketingServiceCallback {

  public void SeatStatus(string message) {

   Form1.SeatsOccupied(message);

  }

 }

}

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) {

 InstanceContext context =

  new InstanceContext(new SeatStatusCallback());

 _client = new

  ServiceReference1.TicketingServiceClient(context);

 _client.RegisterClient(_guid);

 //---display the seats---

 seatsArray = new Button[COLUMNS, ROWS];

 for (int r = 0; r < ROWS; r++) {

  for (int c = 0; c < ROWS; c++) {

   Button btn = new Button();

   btn.Location = new Point(

    START_X + (SEAT_WIDTH * c),

    START_Y + (SEAT_HEIGHT * r));

   btn.Size = new Size(SEAT_WIDTH, SEAT_HEIGHT);

   btn.Text =

    (c + 1).ToString() + '-' + (r + 1).ToString();

   btn.BackColor = Color.White;

   seatsArray[c, r] = btn;

   btn.Click += new EventHandler(btn_Click);

   this.Controls.Add(btn);

  }

 }

}

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) {

 string seatsToBook = string.Empty;

 for (int r = 0; r < ROWS; r++) {

  for (int c = 0; c < ROWS; c++) {

   if (seatsArray[c, r].BackColor == Color.Yellow) {

    seatsToBook += seatsArray[c, r].Text + ',';

   }

  }

 }

 //---send to WCF service---

 _client.SetSeatStatus(seatsToBook);

}

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);

}

Testing the Application

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:

...

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

0

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

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