December 20, 2014
Hot Topics:

Creating and Using a Web Service in Managed C++

  • July 25, 2002
  • By Kate Gregory
  • Send Email »
  • More Articles »

If you've been following along, you can test the code by choosing Start, Debug. You can't really run a web service, but this starts a web browser and loads Calculator.asmx into it. This is the runs-on-the-web-server file that actually uses your class and methods. If you prefer, open a browser and enter the URL yourself:

http://localhost/Calculator/Calculator.asmx

If you have any trouble, make sure that your web server is started, and that your browser isn't going through a proxy server. On the proxy server, localhost is the proxy server, not your machine. You can follow the link for Add to see some generated documentation for the method, and even test it by entering numbers in the edit boxes and clicking the Invoke button. When you do that, another browser window opens with the result of your call, wrapped up in XML. For example I entered 3 and 2 in the edit boxes, and got this XML:

<?xml version="1.0" encoding="utf-8" ?> 
<double xmlns="http://tempuri.org/">5</double>

Since 3 plus 2 does equal 5, the web service appears to be working.

Using a Web Service

Writing a web service was pretty easy: you just needed an attribute on the class, an attribute on the method, and the calculator.asmx file, and all three were generated for you by Visual Studio. But using one is even easier. I created a Managed C++ application called CalcTest to get started. Before you can use a web service, you need to teach your project where to find it. I right-clicked the CalcTest project in Solution Explorer and chose Add Web Reference. The Add Web Reference dialog has an edit box where you can type a URL. You can also use the UDDI directories to find Web Services throughout the Internet or test web services on your own machine. The simplest thing to do is to enter the URL to Calculator.asmx and press Enter. You will be shown the same documentation you saw when you ran the web service project: click Add Reference to complete the process.

Once the reference has been added, calling a web service is just like using any C++ class. Adding the reference creates a header file that you can include wherever you want to use the web service. I replaced the line that printed Hello World with a line to create the object that represents my web service and another to use it. The edited CalcTest.cpp looks like this:

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>
#include "WebService.h"

using namespace System;

// This is the entry point for this application
int _tmain(void)
{
    CalculatorService * Calc = new CalculatorService;
    System::Console::WriteLine("1 plus 1 is {0}", 
                               __box(Calc->Add(1,1)));
    return 0;
}

(If the __box keyword doesn't ring a bell, check my previous column on boxing and unboxing fundamental types.) When this application runs, it prints out, not surprisingly I hope:

1 plus 1 is 2

And that's how simple it is to use a web method from within your own code. If you're wondering what happened to the XML and why you didn't have to parse the number out from inside it - that's just one of the cool things that Visual Studio took care of for your when you added the web reference.

The possibilities for this are endless. Any code that can run on your server can, if you want, be accessed by other code through the Internet. Security, authentication, encryption and so on are all available to you and supported by SOAP, one of the standards involved in web services. Since those .asmx files are in fact part of ASP.NET, everything you know about ASP.NET pages applies to web services in .NET as well. Why not try your hand at a little application-to-application integration today?

About the Author

Kate Gregory is a founding partner of Gregory Consulting Limited (www.gregcons.com). In January 2002, she was appointed MSDN Regional Director for Toronto, Canada. Her experience with C++ stretches back to before Visual C++ existed. She is a well-known speaker and lecturer at colleges and Microsoft events on subjects such as .NET, Visual Studio, XML, UML, C++, Java, and the Internet. Kate and her colleagues at Gregory Consulting specialize in combining software develoment with Web site development to create active sites. They build quality custom and off-the-shelf software components for Web pages and other applications. Kate is the author of numerous books for Que, including Special Edition Using Visual C++ .NET.

# # #





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel