|
|
||||||||||||||||||||
| Home > SOA News > Designing a VB.NET client for Web services | |
| SOA News: |
|
||
Roy Hoobler moderates our Enterprise Developer Forums -- post your question here. Creating Web services with Visual Studio.NET is a great way to get systems communicating with each other; either internally over the network or externally over the Internet. However, building a scalable OOP (object-oriented programming) client is just as important. This article discusses building an ASP.NET application that retrieves event information from a Web service.
Planning what will the client will do The client ASP.NET Web site will retrieve either a list of 'events' for a current date, or the same list filtered by a category. The client will also need to connect and get the category list as well.
Designing classes for a Web service From the description above, I can start to design what my objects are going to look like. In the Visual Basic class library, a class is needed to handle the request to the Web service (Listing 1). Each ASP.NET page could have contacted the Web service directly, but what would happen if our Web service provider changes a Web method parameter or goes out of business? Using a central place to connect to the Web service will require changes in just one class which is called WebServiceCalls.vb. In the class library, a reference to the Web service needs to be created. In most .NET projects, there is a folder in the Solution Explorer for "Web references" or "references". Right clicking on the folder and selecting "Add Web reference", brings up an interesting dialog where you can enter the URL to the Web service (for example: http://localhost/eventWS/eventWS.asmx). If successful, a list of available methods will appear. Adding the Web reference will allow you to call any of the methods listed. Once the object is instantiated as "...New mySite.com.eventWS()," all the Web methods are available to our class. The three methods in the WebServiceCalls.vb class mirror the three Web methods of the Web service. The ASP.NET and Windows application can then use this new VB class library. The three methods are: getCategoryList, getEventsByDateAndCategory and getEventListByDate. In the ASP.NET and in the test windows application, you need to add a project reference to the class library project. Since the category list is returned as XML, I created an XML utility class (Listing 2) that populates an array from XML and then uses the result to populate a Web Form Listbox control. Again, the objectives are to keep the XML code out of the ASP.NET page, build a reusable component and make sure this component can be tested individually. Before working with an ASP.NET project, it is better to test the classes locally, in the Windows application project. This saves a lot of time because configuring and connecting to a Web server is not necessary. Testing the above classes in an ASP.NET project would require remote debugging and the time it takes to build a simple test application is always less than remote debugging. This Windows application can also be improved to be a complete program testing how the classes actually work and if the Web service is running. Listing 3 displays a sample test function used in a Visual Basic.NET application.
Integrating the Web service classes with ASP.NET The ASP.NET page displays the information as expected and assigning values to the controls from the results works well. Fortunately, the search results come back as HTML from the Web service. If the results came back as an Array or XML, more functions could be added to the XMLData.vb classes, but the ASP.NET code would change very little. Listing 4 shows the index.aspx file and the code behind index.aspx.vb file.
The Final Results Get the code here (Listings 1-4).
For More Information
'); // -->
|
|
|||||||||||||||||||||||||
| About Us | Contact Us | For Advertisers | For Business Partners | Site Index | RSS |
|
|
|
|||||||