For simplicity of demonstration, the following shortcuts are made:

□ The seats booked by a client are simply broadcast to all connected clients. In real life, they would also be saved in database or array.

□ When new clients connect to the server, the current seat allocation status (which seats are booked and which are not) is not sent to them.

Next, double-click on the App.config file in Solution Explorer. Change the following highlighted attributes values:

<system.serviceModel>: <services>

 <service name='WcfTicketingService.Ticketing'

  behaviorConfiguration='WcfTicketingService.Service1Behavior'>

  <host>

   <baseAddresses>

    <add

     baseAddress='http://localhost:8731/Design_Time_Addresses/WcfTicketingService/Service1/' />

   </baseAddresses>

  </host>

  <!-- Service Endpoints -->

  <!-- Unless fully qualified, address is relative to base address

   supplied above -->

  <endpoint address='' binding='wsHttpBinding'

   contract='WcfTicketingService.ITicketService'>

   ...

Right-click on the App.config file, and select Edit WCF Configuration. Expand the EndPoints node (see Figure 20-39), and select the first [Empty Name] node. Set its properties as follows:

Property Value
Address net.tcp://localhost:5000/TicketingService
Binding NetTcpBinding

Figure 20-39

TCP is the transport protocol.

Save the app.config file and close the configuration window. Press F5 to debug the service. In the WCF Test Client, you will see something like Figure 20-40. The error icons (represented by the exclamation symbols) are normal.

Figure 20-40

Building the Client

The WCF service is complete, so it's time to build the client to consume the service. Add a new Windows Forms Application project to the current solution. Name the project Client.

Add a service reference to the ticketing WCF service. In the Add Service Reference dialog, click the Discover button and locate the Ticketing WCF service (see Figure 20-41). Click OK.

Figure 20-41

Populate Form1 with the controls shown in Figure 20-42. Set the Size property of Form1 to 477, 387.

Figure 20-42

In the code-behind of Form1, import the following namespace:

using System.ServiceModel;

Declare the following constants and objects:

namespace Client {

 public partial class Form1 : Form {

  int ROWS = 10;

  int COLUMNS = 10;

  const int SEAT_WIDTH = 45;

  const int SEAT_HEIGHT = 25;

  const int START_X = 10;

  const int START_Y = 40;

  static Button[,] seatsArray;

  private ServiceReference1.TicketingServiceClient _client;

  private Guid _guid = Guid.NewGuid();

Define the SeatsOccupied() static function within the Form1 class as follows:

public partial class Form1 : Form {

 ...

 ...

 ...

 //---set all occupied seats in red---

 public static void SeatsOccupied(string strSeatsOccupied) {

  string[] seats = strSeatsOccupied.Split(',');

  for (int i = 0; i < seats.Length - 1; i++) {

   string[] xy = seats[i].Split('-');

   Button btn = seatsArray[int.Parse(xy[0]) - 1,

    int.Parse(xy[1]) - 1];

   btn.BackColor = Color.Red;

  }

 }

}

This function accepts a string containing the seats that are occupied. The format of the string is:

<column>-<row>,<column>-<row>,...

For each seat (represented by the Button control) that is booked, the background color is changed to red.

Define the SeatStatusCallback class and implement the SeatStatus() method as defined in the TicketingServiceCallback interface (defined in the service):

namespace Client {

 public partial class Form1 : Form {

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

0

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

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