14.6. Writing the Client


To create a client that uses your new web service, create a new (normal) ASP.NET web site called StockPriceClient, as shown in Figure 14-6.

Your client will need knowledge of your web service. The easiest way to provide that knowledge is to create a web reference. Right-click on your project and choose Add Web Reference, as shown in Figure 14-7.

Figure 14-5. WSDL document


Figure 14-6. Creating client web site


The Add Web Reference dialog will open. If you have created the web service on the same machine as the client, click on "web services on the local machine," as shown in Figure 14-8.

If you have been using file-based web applications, you'll need to create a virtual directory in IIS to point to the web service, for this web reference to work.


Figure 14-7. Add Web Reference


Figure 14-8. Choosing local web service


Once you click on Service, the Add Web Reference dialog will locate the service (and bring up the test page). Give your service a reference name (a name by which you can refer to it in your code) and click Add Reference, as shown in Figure 14-9.

Figure 14-9. Name and Add Reference


When you click Add Reference, the web reference is added to your project, and reflected in the Solution explorer, as shown in Figure 14-10.

Figure 14-10. Client solution with web reference


Switch to Design view. Enter the words Stock Ticker and highlight them. Set them to Heading 1 and center them using the toolbar controls.

Move down on the page and enter the text Please enter a stock symbol:, then drag a text box into place. Name the text box txtStockSymbol. Add a button, name it btnSubmit, and set its text to Get Stock Info. Finally, add a label (lblMsg) and set its text to blank (see Figure 14-11).

Figure 14-11. Running the Stock Ticker Client


Double-click on the button to create an event handler. In the event handler, you'll get the text from txtStockSymbol and send it to the GetName and GetPrice methods of your service. You'll then format the response and put it in the output label, as shown in Example 14-5.

Example 14-5. Submit button Click event handler
 Protected Sub btnSubmit_Click( _ ByVal sender As Object, _ ByVal e As System.EventArgs) Handles btnSubmit.Click     Dim proxy As StockPriceService.Service = New StockPriceService.Service( )     Dim symbol As String = Me.txtStockSymbol.Text     Dim stockName As String = proxy.GetName(symbol)     Dim stockPrice As Double = proxy.GetPrice(symbol)     Me.lblMsg.Text = "The price of " & stockName & " is " & stockPrice.ToString( ) End Sub 

The result is that when you put in a stock symbol and press the Get Stock Info button, the methods of the web service are called, and values are returned to your client application for display, as shown in Figure 14-11.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

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