Creating a Simple Web Service

Because you might want to expose C++ code as Web services, Visual Studio .NET contains tools to help you build and deploy Web services.


To create and use the Web services described in this chapter, you will need to have Microsoft Internet Information Services (IIS) running on your machine. It’s also possible to write a Web service and then deploy it on another server, but doing so is outside the scope of this book.

This exercise will show you how to create and deploy a simple service that implements methods to convert temperatures between Fahrenheit and Celsius.

  1. Create a new project, and make sure that it’s an ASP.NET Web service project. Name it Converter.

    click to expand

  2. When you look at the project details, you’ll find an .asmx file as well as the more familiar C++ source files. The .asmx file contains details that tell the Web server which class contains the implementation of the Web service. Open the file, and take a look at the contents.

    <%@ WebService Class=Converter.ConverterClass %>

    The single line tells the Web server that the Converter Web service is implemented by Converter.ConverterClass, which indicates the class named ConverterClass in the Converter namespace. If you open the ConverterClass.h file, you’ll see the class and namespace defined there.

    The sample class in the Converter namespace currently has one method named HelloWorld, which shows you how to add methods to a WebService class.

  3. You can use HelloWorld to test a Web service, but let’s replace the function with a real one. Edit the class definition in ConverterClass.h to add the two conversion functions, making sure that you remove the dummy HelloWorld function. Notice also how I’ve added the WebService attribute to the class.

    namespace Converter { [WebService( Namespace="http://VCSBS/WebServices/", Description="A Web service for converting "  "temperatures between Fahrenheit and Celsius")] public __gc class ConverterClass : public WebService { // Constructor, Dispose, and Component-related code not shown public: [WebMethod(Description=  "Converts temperatures from Fahrenheit to Celsius")] double ConvertF2C(double dFahrenheit); [WebMethod(Description=  "Converts temperatures from Celsius to Fahrenheit")] double ConvertC2F(double dCelsius); }; }


    The Description of the WebServiceAttribute has been declared with string literals on two separate lines. If the C++ preprocessor sees two adjacent string literals, it will concatenate them, which provides an easy way to split long strings over more than one line.

  4. Web services are a very good advertisement for the use of attributes, and you can see here how attributes are used to add the necessary metadata to ordinary C++ classes and members. The WebService attribute provides a description and an XML namespace for the service that is used to distinguish it from other services on the Web that might have the same name. You can also use this attribute to supply a name if you want the service name to be different from that of the namespace and class.

    The WebMethod attribute shows that these two methods are exposed by the Web service.

  5. Implement the ConvertC2F and ConvertF2C functions by editing the ConverterClass.cpp file so that it looks like this:

    namespace Converter { double ConverterClass::ConvertF2C(double dFahrenheit) { return ((dFahrenheit - 32) * 5) / 9.0; } double ConverterClass::ConvertC2F(double dCelsius) { return (9/5.0 * dCelsius) + 32; } } 
  6. Build the project.

    The code will compile, and the Web service will automatically deploy on your Web server. You can verify that the service has been installed by looking at the Web server’s Inetpub\wwwroot directory, where you should see a virtual directory with the same name as the project.

And that’s all there is to it. Some Web services will obviously be more complicated, but creating a Web service can be as simple as adding some attributes to a class.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208 © 2008-2017.
If you may any questions please contact us: