Export (0) Print
Expand All
3 out of 8 rated this helpful - Rate this topic

Walkthrough: Accessing an XML Web Service Using Visual Basic or Visual C#

Visual Studio .NET 2003

The following walkthrough describes the process for accessing an XML Web service from an application created with Visual Basic or Visual C#.

During the course of this walkthrough, you will accomplish the following activities:

  • Create a client application using the ASP.NET Web Application project template.
  • Add a Web reference for an XML Web service.
  • Write code to access the XML Web service.
  • Run the Web application in debug mode.
  • Deploy the Web application.

To complete the walkthrough, you must provide the following:

Creating an XML Web Service Client Project

For this walkthrough, you will create a simple Web application that accesses the TempConvert1 XML Web service, which was the name given to the XML Web service created in Walkthrough: Creating an XML Web Service Using Visual Basic or Visual C#.

Note   To access a different implementation of the temperature conversion XML Web service, simply substitute the appropriate names where the TempConvert1 name appears throughout this walkthrough.

To create an ASP.NET Web application

  1. On the File menu, point to New, and then click Project to open the New Project dialog box.
  2. Expand either the Visual Basic Projects or the Visual C# Projects folder.
  3. Click the ASP.NET Web Application icon.
  4. Enter the address of the Web server on which you will develop the Web application and specify TempConvertClient1 as the directory name, such as "http://MyServer/TempConvertClient1". By default, the project uses your local machine, "http://localhost".
    Note   For some project types, the Name textbox is unavailable because specifying the location sets the project name instead. For example, Web applications and XML Web services are located on a Web server and derive their name from the virtual directory specified on that server.
    Note   You develop Web applications on a development server. By default, the development server is your local machine. Typically, you develop and build the project on a development server, and then you deploy it to another server (the deployment server) that hosts the Web application using a deployment project. However, if you are developing directly on the server that hosts the Web application, the development server and deployment server are the same.
  5. Click OK to create the project.
  6. From the Web Forms tab of the Toolbox, drag a Text Box, a Label, and a Button to the design surface of WebForm1.aspx and arrange them to your liking.
  7. Right-click the button you added, Button1, and click Properties on the shortcut menu. In the Properties window, set the Text property to Convert.
  8. Right-click the label you added, Label1, and click Properties on the shortcut menu. In the Properties window, clear the Text property to make this a blank label.

Adding a Web Reference

XML Web service discovery is the process by which a client locates an XML Web service and obtains its service description. The process of XML Web service discovery in Visual Studio involves interrogating a Web site following a predetermined algorithm. The goal of the process is to locate the service description, which is an XML document that uses the Web Services Description Language (WSDL). For more information, see XML Web Service Discovery.

The service description describes what services are available and how to interact with those services. Without a service description, it is impossible to programmatically interact with an XML Web service. For more information, see XML Web Service Description.

Your application must have a means to communicate with the XML Web service and to locate it at run time. Adding a Web reference to your project for the XML Web service does this by generating a proxy class that interfaces with the XML Web service and provides a local representation of the XML Web service. For more information, see Web References and Generating an XML Web Service Proxy.

To add a Web reference

  1. On the Project menu, click Add Web Reference.
  2. In the URL box of the Add Web Reference dialog box, type the URL to obtain the service description of the XML Web service you want to access, such as http://localhost/TempConvert1/Service1.asmx. Then click the Go button to retrieve information about the XML Web service.

    - or -

    If the XML Web service exists on the local machine, click the Web services on the local machine link in the browser pane. Then click the link for the TempConvert1 XML Web service from the list provided to retrieve information about the XML Web service.

  3. In the Web reference name box, rename the Web reference to ConvertSvc, which is the namespace you will use for this Web reference.
  4. Click Add Reference to add a Web reference for the target XML Web service. For more information, see Adding and Removing Web References.

    Visual Studio downloads the service description and generates a proxy class to interface between your application and the XML Web service.

Accessing the XML Web Service

Once you add a reference for the XML Web service to your project, the next step is to create an instance of the XML Web service's proxy class. You can then access the methods of the XML Web service in the same manner that you access any object's methods by calling methods in the proxy class. When your application calls these methods, the proxy class code generated by Visual Studio handles the communications between your application and the XML Web service.

First, you will create an instance of the XML Web service's proxy class. Next, you will take a value, provided in TextBox1, and make a call to the XML Web service's ConvertTemperature method using the proxy class. You will then display the value returned from the XML Web service in Label1.

To access the XML Web service

  1. Double-click the Convert button on WebForm1.aspx to create an event-handling method for this button and to display the code-behind file.
    Note   Solution Explorer hides code-behind files by default. Click Show All Files in the Solution Explorer toolbar to see hidden files. Alternatively, on the Project menu, click Show All Files.
  2. Enter the following code:
    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As Object, _
      ByVal e as EventArgs) Handles Button1.Click
        Dim ws As New ConvertSvc.Service1()
        Dim dFahrenheit As Double
        Dim dCelsius As Double
        Try
            dFahrenheit = Convert.ToDouble(TextBox1.Text)
            dCelsius = ws.ConvertTemperature(dFahrenheit)
            Label1.Text = dCelsius.ToString()
        Catch
            Label1.Text = "Conversion failed."
        End Try
    End Sub
    
    // C#
    protected void Button1_Click (System.Object sender, System.EventArgs e)
    {
       try
       {
          ConvertSvc.Service1 ws = new ConvertSvc.Service1();
          double dFahrenheit = Convert.ToDouble(TextBox1.Text);
          double dCelsius = ws.ConvertTemperature(dFahrenheit);
          Label1.Text = dCelsius.ToString();
       }
       catch
       {
          Label1.Text = "Conversion failed.";
       }
    }
    
    Note   The name of the XML Web service class generated when adding a Web reference may differ from the one shown above as Service1.
  3. Select WebForm1.aspx in Solution Explorer.
  4. On the Project menu, point to Web Project, and then click Set as Start Page.
  5. Save the solution.

For more information, see Accessing an XML Web Service in Managed Code.

Debugging the XML Web Service Client

Visual Studio offers several methods to build and run a Web application from the IDE, such as:

  • Start (with Debugging)
  • Start without Debugging
  • View in Browser

As a Visual Studio project, this Web application has separate configurations for Release and Debug versions. Since you created this project using the ASP.NET Web Application project template, Visual Studio automatically created these configurations and set the appropriate default options and other settings. For more information, see Debug and Release Configurations.

In this walkthrough, you will place a breakpoint in the Button1_Click event and use the Start (with debugging) method. For more information, see Debugging XML Web Services in Managed Code.

Prior to debugging, verify your debug settings. For more information, see Debugging Preparation: ASP.NET Web Applications.

To use a breakpoint and start the Web application with debugging

  1. On the Debug menu, click New Breakpoint.

    On the Function tab, type Button1_Click in the Function box and click OK to place a breakpoint on the Button1_Click event handler. For more information, see New Breakpoint Dialog Box.

  2. On the Debug menu, click Start.

    This command instructs Visual Studio to run the Web application in the debugger. Visual Studio builds the project and deploys it to the designated development server. Upon completion, the default browser launches and navigates to the .aspx file on the deployment server.

  3. On the Debug menu, click Stop Debugging, to stop running the Web application and return to the Code Editor.
  4. Once the page displays in the browser, type the number 212 in the text box and then click the Convert button.

    When processing reaches the Button1_Click event handler, processing stops. The Visual Studio debugger highlights the line containing the breakpoint and while halted, you can perform a variety of tasks. For more information, see Using the Debugger and Debugging Tools for Inspecting Your Program.

  5. On the Debug menu, click Continue to continue processing.
  6. The XML Web service responds by returning the converted value and the application sets the text of Label1 to 100.

    To stop running the Web application and return to the Code Editor, on the Debug menu, click Stop Debugging.

  7. On the Debug menu, click Clear All Breakpoints.

Deploying the Client

To make your Web application available to others, you must deploy it to a Web server that is accessible to the clients you wish to support. To deploy the Web application to a server other than the development server, you can either add a Web Setup project or copy the required files to the destination server. In this walkthrough, you can choose how to deploy this Web application. For more information, see Deploying Applications and Components.

To deploy the Web application using a Web Setup project

  1. On the File menu, point to Add Project, and then click New Project.
  2. Select the Setup and Deployment Projects folder, and then click Web Setup Project.
  3. In the Name box, type TempConvertClient1WebSetup, and then click OK.
    Note   By default, the installer uses the deployment project name when creating the virtual directory on your deployment server.
  4. In the left pane of the File System Editor, select Web Application Folder. For more information, see File System Editor.
  5. In Solution Explorer, right-click TempConvertClient1WebSetup, point to Add, and then click Project Output.
  6. In the Add Project Output Group dialog box, select Content Files, Primary output and Debug Symbols. For more information, see Adding and Removing Project Outputs in the File System Editor.
    • The Content Files group consists of the remaining files for the Web application, such as WebForm1.aspx and Web.config.
    • The Primary output group consists of the project DLL, TempConvertClient1.dll, and its dependencies.
    • The Debug Symbols group consists of the project PDB file, TempConvertClient1.pdb.
  7. Click OK.
  8. In Solution Explorer, right-click the TempConvertClient1WebSetup project, and then on the shortcut menu, click Build.

    This creates a Windows Installer file in the local project directory. Executing this file installs the Web application.

To deploy the Web application by copying the project

  1. In Solution Explorer, select the TempConvertClient1 project.
  2. On the Project menu, click Copy Project.
  3. In the Destination project folder box, enter the location to which you want to copy the project.
  4. Click either FrontPage or File share to select a Web access method. For more information, see Changing Web Project Access Methods.
  5. Click Only files needed to run this application.
  6. Click OK.

See Also

Creating and Accessing XML Web Services Walkthroughs | Visual Studio Walkthroughs | Web Application Security at Design Time in Visual Studio | Programming the Web with XML Web Services

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.