Returning an Array of Values from a Web Service Method

Just as a web service method can receive an array of values as a parameter, a method can also return an array of values. The following Presidents web service returns an array of strings that contains the names of the U.S. presidents:

string() GetUS()

To create the Presidents 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 C# 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 Presidents. 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 string[] GetUS()       {          string[] Presidents = {"George Washington",          "John Adams", "Thomas Jefferson",          "James Madison", "James Monroe",          "John Quincy Adams", "Andrew Jackson",          "Martin Van Buren", "William Henry Harrison",          "John Tyler", "James K. Polk",          "Zachary Taylor", "Millard Fillmore",          "Franklin Pierce", "James Buchanan",          "Abraham Lincoln", "Andrew Johnson",          "Ulysses S. Grant", "Rutherford B. Hayes",          "James A. Garfield", "Chester A. Arthur",          "Grover Cleveland", "Benjamin Harrison",          "William McKinley", "Theodore Roosevelt",          "William Howard Taft", "Woodrow Wilson",          "Warren G. Harding", "Calvin Coolidge",          "Herbert Hoover", "Franklin D. Roosevelt",          "Harry S Truman", "Dwight D. Eisenhower",          "John F. Kennedy", "Lyndon B. Johnson",          "Richard M. Nixon", "Gerald R. Ford",          "Jimmy Carter", "Ronald Reagan",          "George Bush","Bill Clinton",          "George W. Bush" };          return Presidents;       }
  4. After you enter the code, select the Build menu Build Solution option to create the service.

As you can see, the method simply creates an array of character strings that contains the names of the presidents. The code then returns the array to the caller.

Putting the Presidents Web Service to Use

The following Visual Basic .NET program, UsePresidents.cs, displays a form that contains a text box. When you click the List Presidents button, the program will use the Presidents web service, which returns an array of strings that correspond to the names of the presidents. The program then uses a For Each loop to assign the names to the text box, as shown in Figure 2.12.


Figure 2.12: Using an array of values returned by a web service method

To create the UsePresidents.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 UsePresidents. 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 text box previously shown in Figure 2.12 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/Presidents/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:

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As  Ä   System.EventArgs) Handles MyBase.Load         Dim PresidentObject As New localhost.Service1()         Dim PresArray As String()         Dim President As String         Try             PresArray = PresidentObject.GetUS()             For Each President In PresArray                 TextBox1.Text &= President & vbCrLf             Next             TextBox1.Select(1, 0)         Catch Ex As Exception             TextBox1.Text = "Exception: " & Ex.Message         End Try     End Sub

When the program runs, the Form1_Load subroutine creates an object the code will use to interact with the web service. Then, the code calls the service’s GetUS method, assigning the result to an array of strings. The code then uses a For Each loop to place the name of each president within the text box. The code uses to TextBox.Select method to turn off selected text within the text box.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

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