How to build and use XML Web services by using Visual C++ .NET or Visual C++ 2005 (816153)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
For a Microsoft Visual Basic .NET version of this article, see 315935.
SUMMARYThis 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 servicesXML 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 serviceThis 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.
- In the Add member function dialog box, set
following the properties to add a new PythagoreanTheorem function:
- Function Name: PythagoreanTheorem
- Return Type: double
- parameter 1: double Num1
- parameter 2 : double Num2
- Access: public
- 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);
}
- 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);
- On the Build menu, click Build
Solution to build the Web service.
Use the Web serviceTo 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 Stuido .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 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.
- 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.
- 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
- In the Services section, click
PythagoreanTheoremWS, and then click Add
Reference.
Note The URL for PythagoreanTheoremWS is as follows:http://localhost/PythagoreanTheoremWS/PythagoreanTheoremWS.asmx - 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;
} - On the Debug menu, click
Start to run the application. The Console window displays the
following output:
5 13 25
Complete code listingPythagoreanTheoremWSClass.h
// PythagoreanTheoremWSClass.h
#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:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent()
{
}
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string "Hello, World!".
// 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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 Web site: PythagoreanTheoremWSClass.cpp
#include "stdafx.h"
#include "PythagoreanTheoremWSClass.h"
#include "Global.asax.h"
#include ".\pythagoreantheoremwsclass.h"
#include <cmath>
namespace PythagoreanTheoremWS
{
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string "Hello, World!".
// 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.
String __gc* PythagoreanTheoremWSClass::HelloWorld()
{
// TODO: Add the implementation of your Web service here
return S"Hello World!";
}
};
double PythagoreanTheoremWS::PythagoreanTheoremWSClass::PythagoreanTheorem(double Num1, double Num2)
{
double Result = (Num1 * Num1) + (Num2 * Num2);
return Math::Sqrt(Result);
}
REFERENCES For more information about Web services, visit the following Microsoft Web
sites:
Modification Type: | Major | Last Reviewed: | 1/26/2006 |
---|
Keywords: | kbIJW kbWebServices kbHOWTOmaster KB816153 kbAudDeveloper kbAudITPRO |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|