Visual Studio 2013 includes the C++ REST SDK version 1.0, also known as Casablanca. This Microsoft open source project is evolving in CodePlex, and takes advantage of the new set of capabilities introduced in C++ 11 to simplify cloud-based coding with a modern, asynchronous, and multi-platform API design. In this first article in a two-part series on this C++ REST SDK, I explain how you can use this SDK to consume REST services. In the next article, I’ll show how to use the SDK to retrieve and send JSON documents.
Understanding C++ REST SDK Architecture
When you need the best performance, you usually evaluate going native, and C++ is one of the best options for doing so. Microsoft believes C++ is valuable in the cloud; and the company’s new C++ REST SDK enables developers to work with C++ to consume REST services and achieve both great performance and scalability. It allows you to stay in C++ when consuming REST services or developing other code closely related to the cloud.
If you use C++ to consume cloud services but you use a C-based and synchronous API with callbacks, you aren’t taking full advantage of the improvements included in the latest C++ versions. In addition, your code will be difficult to read and debug, and the synchronous API will make it difficult for you to create a responsive UI. Most modern Web APIs try to reduce unnecessary boilerplate and so offer asynchronous methods without the complexity of C-style callbacks.
For example, if you work with C++ 11 but you use it to make calls to a synchronous C-based API to make an HTTP
GET call, your productivity levels cannot even be compared to other programming languages such as C# or Python. Microsoft developed the C++ REST SDK on top of the Parallel Patterns Library (PPL), and leverages PPL’s task-based programming model. Whenever you perform an asynchronous operation with the C++ REST SDK, you are creating a new PPL task. To make the C++ REST SDK portable to Linux, Microsoft made the necessary portions of PPL run on Linux (and compile cleanly with GCC). Thus, the C++ REST SDK uses a concurrency runtime for C++ that relies heavily on C++ 11 features. Instead of working with callbacks, you can write elegant C++ 11 code that creates tasks and schedules other tasks to be executed when certain other tasks finish execution. If you have previous experience with PPL, you will find it easier to work with the C++ REST SDK.
The C++ REST SDK relies on the following four low-level stacks or APIs that ride on top of the services provided by the different operating system (see Figure 1):
- WinHTTP: also known as Microsoft Windows HTTP Services. It is a C-based HTTP client API.
- PPL (short for Parallel Patterns Library): the programming model for composing asynchronous operations. The C++ REST SDK uses WinHTTP on different Windows versions.
- Boost.Asio: a cross-platform C++ library for network and low-level I/O programming that provides a consistent asynchronous model. The library uses a modern C++ approach. The C++ REST SDK uses Boost.Asio to manage communications on Linux.
- HTTP.sys: the Windows server-side API for HTTP. The C++ REST SDK uses HTTP.sys on different Windows versions.
Figure 1: The four low-level stacks used by the C++ REST SDK uses.