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

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

Visual Studio .NET 2003

Developers can use Managed Extensions for C++ to expose functionality written in C++ as an XML Web service. Using Managed Extensions for C++, developers can create managed classes using C++. These classes and their public methods can then be exposed as XML Web services using .asmx files in the same manner as XML Web services in Visual Basic and Visual C#.

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

  • Create an XML Web service using the ASP.NET Web Service project template.
  • Implement the XML Web service.
  • Debug the XML Web service.
  • Deploy the XML Web service.

To complete the walkthrough, you will need access to a machine that meets the requirements for creating a Web project. For more information, see Creating Web Projects and Visual Studio .NET Software Requirements. You will also need sufficient permissions to be able to create XML Web Service projects on the computer where your Web server is. For details, see Web Application Security at Design Time in Visual Studio

Creating the XML Web Service Project

Using the ASP.NET Web Service project template, Visual Studio automatically creates an XML Web service project that uses Managed Extensions for C++. For more information, see Creating Projects with Managed Extensions for C++ and File Types Created for Managed Extensions for C++ Projects.

To create an ASP.NET Web Service project with Managed Extensions for C++

  1. On the File menu, point to New, and then click Project to open the New Project dialog box.
  2. Select the Visual C++ Projects folder, and then click the ASP.NET Web Service icon.
  3. In the Name box, type TempConvert3 and then click OK.

    The project template automatically provides the necessary files and references to support an XML Web service.

    Tip   In Solution Explorer see the ReadMe.txt file that describes the projects and files generated.

Implementing the XML Web Service

The next step is to write the code to implement the functionality of the XML Web service that clients can access. When the Web server receives a request for your XML Web service, it calls TempConvert3.asmx, which in turn calls this code.

Note   To view the contents of the .asmx file, in Solution Explorer, right-click the .asmx file and click Open With on the shortcut menu. In the Open With dialog box, select Source Code (Text) Editor and then click Open.

The .asmx file specifies the class that is the implementation of the XML Web service in the Class property of the .asmx file's WebService processing directive, which resembles the following:

<%@ WebService Class="TempConvert3.TempConvert3Class" %>
Tip   If you choose to rename the class, be sure to change the class name in the Class attribute of the WebService directive.

To add an XML Web service method

  1. Double-click the Component Designer's design surface to view the code for this file, TempConvert3.h.
  2. The methods of the TempConvert3Class class provide the implementation of the XML Web service. TempConvert3Class inherits from the System::Web::Services::WebService base class. Locate the class declaration:
    public __gc
             class TempConvert3Class : public System::Web::Services::WebService
    

    Insert the following code before that declaration:

    [System::Web::Services::WebServiceAttribute(
       Namespace="http://Walkthrough/XmlWebServices/",
       Description="A temperature conversion XML Web Service.")]
    

    Attaching the System::Web::Services::WebServiceAttribute attribute to this class makes it possible for you to include additional information about the XML Web service, such as specifying a namespace and a description. The description property of this attribute is included in the Service help page. For more information, see WebServiceAttribute Class.

  3. You will add a public method to this class and expose that method as part of the XML Web service. Locate the sample HelloWorld method:
    public:
       [System::Web::Services::WebMethod] 
          String __gc* HelloWorld();
    
          // TODO: Add the methods of your Web Service here
    

    Replace that method with the following:

    public:
       [System::Web::Services::WebMethod(
          Description="Converts temperatures from F to C.")]
          double ConvertTemperature(double dFahrenheit);
    

    Attaching the System::Web::Services::WebMethod attribute to this method indicates that you want the method exposed as part of the XML Web service. The description property of this attribute is included in the Service help page and the Service method help page. For more information, see WebMethodAttribute Class.

  4. In Solution Explorer, locate TempConvert3Class.cpp in the Source Files folder of the TempConvert3 project and open this file for editing.
  5. Locate and replace the contents of the TempConvert3 namespace with the following:
    namespace TempConvert3
    {
       double TempConvert3Class::ConvertTemperature(double dFahrenheit) {
          return ((dFahrenheit - 32) * 5) / 9;
       }
    };
    
  6. Save the solution.
  7. On the Build menu, click Build Solution.

Debugging the XML Web Service

Visual Studio offers several methods to build and run an XML Web service from the IDE, such as:

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

As a Visual Studio project, this XML Web service has separate configurations for Release and Debug versions. Because you created this project using the ASP.NET Web Service 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 XML Web service and use the Start (with Debugging) method. Once the XML Web service is running, you will attach to the system process in which it is running to debug it. 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 Services Created with Managed Extensions for C++.

To use a breakpoint and start the XML Web service with debugging

  1. On the Debug menu, click New Breakpoint.

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

  2. On the Debug menu, click Start.

    This command instructs Visual Studio to run the XML Web service as it would normally. 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.

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

    When you open an .asmx file in a browser, the XML Web service returns a Service helper page that provides information about the XML Web service. Clicking the Service Description link returns an XML document that contains the formal service description of the XML Web service. For more information, see XML Web Service Description.

  3. On the Debug menu, click Processes.
  4. Select Show system processes.
    Note   If you are debugging in a terminal server session, you must also select Show processes in all sessions.
  5. In the Available Processes pane, locate and select aspnet_wp.exe, which is the process that loads and executes your code.
  6. Click Attach.
  7. In the Attach to Process dialog box, select Common Language Runtime under Choose the project types you want to debug, then click OK.

    After you attach the debugger to the aspnet_wp.exe process, notice that Visual Studio adds it to the Debugged Processes pane of the Processes dialog box. For more information, see Attaching to a Running Program or Multiple Programs.

  8. Click Close.
  9. On the Service helper page, click the ConvertTemperature link.
  10. In the dFahrenheit box, type the number 212, and then click the Invoke button.

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

  11. On the Debug menu, click Continue to continue processing.
  12. The XML Web service responds by returning the converted value in an XML document resembling the following:
    <?xml version="1.0" encoding="utf-8" ?>
    <double xmlns="http://Walkthrough/XmlWebServices/">100</double>
    

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

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

For more information, see Walkthrough: Debugging a C# Web Service.

Deploying the XML Web Service

To deploy this XML Web service, you must create a deployment project.

To deploy the XML Web service 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 TempConvert3WebSetup, 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 TempConvert3WebSetup, point to Add, and then click Project Output.
  6. In the Add Project Output Group dialog box, select Content Files. For more information, see Adding and Removing Project Outputs in the File System Editor.
  7. Click OK.
  8. In the left pane of the File System Editor, expand Web Application Folder and locate the folder labeled bin.
  9. Right-click the bin folder, point to Add, and then click Project Output.
  10. In the Add Project Output Group dialog box, select Primary output and Debug Symbols. For more information, see Adding and Removing Project Outputs in the File System Editor.
    • The Primary output group consists of the project DLL, TempConvert3.dll, and its dependencies.
    • The Debug Symbols group consists of the project PDB file, TempConvert3.pdb.
  11. Click OK.
  12. In Solution Explorer, right-click the deployment project, and then on the shortcut menu, click Build.

    This creates a Windows Installer file in the local project directory. Execute this file on a Web server to install this XML Web service.

For more information, see Deploying Applications and Components.

Finally, to create a client application that accesses this XML Web service, see one of the following:

See Also

Creating and Accessing XML Web Services Walkthroughs | Visual Studio Walkthroughs | 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.