|
|
Depending on the processing your web service performs, there may be times when you will need one web service to call another. A web service, like any other program, can call different web services that reside on the same or a different server.
Keep in mind, however, that the network messages and remote invocations add considerable overhead that will slow the processing. Simply put, the more web services an application calls, the slower the application will execute.
The following NestedCall web service takes advantage of the StockQuote service presented in Chapter 1. The NestedCall web service provides one method that returns the current price of Microsoft stock:
float GetMSFT()
To create the NestedCall web service, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click ASP.NET Web Service. Within the Location fields, type NestedCall. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s components.
Select the View menu Code option. Visual Studio .NET will display the service’s source code.
Before a web service can call a second web service, you must add a Web Reference to the remote service. To do so, select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type the URL of the WSDL file that describes the Web Service. In this case, type localhost/ExceptionDemo/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
Within the source code add the following program statements:
[WebMethod] public float GetMSFT() { net.xmethods.services.netxmethodsservicesstockquoteStockQuoteService Ä Quote; Ä Quote = new Änet.xmethods.services.netxmethodsservicesstockquoteStockQuoteService(); Ä try { return Quote.getQuote("MSFT"); } catch (Exception Ex) { return(-1); } }
After you enter the code, select the Build menu Build Solution option to create the service.
To use the StockQuote web service, the code creates an object it will use to interact with the service. Then, within a try-catch block, the code calls the service’s getQuote method, returning the result to the caller. If an exception occurs within the getQuote method call, the code will detect the exception and return the value -1 to the calling program.
The Visual Basic .NET program MSFTQuote.vb uses the NestedCall service to retrieve and display a stock quote for Microsoft as shown in Figure 2.16.
Figure 2.16: Using a web service that calls a second web service
To create the MSFTQuote.vb program, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields, type MSFTQuote. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).
Using the Toolbox, drag and drop the button and text box previously shown in Figure 2.16 onto the page.
Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type localhost/NestedCall/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code, add the following program statements:
Private Sub Button1_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button1.Click Dim Obj As New localhost.Service1() Try Dim Price As Single Price = Obj.GetMSFT() TextBox1.Text = Price Catch Ex As Exception TextBox1.Text = Ex.Message End Try End Sub
To use the NestedCall service, the code simply creates an object it will use to interact with the service and then, within a try-catch block, calls the service’s GetMSFT method. The code assigns the service’s result to the text box.
|
|