Saying “Hello World”

So let’s dive in and start coding!

We’re assuming you’ve installed Visual Studio 2012 and the latest Casablanca SDK. We’re using the bits published on Sep 24.

The code for this project is available at in the ‘HelloWorld’ directory.

This code is quite different from the default we talked about in the video, and reflects the evolution and maturing of the Casablanca SDK itself. In fact, the next version of the Casablanca SDK may have a different default application, reflecting further improvements in the SDK. We’ll do our best to keep the blog up-to-date!

Fire up Visual Studio and create a new Casablanca project…


[The older bits gave us a second dialog allowing us to choose a Web or Worker role, and configure those appropriately, but the current drop seems to blitz past that dialog. I will post an explanation as soon as I find one! –John]

…which gives us the default project.


We’ll walk through the code in a minute, but let’s run the project as is, and see what happens…


Boom! The code compiles and runs, but obviously fails. It is time to dissect the code and understand what’s going on inside.

Let’s look at the project first, and figure out what is going on there:

First of all, the C++ compiler needs to get some header information about the Casablanca classes…


…and the linker needs to include the Casablanca libraries…


Pay close attention to the SDK folder, as this may change with future releases, and you will need to update your projects as and when this happens.

[I don’t know if there is a system like ‘nuget’ or ‘apm’ for Visual C++ projects, which can consume a library and keep it up-to-date in a streamlined way. It sure sounds like a good way to release Casablanca bits –John]

The actual Casablanca library is dependent on the version of Visual Studio. For VS 11 aka VS 2012, it is called ‘casablanca110.lib’. The Actors library, analogously, is contained in ‘actors110.lib’. This will be different if you’re using VS 2010.


Similarly, the Project Configuration has a reference to a Platform Toolset, which is how the project is configured to build in a given version of Visual Studio.


The project we are building does not create an .exe file. Rather, we are writing a library, as highlighted above. Our DLL is loaded by a host which is part of the Casablanca system. When we run the project in Visual Studio 2012, we are actually running CasablancaHost110.exe, and specifying the DLL to load in the actors_config.cfg file.



Therefore, there is a Post-Build step to copy it from the project directory to the deployment target directory.


Now let’s go and get the application running:

The only material change that is required is that the endpoint needs to be specified. We could change

// Function to help find dynamically assigned port for our endpoint.
casablanca::string_t find_addr(const std::vector<casablanca::string_t> &configs)
    // The configuration data is a vector of [key1,value1,key2,value2,...,keyN,valueN] pairs.
    return U("http://*");


// Function to help find dynamically assigned port for our endpoint.
casablanca::string_t find_addr(const std::vector<casablanca::string_t> &configs)
    // The configuration data is a vector of [key1,value1,key2,value2,...,keyN,valueN] pairs.
    return U("http://localhost:8181");

and run successfully.


The functionality we’re demonstrating above is provided by the handler for the HTTP GET verb. We’re specifying that here., [](http_request message)
        message.reply(http::status_codes::OK, U("Hello World!"));
        actors::log::post(actors::LOG_INFO, U("Serviced a GET request for ") + message.request_uri().to_string());

What we are actually doing is providing the listener a call-back lambda which writes “Hello World” into the response of the current request.

Two key points for the future:

1) This lambda does not close over any local or instance variables, so it’s effectively a free-standing static method. We can use this information to build more complex mechanisms to respond to HTTP methods in the future

2) There are several overloads to message.reply(), and we’ll encounter a few more as we build other projects.

That’s it for now. I hope this post has provided a detailed explanation of the basic infrastructure of a Casablanca project, which helps you to understand what is going on better!

Happy Coding!


TechEd Australia 2012: AZR331

This is a reprise of the blog post following our talk at TechEd Australia 2012, where we summarise what we talked about.

We gave a talk at Tech Ed Australia this year titled Casablanca: C++ on Azure. The talk itself was slotted in at 8:15 am on the last day of Tech.Ed after a long party the night before. The crowd was small, and although we were initially disappointed by the turn out, we took heart in the fact that this was the most viewed online video at Tech.Ed this year – lots of five star ratings, Facebook likes and tweets.  This post gives you an introduction to Casablanca and highlights the things we talked about in the Tech.Ed presentation.

So, what is Casablanca? Casablanca is an incubation effort from Microsoft with the aim of providing an option for people to run C++ on Windows Azure. Until now, if you were a C++ programmer, the easiest option for you to use C++ would be to create a library and then P/Invoke it from C# or VB.NET code. Casablanca gives you an option to do away with things like that.

If you are a C++ developer and want to move your code to Azure right away, all we can say is “Hold your horses!” It is, like we said, an incubation effort and  not production ready, yet. But you can download it from the Devlabs site, play with it and provide valuable feedback to the product team.

You are also probably thinking, “Why use C++?” The answer to that question is really “Why not?” Microsoft has been providing developers the option to use various other languages/platforms such as java and Node.js to write for Azure, and now they are giving the same option to C++ programmers – use the language of their choice to write applications in Azure. Although there has been a bit of resurgence in C++ in the last couple of years, we are not really trying to sell C++ to you. If we are writing a Web App that talks to a database, then our first choice would probably still be ASP.NET MVC using C#, and maybe Entity Frameworks to talk to the DB. What we are trying to say is that you still need to use the right language and framework that works best for you, and if C# is the language you are comfortable with, then why change.

On the other hand if you are using C++, then you probably already know why you want to continue using it. You may be using it for cross-platform compatibility or better performance or maybe you have lots of existing legacy code that you can’t be bothered porting across. Whatever the reason, Casablanca gives you an option to bring your C++ code to Azure without having to use another language to talk to its libraries.

The Node influence

When you first start to look at Casablanca code, you will notice how some of the code has some resemblance to Node.js. A simple Hello World example in node will look like this –

var http = require('http');

http.createServer(function (request, response) {

                  {'Content-Type': 'text/plain'});
  respose.end('Hello World!');

}).listen(8080, '');

The equivalent Hello World in C++ would look something like this –

using namespace http;

    [](http_request request)    
        return request.reply(status_codes::OK, 
                      "text/plain", "Hello World!");

Notice the similarity? This isn’t by accident. The Casablanca team has been influenced a fair bit by Node and the simplicity by which you can code in node.

Other inclusions

The proliferation of HTML, Web servers, web pages and the various languages to write web applications based on HTML happened in the 90s. C++ may have been around a lot longer than that, but surprisingly, it didn’t ride the HTML wave. Web servers were probably written in C++, but the applications themselves were written using much simpler languages like PHP. Of course, we did have CGI, which you could write using C++, and there were scores of web applications written in C++ but somehow, it really wasn’t the language of choice for writing them. (It didn’t help that scores of C++ developers moved on to things like Java, C#, and Ruby). What C++ needed was a good library or SDK to work with HTTP requests, and process them.

In addition to this, RESTful applications are becoming common place, and is increasingly becoming the preferred way to write services. So, the ability to easily process GET, PUT, POST and DELETE requests in C++ was also needed.

When we talk about RESTful apps, we also need to talk about the format in which the data is sent to/from the server. JSON seems to be the format of choice these days due to the ease with which it works with Javascript.

The Casablanca team took these things into consideration and added classes into Casablanca to work with the HTTP protocol, easily create RESTful apps and work with JSON.

To process the different HTTP actions and write a simple REST application to do CRUD operations, the code will look something like this:

auto listener = http_listener::create(L"http://localhost:8082/books/");, [=](http_request request)
    //Read records from DB and send data back
});, [=](http_request request)
    //Create record from data sent in Request body
});, [=](http_request request)
    //Update record based on data sent in Request body
});, [=](http_request request)

/* Prevent Listen() from returning until user hits 'Enter' */ 
listener.listen([]() { fgetc(stdin); }).wait();

Notice how easy it is to process the individual HTTP actions? So, how does conversion from and to Json objects work? To convert a C++ object to a Json object and send it back as a response, the code will look something like this:

using namespace http::json;

value jsonObj = value::object();
jsonObj[L"Isbn"] = value::string(isbn);
jsonObj[L"Title"] = value::string(title);

request.reply(http::status_codes::OK, jsonObj);

To read json data from the request, the code will look something like this:

using namespace http::json;


value jsonValue = request.extract_json().get();

isbn = jsonValue[L"Isbn"].as_string();

You have a collection? no problem, the following code snippet shows how you can create a Json array

auto elements = http::json::value::element_vector();
for (auto i = mymap.begin(); i != mymap.end(); ++i) 
    T t = *i;

    auto jsonOfT = ...; // Convert t to http::json::value
    elements.insert(elements.end(), jsonOfT);
return http::json::value::array(elements);


Azure Storage

If you are running your application in Windows Azure, then chances are you may also want to use Azure storage. Casablanca provides you with the libraries to be able to do this. The usage, again is quite simple, to create the various clients for blobs, queues and tables the usage is as follow:

storage_credentials creds(storageName, storageKey);

cloud_table_client table_client(tableUrl, creds);
cloud_blob_client blob_client(blobUrl, creds);
cloud_queue_client queue_client(queueUrl, creds);

Notice the consistent way of creating the various client objects. Once you have initialized them, then their usage is quite simple too. The following code snippet shows you how to read data from Table storage:

cloud_table table(table_client, tableName);
query_params params;
auto results = table.query_entities(params)

for (auto i =  results.begin(); 
          i != result_vector.end(); ++i) 
    cloud_table_entity entity = *i;
    entity.match_property(L"ISBN", isbn);

Writing to Table storage is not difficult either, as seen in this code snippet:

cloud_table table(table_client, table_name);
cloud_table_entity entity(partitionKey, rowKey);

entity.set(L"ISBN", isbn, cloud_table_entity::String);


Writing to blobs, and queues follow a similar pattern of usage.


Another one of the main inclusions in Casablanca is the ability to do things in an asynchronous fashion. If you’ve looked at the way things are done on Windows Store applications or used Parallel Patterns Library (PPL), then you would already be familiar with the “promise” syntax. In the previous code snippets, we resisted the urge to use it, as we hadn’t introduced it yet. 

… and Client-Side Libraries

Also, we have been talking mainly about the server side use of Casablanca, but another thing to highlight is the fact that it can also be used to do client side programming. The following code shows the client side use of Casablanca and how promises can be used:

http::client::http_client client(L"http://someurl/");
client.request(methods::GET, L"/foo.html")
      [=](pplx::task task)
         http_response response = task.get();
         //Do something with response

If you need to find out more about ppl and promises, then you should read the article Asynchronous Programming in C++ written by Artur Laksberg in the MSDN magazine.

Wait, there is more…but first lets get started

Casablanca has also been influenced by Erlang, and the concept of Actors, but let’s talk about it another post. To get started with Casablanca, download it from the DevLabs site. It is available for both VS 2010 and 2012.



Basic Resources

You’ll need these links to be able to make the best of the Casablanca Preview

The TechEd Australia 2012 Talk

First things first – let’s share the video of the presentation and talk itself. Here is the link to the video on the Channel 9 website.

It’s about an hour long, and you get to share the experience of the 25 faithful people who braved the 8:15 am time slot on the day after the party!

Do send us (John) or (Mahesh) comments or requests if you want us to explain something we didn’t!

cout << “Hello World!” << endl;


We (John Azariah and Mahesh Krishnan) recently gave a talk at Tech Ed Australia this year titled Casablanca: C++ on Azure. This is an exciting new technology from Microsoft making C++ a first-class citizen in the Azure eco-system.

While it is still an incubation effort, it’s exciting to play with and use the technology hot off the press!

Watch this space for tips, tricks, samples and extensions to the Casablanca API! We’ll start with the talk itself, and walk through the samples we showed there, and go forward from there!

It’s going to be awesome!