Changing the Hello Web Service

The Hello web service HelloWorld method returns a string that contains the message “Hello World.” In this section, you will change the method to return a string that contains the message “Hello, Web Service World.” To change the service, perform these steps:

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

  2. Within the Open Project dialog box, select the Hello web service project.

  3. Within the project’s code window, change the HelloWorld method to return the string “Hello, Web Service World” as shown here:

    <WebMethod()> Public Function HelloWorld() As String     HelloWorld = "Hello, Web Service World" End Function
  4. Select the Build menu Build Solution option to build the web service with your change in effect.

  5. Select the File menu Close Solution option to close the web service project.

Next, you will create the C# program DisplayHello.cs, which displays the message that the Hello web service returns within a text box, as shown in Figure 2.3.


Figure 2.3: Displaying the result of the Hello web service within a C# program

To create the DisplayHello.cs 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 C# Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields, type DisplayHello. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.

  3. Using the Toolbox, drag and drop the text box (make sure you set the text box to multiline) previously shown in Figure 2.3 onto the form.

  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/Hello/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 void Form1_Load(object sender, System.EventArgs e) {   localhost.Service1 WebServiceObject;   WebServiceObject = new localhost.Service1();   string Result;        try     {       textBox1.Text = WebServiceObject.HelloWorld();     }   catch (Exception Ex)     {       textBox1.Text = "Web Service Exception: " + Ex.Message;     } }

When the user first runs the application, the program will call the form’s Load handler, which in this case is named Form1_Load. Within the handler, the code creates an object the program will use to interact with the web service. Then, the code calls the service from within a try-catch block, assigning the method’s result to 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