Export (0) Print
Expand All

Walkthrough: Accessing an XML Web Service Using Managed Extensions for C++

Visual Studio .NET 2003

The following walkthrough describes the process for accessing an XML Web service from an application created using the Managed Extensions for C++.

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

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

To complete the walkthrough, you must provide the following:

Creating an XML Web Service Client Project

For this walkthrough, you will create a simple console 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 a console application

  1. On the File menu, point to New, and then click Project to open the New Project dialog box.
  2. Expand Visual C++ Projects folder.
  3. Click the Console Application (.NET) icon.
  4. Change the name of the project to TempConvertClient2.
  5. Click OK to create the project.

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 needs 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 adds it to your project. Then it generates a proxy class and compiles a DLL from the service description. Finally, Visual Studio adds a new header file, WebService.h, to your project. This header file references the necessary namespaces of the .NET Framework for using XML Web services and the DLL it created. If you explore the local project directory, you will also find the Visual C# source file for the DLL.

Accessing the XML Web Service

After 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 the methods of the proxy class. When your application calls these methods, the code generated by Visual Studio handles the communications between your application and the XML Web service.

To access the XML Web service

  1. In Solution Explorer, locate TempConvertClient2.cpp in the Source Files folder and open this file for editing.
  2. Replace the contents of this file with the following:
    #include "stdafx.h"
    #include "WebService.h"
    #using <mscorlib.dll>
    using namespace System;
    int main(void)
    {
       while (true)
       {
          try
          {
             ConvertSvc::Service1* proxy = new ConvertSvc::Service1();
             Console::Write("Temperature in degrees Fahrenheit: ");
             System::String *sInput = Console::ReadLine();
             double dFahrenheit = System::Convert::ToDouble(sInput);
             double dCelsius = proxy->ConvertTemperature(dFahrenheit);
             Console::Write("Temperature in degrees Celsius: ");
             Console::WriteLine(dCelsius.ToString());
          }
          catch (System::FormatException *e)
          {
             Console::WriteLine("Invalid value...exiting");
             break;
          }
       }
       return 0;
    }
    
    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. Save the solution.
  4. On the Build menu, click Build Solution.

Debugging the XML Web Service Client

When you run the application, it accesses the XML Web service and displays the Celsius equivalent of the entered Fahrenheit temperature.

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

  • Start (with Debugging)
  • Start without Debugging

As a Visual Studio project, this application has separate configurations for Release and Debug versions. Because you created this project using the Console Application (.NET) 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 application and use the Start (with Debugging) method.

Prior to debugging, verify your debug settings. For more information, see Debugging Preparation: Console Applications Created with Managed Extensions for C++.

To use a breakpoint and start the application with debugging

  1. In the Code Editor, place the cursor on the line of code that calls the proxy function:
    double dCelsius = proxy->ConvertTemperature(dFahrenheit);
    
  2. Press F9 to place a breakpoint on this line of code.

    — Or —

    Click to the left of that line of code in the indicator margin.

    For more information, see Debugging Code in the Editor.

  3. On the Debug menu, click Start.

    This command instructs Visual Studio to run the application in the debugger. Visual Studio builds the project and launches the application.

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

  4. In the console window, type the number 212 and press ENTER.

    When processing reaches the breakpoint, 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.

    The application creates a SOAP request message, which it sends to the XML Web service. In return, the application receives a SOAP response message. The application then parses the response and displays a message indicating that 212 degrees Fahrenheit is the equivalent of 100 degrees Celsius.

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

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

See Also

Creating and Accessing XML Web Services Walkthroughs | Visual Studio Walkthroughs | Programming the Web with XML Web Services

Show:
© 2014 Microsoft