Guide To Creating CPP Web Services
Guide To Creating CPP Web Services
WHITE PAPER
Abstract
This whitepaper provides an introduction to creating C++ Web services and focuses on: Challenges involved in integrating C++ applications with Web services Benefits of using C++ Web services An example showing step by step how to create and implement a C++ Web service Advanced Web services features
Introduction
This paper describes how to develop and host high performance C++ Web services, including using advanced XML, SOAP, and WSDL productivity tools for transforming new and existing C++ applications for use in Service Oriented Architectures (SOA). The focus is on HydraExpress, part of the Rogue Wave Hydra Suite, which also includes the HydraSDO data components that implement the Service Data Object (SDO) specification, and HydraSCA, the first product available for deploying high-performance SOA applications based on the Service Component Architecture (SCA) specification. The SDO and SCA specifications are multi-language industry SOA specifications available in both Java and C++.
WHITE PAPER
PAGE 2
use of middleware creates a tightly coupled system by tying the C++ application to only that message bus and its ability to provide compatibility with the rest of the enterprise. This is not only time consuming and error prone, but reduces flexibility compared to Web services which, once exposed from the C++ application, can be easily linked to the rest of the enterprise.
WSDL
FEEDS INTO
d Hy
raE
s pre
s
PRODUCES
OY
EPL
OYS
TO
WHITE PAPER
PAGE 3
WHITE PAPER
PAGE 4
2. Set up the environment 3. Invoke the generator by passing it the WSDL file 4. Implement the service and client 5. Compile the service and client 6. Deploy the service 7. Run the service and client The WSDL file used in the example describes a Greeting Service that has a single operation (sayHello) which takes a string as input and returns a string as output. The WSDL file can be found in the HelloWorld tutorial that is available with the HydraExpress evaluation. Now to look at the steps in more detail: 1. Install the HydraExpress evaluation. Download the HydraExpress evaluation for Windows or Linux from http://www.roguewave.com/download-center/ and follow the setup instructions. 2. Set up the environment i. Ensure that your C++ compiler is set up in your environment and available in the PATH environment variable
ii. Set the JAVA_HOME environment variable to your JDK installation iii. On UNIX/Linux set the RWSF_HOME environment variable to the HydraExpress root directory iv. Run the script rwsfvars located in the HydraExpress root directory Further information on configuring the environment is available in Section 2.3 of the HydraExpress Users Guide which is provided with the HydraExpress evaluation. 3. Invoke the HydraExpress code generator by passing it the WSDL file i. Using a command prompt setup as described above, invoke rwsfgen passing it the name of the project to be created and the WSDL file name:
WHITE PAPER
PAGE 5
Figure1
Alternatively you can save the project settings in an xml project file, and pass this as the sole argument to rwsfgen. A sample project file is provided with the HelloWorld tutorial.
Figure2
WHITE PAPER
PAGE 6
4. Implement the service and client a) Implement the service Open the file HelloWorldExample\app\server\GreetingPortTypeImp.cpp. Edit the sayHello method so that it returns Hello + the string passed from the client.
std::string GreetingPortTypeImp::sayHello(rwsf::CallInfo& callInfo, const std::string& hellorequest_in) { return std::string(Hello + hellorequest_in); }
b) Implement the client Open the file HelloWorldExample\app\client\GreetingPortClient.cpp. Edit the invoke_sayHello method so that it calls the proxys sayHello method with a fixed input string of World !
void invoke_sayHello(GreetingBindingProxy& proxy) { std::string hellorequest_in(World!); std::string return_ret; rwsf::CallInfo callInfo; try { return_ret = proxy.sayHello(callInfo, hellorequest_in); std::cout << Server Response: << return_ret << std::endl; } catch(const rwsf::SoapFaultException& e) { std::cout << Fault Code: << e.getFault().getFaultcode().asString() << std::endl; std::cout << Fault String: << e.getFault().getFaultstring() << std::endl; } }
5. Compile the service and client Change to the HelloWorldExample directory and run nmake (Windows) or make (Unix/Linux) at the command prompt. This builds the HelloWorld client and service. 6. Deploy the service Run nmake deploy (Windows) or make deploy (Unix/Linux) to deploy the service.
WHITE PAPER
PAGE 7
7. Run the service and the client a) Start the HydraExpress agent using rwsfserver start
Figure3
A new command window appears showing the helloworld service has been loaded and is running (Loading context: /helloworld/)
Figure 4
WHITE PAPER
PAGE 8
b) Start the client GreetingPortClient from the HelloWorldExample\bin directory. The server responds.
Figure 5
WHITE PAPER
PAGE 9
Asynchronous messages. Any message pattern can be used either synchronously or asynchronously. This choice can be made at runtime by selecting either the synchronous or asynchronous service operation method. Support for sessions and other state-dependent processes. HydraExpress has built-in support for session management. In addition, the generalized support for metadata in headers can be adapted to support sessions, transactions, and other processes that depend on state. Support for XML binding. The HydraExpress code generator automatically compiles XML Schemas into C++ datatypes, whether the schema is embedded in the WSDL file, or the WSDL references an outside schema. Support for SOAP attachments. HydraExpress creates code to support MIME bindings that are defined in WSDL. Support for WSDL-defined faults. HydraExpress generates classes that encapsulate fault messages defined in the WSDL, and server code for capturing and returning fault messages to the client. An HTTPS transport. Accessible from the command line or built into the client, this transport provides for a secure client that uses HTTPS. Generated documentation. The code generator creates full, detailed documentation for all generated datatypes.
WHITE PAPER
PAGE 10
Summary
This paper details the benefits of C++ Web Services development and takes the reader step by step through a simple example. It also highlights the challenges faced when developing Web Services in C++ generally and illustrates how developers can save considerable development time and effort, while producing code that is robust and easily maintainable. Lastly, some advanced C++ Web Services features are discussed.
WHITE PAPER
PAGE 11
Copyright 2008, Rogue Wave Software, Inc. All Rights Reserved. Rogue Wave is a registered trademark of Rogue Wave Software, Inc. All other trademarks are the property of their respective owners.