Export (0) Print
Expand All

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

Visual Studio 2005

Developers can use the new C++ syntax to expose functionality written in C++ as an XML Web service. Using this syntax, 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 Visual Studio Hardware Requirements. You will also need sufficient permissions to be able to create XML Web Service projects on the computer where your Web server is.

Using the ASP.NET Web Service project template, Visual Studio automatically creates an XML Web service project that uses C++.

To create an ASP.NET Web Service project with 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.

    NoteTip

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

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.

NoteNote

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 OK.

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" %>
NoteTip

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, TempConvert3Class.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 ref 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.

  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 ^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.

  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.

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

  • Start 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 How to: Set Debug and Release Configurations.

In this walkthrough, you will place a breakpoint in the XML Web service and use the Start 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 How to: Debug XML Web Services in Managed Code.

Prior to debugging, verify your debug settings. For more information, see Debugging Preparation: XML Web Services (C++).

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

  1. On the Debug menu, select New Breakpoint, and then select Break at Function.

    From the New Breakpoint dialog box, type TempConvert3::TempConvert3Class::ConvertTemperature next to Function and click OK to place a breakpoint on the ConvertTemperature method declaration.

  2. On the Debug menu, click Start Debugging.

    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.

    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 TempConvert3Class Web Service page, click the ConvertTemperature link.

  4. 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 Debugger and Language Overview and Viewing Data in the Debugger.

  5. On the Debug menu, click Continue to continue processing.

  6. 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.

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

For more information, see Walkthrough: Debugging an 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 New and then click Project.

  2. From the Project types pane, expand the Other Project Types node and select Setup and Deployment. From the Templates pane, click Web Setup Project.

  3. In the Name box, type TempConvert3WebSetup.

  4. From the Solution drop down list, select Add to Solution, and then click OK.

    NoteNote

    By default, the installer uses the deployment project name when creating the virtual directory on your deployment server.

  5. In the left pane of the File System (TempConvert3WebSetup) tab, select Web Application Folder. For more information, see File System Editor.

  6. In Solution Explorer, right-click TempConvert3WebSetup, point to Add, and then click Project Output.

  7. In the Add Project Output Group dialog box, select Content Files. For more information, see How to: Add and Remove Project Outputs in the File System Editor.

  8. Click OK.

  9. In the left pane of the File System (TempConvert3WebSetup) tab, expand Web Application Folder and locate the folder labeled bin.

  10. Right-click the bin folder, point to Add, and then click Project Output.

  11. In the Add Project Output Group dialog box, select Primary output and Debug Symbols. For more information, see How to: Add and Remove 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.

  12. Click OK.

  13. 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:

Community Additions

ADD
Show:
© 2014 Microsoft