|
|
As the operations your web services perform become more complex, there will be many times when your service will return a structure that contains fields or an array of structures. The following ServerInfo web service returns a structure of type Server that contains specifics about the web server:
Structure Server Dim OS As String Dim DotNet As Boolean Dim SSL As Boolean Dim Administrator As String Dim WebServer As String End Structure
The ServerInfo web service supports one method, GetInfo, as shown here:
Server GetInfo()
To create the ServerInfo 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 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 ServerInfo. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
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:
Structure Server Dim OS As String Dim DotNet As Boolean Dim SSL As Boolean Dim Administrator As String Dim WebServer As String End Structure <WebMethod()> Public Function GetInfo() As Server Dim Result As Server Result.OS = "Windows" Result.DotNet = True Result.SSL = True Result.Administrator = "B. Gates" Result.WebServer = "IIS" GetInfo = Result End Function
After you enter the code, select the Build menu Build Solution option to create the service.
As you can see, the code simply assigns values to the structure variable and then returns the variable to calling program. Depending on your requirements, you might assign actual server values, similar to those contained in the actual .NET Server object.
The following Visual Basic .NET program, ShowSeverInfo.vb, uses the ServerInfo web server to retrieve specifics about a server. The program then displays the information within a form, as shown in Figure 2.13.
Figure 2.13: Using a web service method that returns a structure
To create the ShowServer.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 ShowServer. 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 text box previously shown in Figure 2.13 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/ServerInfo/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 Form1_Load(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles MyBase.Load Dim ServerObj As New localhost.Service1() Dim Server As localhost.Server Try Server = ServerObj.GetInfo() TextBox1.Text = "Server: " TextBox1.Text &= Server.OS & vbCrLf TextBox1.Text &= ".NET Support: " TextBox1.Text &= Server.DotNet.ToString() & vbCrLf TextBox1.Text &= "Admin: " TextBox1.Text &= Server.Administrator & vbCrLf TextBox1.Text &= "SSL support: " TextBox1.Text &= Server.SSL.ToString() & vbCrLf TextBox1.Text &= "Web Server: " TextBox1.Text &= Server.WebServer & vbCrLf TextBox1.Select(1, 0) Catch Ex As Exception TextBox1.Text = "Exception: " & Ex.Message End Try End Sub
To use a structure returned by a web service, the program must declare a variable to store the structure’s fields. Because the web service defines the structure’s format, the program declares a variable based on the structure type the service defines:
Dim Server As localhost.Server
In other words, the code creates an object it will use to interact with a service and also a variable that corresponds to the structure type the service returns:
Dim ServerObj As New localhost.Service1() ' Interact with service Dim Server As localhost.Server ' Structure defined by service
After the code calls the service, the code simply displays the structure’s contents (the structure fields) within a text box.
|
|