Passing Parameters to a Web Service

The methods in this chapter’s previous web services did not support parameters. However, as you learned in Chapter 1, web service methods can support a number of parameters of various types.

The following DayTimeGreeting service changes the GreetSpecific web service slightly to support an integer parameter that corresponds to the current hours of the day, from 0 to 23 (military time). Based on the current time, the service returns a message such as Good morning, Good afternoon, or Good evening. As before, the service provides several methods that return the messages in different languages:

string English(integer hour) string Spanish(integer hour) string French(integer hour) string German(integer hour)

To create the DayTimeGreeting 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 DayTimeGreeting. 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 English(ByVal Hour As Integer) _ Ä    As String         If (Hour < 12) Then             English = "Good Morning"         ElseIf (Hour < 18) Then             English = "Good Afternoon"         Else             English = "Good Evening"         End If     End Function     <WebMethod()> Public Function Spanish(ByVal Hour As Integer) _ Ä   As String         If (Hour < 12) Then             Spanish = "Buenos días"         ElseIf (Hour < 18) Then             Spanish = "Buonas tardes"         Else             Spanish = "Buonas noches"         End If     End Function

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

Within each method, the code examines the value of the Hour parameter and then returns a string that corresponds to the morning, afternoon, or evening. For simplicity, the web service supports only English and Spanish. However, by using one of the web-based translators discussed earlier in this chapter, you can extend the service to support additional languages.

Putting the DayTimeGreeting Web Service to Use

The ASP.NET page GreetWorld.aspx uses the DayTimeGreeting service to display a greeting in a specific language based on the current time. When you view the page, it will display a text box you can use to specify the current hours and a list you can choose to select a language. After you make your selections and click the Show Message button, the page will call the web service, displaying the result as shown in Figure 2.5.

click to expand
Figure 2.5: Using the DayTimeGreeting web service to display a time-based greeting in various languages

To create the GreetWorld.aspx page, 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 Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name GreetWorld. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the list box (adding the data shown), button, and text boxes previously shown in Figure 2.5 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/DayTimeGreeting/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 Button1_Click(ByVal sender As System.Object, _ Ä  ByVal e As System.EventArgs) Handles Button1.Click       Dim GreetObject As New localhost.Service1()       Dim Hour As Integer       Try         If (TextBox1.Text.Length = 0) Then           TextBox2.Text = "Please enter an hour (0 to 24)"         Else           Hour = TextBox1.Text           If (Hour < 1) Or (Hour > 24) Then             TextBox2.Text = "Please enter an hour (0 to 24)"           Else             If (ListBox1.SelectedIndex = 0) Then               TextBox2.Text =  GreetObject.English(TextBox1.Text)             ElseIf (ListBox1.SelectedIndex = 1) Then               TextBox2.Text = GreetObject.Spanish(TextBox1.Text)             Else               TextBox2.Text = "Please select a language"             End If           End If         End If       Catch Ex As Exception         TextBox2.Text = Ex.Message       End Try     End Sub

Within the button-click handler, the code creates an object the program will use to interact with the web service. Then, the program does some simple data-entry checking to make sure the user has selected a language and time of day. Then, the program calls either the English or Spanish method, displaying the result 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