.NET Web Services Solutions

Several of the previous web services have supported input parameters. In addition to using parameters for input data, a web service can also change a parameter’s value. The following Swap web service provides three methods that exchange the values of the two parameters:

string SwapString(byref string A, byref string B) integer SwapInteger(byref integer A, byRef integer B) double SwapDouble(byRef double A, byRef double B)

To create the Swap web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name Swap. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. 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:

    <WebMethod()> Public Function SwapString(ByRef A As String, _ Ä ByRef B As String) As String Dim C As String C = A A = B B = C SwapString = A End Function <WebMethod()> Public Function SwapDouble(ByRef A As Double, _ Ä ByRef B As Double) As Double Dim C As Double C = A A = B B = C SwapDouble = A End Function <WebMethod()> Public Function SwapInteger(ByRef A As Integer, _ Ä ByRef B As Integer) As Integer Dim C As Integer C = A A = B B = C SwapInteger = A End Function

  4. After you enter the code, select the Build menu Build Solution option to create the service.

To change the parameter values within the web service methods, programs must pass the parameters to the methods by reference. As you can see, within each method, the code simply exchanges the parameter values by using a temporary variable to hold the first parameter’s value during the swap operation.

You may be saying to yourself that swapping two parameters is an operation much better suited to a standard function and not a web service. You are correct. The Swap web service exists only to show you that web service methods can change parameter values that you pass to the service.

Putting the Swap Web Service to Use

The following Visual Basic .NET program, ExchangeParameters.vb, uses the Swap web service to switch the values of various parameters. When you run the program, it will display the starting values within a form, as shown in Figure 2.8. After you click the Swap Values buttons, the program will call the service to swap the values. If you repeatedly click the Swap Values button, the program will continue to exchange the values.

Figure 2.8: Exchanging parameter values within web service methods

To create the ExchangeParameters.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. 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 ExchangeParameters. 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).

  3. Using the Toolbox, drag and drop the button and text boxes previously shown in Figure 2.8 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/Swap/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. 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:

    Dim A As Integer = 0 Dim B As Integer = 100 Dim C As Double = 1.23456789 Dim D As Double = 9.87654321 Dim F As String = "ABCDE" Dim G As String = "ZYXVW" Private Sub Button1_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button1.Click Dim SwapObject As New localhost.Service1() Try SwapObject.SwapInteger(A, B) SwapObject.SwapDouble(C, D) SwapObject.SwapString(F, G) TextBox1.Text = A.ToString() TextBox2.Text = B.ToString() TextBox3.Text = C.ToString() TextBox4.Text = D.ToString() TextBox5.Text = F TextBox6.Text = G Catch Ex As Exception MessageBox.Show(Ex.Message) End Try End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Text = A.ToString() TextBox2.Text = B.ToString() TextBox3.Text = C.ToString() TextBox4.Text = D.ToString() TextBox5.Text = F TextBox6.Text = G End Sub

When the program first runs, the Form1_Load subroutine assigns values to the form’s text boxes. Then, each time the user clicks the Swap Values button, the handler uses the web service methods to swap the values of the string, double, and integer variables. The handler then assigns the results to the corresponding text boxes. Each time you click the Swap Values button, the code will exchange the values that appear within the text boxes.

Категории