Microsoft KB Archive/816153

= How to build and use XML Web services by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 816153

Article Last Modified on 4/12/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 315935.



SUMMARY
This article describes how to create and how to use an XML Web service by using Visual C++ .NET or Visual C++ 2005.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET
 * Microsoft Internet Information Server 4.0 or a later verison

This article assumes that you are familiar with the following topics:
 * Microsoft Visual C++ 2005 or Microsoft Visual C++ .NET
 * Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET

XML Web services
XML Web services are reusable units of application logic that you can expose to clients across the Internet. Web services are platform-independent. Additionally, XML Web services are based on standards that the industry agrees upon such as XML, SOAP, and HTTP. Client applications can be any of the following:
 * Web-based ASP.NET application
 * Windows application
 * Pocket PC application
 * Mobile device application
 * Console application

XML Web services provides a new form of connectivity across your whole enterprise. By using Visual Studio .NET, you can easily create and use XML Web services.

Build a Web service
This section describes how to create an XML Web service that implements the Pythagorean theorem.  Create a new ASP.NET Web service in Visual C++ .NET or in Visual C++ 2005. To do this, follow these steps:  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click ASP.NET Web Service under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click ASP.NET Web Service under Templates. In the Name text box, type PythagoreanTheoremWS. Click OK.  In Class View, right-click the PythagoreanTheoremWS class, point to Add, and then click Add Function.</li> In the Add member function dialog box, set following the properties to add a new PythagoreanTheorem function: <ul> Function Name: PythagoreanTheorem</li> Return Type: double</li> parameter 1: double Num1</li> parameter 2 : double Num2</li> Access: public</li></ul> </li>  The Pythagorean theorem states that the square of the hypotenuse of a right triangle is equal to the sum of the squares of the other two sides. Open the PythagoreanTheoremWS.cpp file, and then add the following code in the PythagoreanTheorem function to implement the mathematical formula: double PythagoreanTheoremWS::PythagoreanTheoremWSClass::PythagoreanTheorem(double Num1, double Num2) {   double Result = (Num1 * Num1) + (Num2 * Num2); return Math::Sqrt(Result); } </li>  This function implements the Pythagorean theorem. However, the function is not yet a Web service method. To expose a function as a Web service method, add the WebMethod attribute to the function declaration. To do this, open the PythagoreanTheoremWS.h file, and then modify the function declaration. The function declaration appears as follows: [System::Web::Services::WebMethod] double PythagoreanTheorem(double Num1, double Num2); </li> On the Build menu, click Build Solution to build the Web service.</li></ol>

Use the Web service
To create a Windows application that uses the Web service, follow these steps:  Create a new Console Application (.NET) project in Visual C++ .NET or in Visual C++ 2005 to test the Web service that you created in the earlier section. To do this, follow these steps:  Start Visual Studio .NET or Visual Studio 2005.</li> On the File menu, point to New, and then click Project.</li> Click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates. Name the project MyWebClient.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Console Application under Templates.</li></ol> </li> <li>To access a Web service from a client application, the client must first include a reference to the Web service. To add a Web reference, right-click the project in Solution Explorer, and then click Add Web Reference.</li> <li>In the Add Web Reference dialog box, click Web Services on the Local Machine. Visual Studio .NET or Visual Studio 2005 searches the local computer for any available Web service. This may take several seconds. For more information about how to add a reference in Visual C++, click the following article number to view the article in the Microsoft Knowledge Base:

310674 How to add references to a managed Visual C++ project

</li> <li>In the Services section, click PythagoreanTheoremWS, and then click Add Reference.

Note The URL for PythagoreanTheoremWS is as follows:

http://localhost/PythagoreanTheoremWS/PythagoreanTheoremWS.asmx

</li> <li> Open the MyWebClient.cpp file, and then modify the main function as follows: int _tmain {   double hypotenuse = 0; localhost::PythagoreanTheoremWSClass ^ ws = gcnew localhost::PythagoreanTheoremWSClass;

// Pythagorean Triple: 3, 4, 5 hypotenuse = ws->PythagoreanTheorem (3,4); Console::WriteLine(hypotenuse);

//Pythagorean Triple: 5, 12, 13 hypotenuse = ws->PythagoreanTheorem(5, 12); Console::WriteLine(hypotenuse);

// Pythagorean Triple: 7, 24, 25 hypotenuse = ws->PythagoreanTheorem(7, 24); Console::WriteLine(hypotenuse);

Console::Read;

return 0; } </li> <li> On the Debug menu, click Start to run the application. The Console window displays the following output: <pre class="fixed_text">5

13

25 </li></ol>

PythagoreanTheoremWSClass.h
// PythagoreanTheoremWSClass.h


 * 1) pragma once

using namespace System; using namespace System::Web; using namespace System::Web::Services;

namespace PythagoreanTheoremWS {   /// WARNING: If you change the name of this class, you must change the ///         'Resource File Name' property for the managed resource compiler tool that is      ///          associated with all .resx files that this class depends on. Otherwise, ///         the designers cannot interact correctly with localized ///         resources that are associated with this form. public __gc class PythagoreanTheoremWSClass : public System::Web::Services::WebService {   public: PythagoreanTheoremWSClass {           InitializeComponent; }   protected: void Dispose(Boolean disposing) {           if (disposing && components) {               components->Dispose; }           __super::Dispose(disposing); }

private: ///        /// Required designer variable. ///        System::ComponentModel::Container * components;

///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        void InitializeComponent {       }

// WEB SERVICE EXAMPLE // The HelloWorld example service returns the string &quot;Hello, World!&quot;. // To test this Web service, make sure that the .asmx file in the deployment path is       // set as your Debug HTTP URL, in project properties. // and press F5.

public: [System::Web::Services::WebMethod] String __gc* HelloWorld;

// TODO: Add the methods of your Web service here [System::Web::Services::WebMethod] double PythagoreanTheorem(double Num1, double Num2); }; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

PythagoreanTheoremWSClass.cpp
namespace PythagoreanTheoremWS {   // WEB SERVICE EXAMPLE // The HelloWorld example service returns the string &quot;Hello, World!&quot;. // To test this Web service, make sure that the .asmx file in the deployment path is   // set as your Debug HTTP URL, in project properties. // and press F5.
 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;PythagoreanTheoremWSClass.h&quot;
 * 3) include &quot;Global.asax.h&quot;
 * 4) include &quot;.\pythagoreantheoremwsclass.h&quot;
 * 5) include

String __gc* PythagoreanTheoremWSClass::HelloWorld {

// TODO: Add the implementation of your Web service here

return S&quot;Hello World!&quot;; }

};

double PythagoreanTheoremWS::PythagoreanTheoremWSClass::PythagoreanTheorem(double Num1, double Num2) {   double Result = (Num1 * Num1) + (Num2 * Num2); return Math::Sqrt(Result); }

<div class="references_section">