This documentation is archived and is not being maintained.

Walkthrough: Accessing an XML Web Service Using C++ and the CLR 

Visual Studio 2005

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

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

  • Create a client application using the CLR Console Application 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:

For this walkthrough, you will create a simple console application that accesses the TempConvert3 XML Web service, which was the name given to the XML Web service created in Walkthrough: Creating an XML Web Service Using C++ and the CLR.


To access a different implementation of the temperature conversion XML Web service, simply substitute the appropriate names where the TempConvert3 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 the Visual C++ node, and select CLR.

  3. Click the CLR Console Application icon.

  4. Change the name of the project to TempConvertClient3.

  5. Click OK to create the project.

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 How to: Generate an XML Web Service Proxy.

To add a Web reference

  1. From Solution Explorer, select the TempConvert3Client project node. 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/TempConvert3WebSetup/TempConvert3.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 TempConvert3 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 How to: Add and Remove 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.

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 TempConvertClient3.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 namespace System;
    int main(void)
        while (true)
                ConvertSvc::TempConvert3Class^ proxy =
                    gcnew ConvertSvc::TempConvert3Class();
                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: ");
            catch (System::FormatException^ e)
                Console::WriteLine("Invalid value...exiting: {0}", e->Message);
        return 0;

    The name of the XML Web service class generated when adding a Web reference may differ from the one shown above as TempConvert3Class.

  3. Save the solution.

  4. On the Build menu, click Build Solution.

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 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 CLR Console Application project template, Visual Studio automatically created these configurations and set the appropriate default options and other settings. For more information, see How to: Set Debug and Release Configurations.

In this walkthrough, you will place a breakpoint in the application and use the Start Debugging method.

Prior to debugging, verify your debug settings. For more information, see Debugging Preparation: Console Projects.

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 How to: Debug Code in the Editor.

  3. On the Debug menu, click Start Debugging.

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

  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 Debugger Roadmap and Viewing Data in the Debugger.

  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 Delete All Breakpoints.