Export (0) Print
Expand All

Walkthrough: Accessing an XML Web Service Using C++

Visual Studio .NET 2003

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 Win32 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 an XML Web service client 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++ Projects folder, and then click the Win32 Project icon.
  3. Change the name of the project to TempConvertClient3.
  4. Click OK to launch the Win32 Application Wizard.
  5. In the Win32 Application Wizard, do the following:
    1. On the Application Settings page, click Console application.
    2. Select ATL to add ATL library support.
    3. Click Finish.

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 or contract, 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. 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.

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 generates a new header file but does not add it to the project. However, it does save the generated header file to the project folder. The name of the header file depends on the XML Web service you chose to access. This header file is re-generated whenever you build the project. To obtain the correct name of the header file, examine the contents of the Output Window or the Output Window section of the build log (BuildLog.htm in the intermediate files directory). The correct filename appears on a line that resembles the following: /out:Service1.h. To view the Output Window, on the View menu point to Other Windows and click Output.

For more information, see Adding and Removing Web References.

Accessing the XML Web Service

After your project has a reference to the XML Web service, the next step is to create an instance of the proxy class. For more information, see Creating XML Web Service Clients.

You can 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. For more information, see Creating XML Web Service Clients.

To access the XML Web service

  1. In Solution Explorer, locate stdafx.h in the Header Files folder and open this file for editing.
  2. Add the following #define statements (in bold) after the existing #define WIN32_LEAN_AND_MEAN statement:
    #define WIN32_LEAN_AND_MEAN
    // Disable client timeout for testing. Default is 10000 milliseconds.
    #define ATL_SOCK_TIMEOUT INFINITE
    // Minimum system requirements are Windows 98, Windows NT 4.0, or later
    #define _WIN32_WINDOWS 0x0410
    
  3. Add the following #include statements (in bold) after the existing #include for <atlbase.h>:
    #include <atlbase.h>
    #include "Service1.h"
    #include <conio.h>
    #include <iostream>
    
    Note   The name of the header file generated when adding a Web reference may differ from the one shown above as Service1.h. To obtain the correct name of the header file, examine the contents of the Output Window or the Output Window section of the build log (BuildLog.htm in the intermediate files directory). The correct filename appears on a line that resembles the following: /out:Service1.h. To view the Output Window, on the View menu point to Other Windows and click Output.
  4. In Solution Explorer, locate TempConvertClient3.cpp in the Source Files folder and open this file for editing.
  5. First, you will create an instance of the XML Web service's proxy class. Next, you will take a value, which the console provides, and make a call to the XML Web service's ConvertTemperature method by calling the method in the proxy class. You will then display the value returned by the XML Web service in the console. Enter the following code:
    Note   The following code uses a class named Service1::CService1. Refer to the generated header file in your project folder to determine the proper class name for the XML Web service and substitute it where Service1::CService1 appears in the code below.
    #include "stdafx.h"
    void AccessService(){
       using namespace std;
       double dFahrenheit = 0.0;
       double dCelsius = 0.0;
       Service1::CService1 ws;
       while (1){
          cout << "Enter a temperature in degrees Fahrenheit: ";
          cin >> dFahrenheit;
          if (!cin.good()){
              cout << "Not a temperature" << endl;
              break;
          }
          HRESULT hr = ws.ConvertTemperature(dFahrenheit, &dCelsius);
          if (SUCCEEDED(hr))
              cout << dFahrenheit << " F = " << dCelsius << " C\n" << endl;
          else
             cout << "An error occurred: " << hr << endl;
       }
    }
    
    int _tmain(int argc, _TCHAR* argv[]){
       if (SUCCEEDED(CoInitialize(NULL))){
          AccessService();
          CoUninitialize();
       }
       return 0;
    }
    
  6. Save the solution.
  7. 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 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 Win32 Project 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: Visual C++ Console Applications.

To use a breakpoint and start the application with debugging

  1. On the Debug menu, click New Breakpoint.

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

  2. On the Debug menu, click Start.
    Tip   F5 is the default keyboard shortcut for Start.

    This command instructs Visual Studio to run the application in the debugger. Visual Studio builds the project and deploys it to the designated development server. Upon completion, the default browser displays the .asmx file from the deployment server.

  3. To stop running the XML Web service and return to the Code Editor, close the browser.

    — Or —

    On the Debug menu, click Stop Debugging.

    Tip   SHIFT+F5 is the default keyboard shortcut for Stop Debugging.
  4. In the console window, type the number 212 and press ENTER.

    When processing reaches the AccessService function, 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.
    Tip   F5 is the default keyboard shortcut for Continue.

    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.
    Tip   Ctrl+Shift+F9 is the default keyboard shortcut for Clear All Breakpoints.

See Also

Creating and Accessing XML Web Services Walkthroughs | Visual Studio Walkthroughs | Programming the Web with XML Web Services | Providing Data Interoperability Between Visual C++ and .NET Framework Applications

Show:
© 2014 Microsoft