April 19, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating and Using a Web Service in Managed C++

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

From Kate Gregory's Codeguru column, "Using Visual C++ .NET".

Web Services are a really exciting part of the .NET initiative - but they are bigger than .NET itself. The concept behind them is simple. Almost any web server you can name has some mechanism for executing server-side code: you type a URL into your browser, and something runs on the server and then writes out HTML in response to your request. It might be ASP, ASP.NET, servlets, even a five-year-old Perl script triggered through CGI. So imagine that the running code returns XML instead of HTML, and that it's called not because a user typed a URL into a browser, but because some code somewhere did a GET over HTTP to ask the web server for that URL. Now you've got an application-to-application connection happening. Just about every programming language has a class library that makes it easy to do a GET request over HTTP and some easy way of parsing XML, so this approach gives you a cross-platform, cross-programming-language, cross-vendor, cross-everything way to have code in one application call code on a completely different machine, as long as they're both on the Internet or otherwise connected.

That's the basic concept behind Web Services. But thinking of that code as a function instead of a page, how do you pass parameters to the function? What if it returns a complex data type? What if a web server has more than one web service on it? How can you find out the names, specify which service you want, and so on? This is where the capital letters are earned. There are standards being developed with names like Web Services Description Language (say wizdle to sound ultra-cool) that cover these technical issues for you. If you build a Web Service with Visual Studio.NET, it will meet these standards. And if you consume a Web Service, no matter how it was built, with Visual Studio.NET, you'll see how amazingly easy it can be to leverage someone else's code.

Writing a Web Service

To write a Web Service, you write a class with at least one method. The class is marked with a [WebService] attribute, and the method with a [WebMethod] attribute. A class might represent a customer, for example, and have a method to get the customer's shipping information, or to add a new order to the customer's order list. Web methods can take and return any variable type, including instances of objects you define. And they can do anything: persist data to and from a database, make any kind of calculation, even call another web method to help get the job done. For this example, I'm going to write a CalculatorService class with a method called Add that - you guessed it - adds two numbers and returns the total.

In Visual Studio.NET, I created a new project. From the Visual C++ project templates, I chose Managed C++ Web Service. I named the project Calculator. The code that is generated includes a class with the exciting name of Class1 - the first thing I did was change this name to CalculatorService in both the .cpp and .h files by hand, though I could have clicked on it in Class View and changed the name in the Properties Window. (I try to avoid having my class name and namespace name be the same, it can confuse Intellisense.) Whichever way I change the class name, I must also change it by hand in the file called Calculator.asmx, which I edit by double-clicking it in Solution Explorer. The edited Calculator.asmx looks like this:

<%@ WebService Class=Calculator.CalculatorService %>

I was given a method called HelloWorld() and it's simple enough to edit it into Add() - I just changed the name in both the .cpp and .h file, changed the signature so that it takes two floating-point numbers, and added code to return the total.

The class declaration ends up looking like this:

using <System.Web.Services.dll>

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

namespace Calculator
{
    public __gc 
        class CalculatorService : public WebService
    {
    public:
        [System::Web::Services::WebMethod] 
        double  Add(double x, double y);
    };
}

The implementation looks like this:

#include "stdafx.h"
#include "Calculator.h"
#include "Global.asax.h"

namespace Calculator
{
    double CalculatorService::Add(double x, double y)
    {
        return x + y;
    }
};




Page 1 of 2



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel