Web Resources

Answers to Self Review Exercises

22.1

a) False. Web services are used to execute methods on remote machines. The Web service receives the arguments it needs to execute a particular method, executes the method and returns the result to the caller. b) False. The proxy is created by Visual C# or Visual Web Developer when you add the Web reference. The proxy class itself is hidden from you. c) True. d) True. e) False. A CookieContainer also must be created on the client side if the client is not a Web browser. f) True. g) True.

22.2

a) SOAP message or SOAP envelope. b) System.Web.Services.WebService. c) code-behind. d) HTTP. e) Description. f) XML serialization. g) domain.

Exercises

22.3

(Phone Book Web Service) Create a Web service that stores phone book entries in a database (PhoneBook.mdf, which is provided in the examples directory for this chapter) and a client application that consumes this service. Give the client user the capability to enter a new contact (Web method AddEntry) and to find contacts by last name (Web method GetEnTRies). Pass only simple types as arguments to the Web service. Add a DataSet to the Web service project to enable the Web service to interact with the database. The GetEnTRies Web method should return an array of strings that contains the matching phone book entries. Each string in the array should consist of the last name, first name and phone number for one phone book entry. When configuring the PhoneBookDataSetTableAdapters.PhoneBookTableAdapter that provides access to the PhoneBook.mdf database, first set up the SELECT query that will be used by the GetEntries Web method. Then, when you add the INSERT statement to the TableAdapter, the TableAdapter Configuration Wizard will preconfigure the INSERT statement for you. You will simply need to name the PhoneBookDataSetTableAdapters.PhoneBookTableAdapter method that will perform the INSERT operation. The SELECT query that will find a phone book entry by last name should be:

SELECT LastName, FirstName, PhoneNumber FROM PhoneBook WHERE (LastName = @lastName)  

The INSERT statement that inserts a new entry into the PhoneBook.mdf database should be:

INSERT INTO [PhoneBook] ([LastName], [FirstName], [PhoneNumber]) VALUES (@LastName, @FirstName, @PhoneNumber)  

[Note: The TableAdapter Configuration Wizard adds the square brackets around each table name and column name by default when it configures the INSERT statement.]

22.4

(Phone Book Web Service Modification) Modify Exercise 22.3 so that it uses a class named PhoneBookEntry to represent a row in the database. The client application should provide objects of type PhoneBookEntry to the Web service when adding contacts and should receive objects of type PhoneBookEntry when searching for contacts.

22.5

(Blackjack Web Service Modification) Modify the blackjack Web service example in Section 22.5 to include class Card. Change Web method DealCard so that it returns an object of type Card. Also modify the client application to keep track of what cards have been dealt by using Card objects. Your Card class should include properties to determine the face and suit of the card.

22.6

(Airline Reservation Web Service Modification) Modify the airline reservation Web service in Section 22.6 so that it contains two separate Web methodsone that allows users to view all available seats, and another that allows users to reserve a particular seat that is currently available. Use an object of type Ticket to pass information to and from the Web service. The Web service must be able to handle cases in which two users view available seats, one reserves a seat and the second user tries to reserve the same seat, not knowing that it is now taken. To obtain the list of available seats, configure the SeatsTableAdapter with the SELECT query:

SELECT Number, Type, Class FROM Seats WHERE (Taken = 0)  

The names of the methods that execute this query should be FillWithAvailableSeats and GetAvailableSeats. To determine whether a specific seat is available, add the following SELECT query to the SeatsTableAdapter:

SELECT Number FROM Seats WHERE (Number = @number) AND (Taken = 0)  

The names of the methods that execute this query should be FillWithSpecificSeat and GetSpecificSeat. Finally, to reserve a specific seat, add the following UPDATE to the SeatsTableAdapter:

UPDATE Seats SET Taken = 1 WHERE (Number = @number) AND (Taken = 0)  

The name of the method that executes this UPDATE should be UpdateSeatAsTaken.

Категории