Hotel Broker Web Services (Case Study)

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 15.  Web Services


The next step in the case study is to make the Customer and Hotel components of the Hotel Broker available as a Web services. For simplicity, we implement two Web services:

  • CustomerWebService.asmx , which implements the ICustomer interface.

  • HotelInfoWebService.asmx , which implements the IHotelInfo interface.

These Web services are found in the WebServices subdirectory of the CaseStudy directory for this chapter.

At this point in your study, you have encountered many different variations of our Acme Travel Agency case study, and this chapter provides a good opportunity to investigate alternative configurations of essentially the same functionality. The CaseStudy folder contains a number of example programs. We highlight several programs in this section, which will illustrate a number of features of working with Web services.

graphics/codeexample.gif

We provide two versions of Customer.dll and Hotel.dll . These assemblies provide complete implementations of all the interfaces. The first version is in the folder LibraryCollection and provides an implementation using collections. This code was introduced in Chapter 6. The second version is in the folder LibraryDatabase and provides an implementation using databases, introduced in Chapter 13. Each folder contains a solution with three projects consisting of a console test program and two class libraries. If you prefer to avoid dealing with a database, you can use the collection version. You can easily switch back and forth between them in exercising Web services built upon them. The Bin directory for this chapter is used to hold these assemblies for use by Web services, and subdirectories are provided for saving the collection and database versions. For simplicity, the projects are arranged to build the assemblies in the top-level directories.

As mentioned, the Web services are provided by two .asmx files in the WebServices directory. You can exercise the Web services through Internet Explorer either by entering the proper HTTP addresses directly or by using the home page for this chapter.

The WebServicesProxies directory contains a project for building a proxy DLL for accessing the two Web services. This Visual Studio project makes use of Web References.

In the ConsoleClient subdirectory you will find a version of a console test program that uses the proxies assembly instead of the Customer and Hotel assemblies.

Since at this stage in the book you have a lot of experience with .NET, we do not spell out the details of building the various pieces of the case study.

Customer Web Service

To implement the customer Web service, we created a file, CustomerWebService.asmx (in the folder WebServices ), that uses the Customer component to implement the details of the Web service:

 graphics/codeexample.gif <WebService(Namespace:="http://www.oi.com/netvb")> _ Class CustomerWebService    Inherits System.Web.Services.WebService Private custs As Customers Public Sub New()    custs = New Customers() End Sub <WebMethod()> _ Public Function RegisterCustomer(_  firstName As String, lastName As String, _  emailAddress As String) As Integer    Dim customerId As Integer = _       custs.RegisterCustomer(firstName, lastName, _          emailAddress)    Return customerId End Function <WebMethod()> _ Public Sub UnregisterCustomer(customerId As Integer)    custs.UnregisterCustomer(customerId) End Sub <WebMethod(), _ XmlInclude(GetType(CustomerListItem))> _ Public Function GetCustomer(customerId As Integer) _  As ArrayList    Dim ar As ArrayList    ar = custs.GetCustomer(customerId)    Return ar End Function <WebMethod()> _ Public Sub ChangeEmailAddress(customerId As Integer, _  emailAddress As String)    custs.ChangeEmailAddress(customerId, emailAddress) End Sub 

The only new attribute is XmlInclude , which allows the XmlSerializer used to create the SOAP protocol to serialize a custom type, in this case CustomerListItem . (Without the attribute, you would get a runtime exception.) This attribute is found in the System.Xml.Serialization namespace. Nonetheless, if you examine the proxy class for this Web service, which is found in the WebServiceProxies directory, you will see that GetCustomer proxy ( customerproxy.cs ) returns only an array of objects.

 Public Function GetCustomer(ByVal customerId As Integer) _  As Object() 

Although the attribute instructs the serializer to save the custom type, the SOAP protocol understands only how to transmit a generic object type. So the ConsoleClient code using the proxy has to treat the return type as an object and then extract the custom type from it.

 graphics/codeexample.gif Private Sub ShowCustomerArray(  ByVal array() As Object  )    Dim cli As CustomerListItem    For Each cli In array    ... 

All the other array lists in the Customer and Hotel Web services are treated as arrays of objects where the appropriate type has to be extracted. Arrays that use types such as strings and integers, however, need no special treatment by the XmlSerializer.

HotelInfo Web Service

We do not provide a full-blown Web service interface to the Hotel Broker. Some features, such as adding or deleting hotels, would only be performed by administrators working for the hotel broker, and not by general people coming in over the Web. However, the IHotelInfo interface makes perfect sense to be exposed over the Web, and that is the interface we implement as a Web service.

To implement this Web service we created a file, HotelInfoWebService.asmx (in the folder WebServices ), that uses the Hotel component to implement the details of the Web service:

 graphics/codeexample.gif <WebService(Namespace:="http://www.oi.com/netvb")> _ Class HotelInfoWebService    Private broker As HotelBroker    Public Sub New()       broker = New HotelBroker()    End Sub    <WebMethod(), _    XmlInclude(GetType(HotelListItem))> _    Public Function GetHotels(city As String) As ArrayList       Dim ar As ArrayList       ar = broker.GetHotels(city)       Return ar    End Function    <WebMethod(  MessageName:="GetAllHotels"  ), _    XmlInclude(GetType(HotelListItem))> _    Public Function GetHotels() As ArrayList       Dim ar As ArrayList       ar = broker.GetHotels()       Return ar    End Function    <WebMethod()> _    Public Function GetCities() As ArrayList       Dim ar As ArrayList       ar = broker.GetCities()       Return ar    End Function End Class 

As was the case with the Customer Web service, we need XmlInclude attributes to enable the XmlSerializer to deal with custom types. Since Web service names have to be unique, we had to use the MessageName property of the WebMethod attribute to give one of the overloaded GetHotels methods a unique name .


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net