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 documentFigure 14-6. Creating client web siteThe 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.
Figure 14-7. Add Web ReferenceFigure 14-8. Choosing local web serviceOnce 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 ReferenceWhen 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 referenceSwitch 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 ClientDouble-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 handlerProtected 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. |