Daily Archives: December 9, 2010

Azure: a simple application

This is my first post here, about Azure programming. An easy start: an application with one web role, and one worker role:

You can download the solution from my AjCodeKatas Google project. The code is at:


In the initial web page you can enter a number to process:

If you send the number 10, this data is send to a queue:

protected void btnProcess_Click(object sender, EventArgs e)
    int number = Convert.ToInt32(txtNumber.Text);
    CloudQueueMessage msg = new CloudQueueMessage(number.ToString());

The worker role is reading the queue. It decrements the number, and if the result is still positive, it is reinjected in the queue:

        public override void Run()
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("NumberWorkerRole entry point called", "Information");
            CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            QueueUtilities qutil = new QueueUtilities(account);
            CloudQueue queue = qutil.CreateQueueIfNotExists("numbers");
            while (true)
                CloudQueueMessage msg = queue.GetMessage();
                if (msg != null)
                    int number = Convert.ToInt32(msg.AsString);
                    Trace.WriteLine(string.Format("Processing number: {0}", number), "Information");
                    if (number > 0)
                        CloudQueueMessage newmsg = new CloudQueueMessage(number.ToString());
                    Trace.WriteLine("Working", "Information");

You can see the output at Development Fabric UI:

Note the use of AzureLibrary to create a Queue:

        public CloudQueue CreateQueueIfNotExists(string queuename)
            CloudQueueClient queueStorage = this.account.CreateCloudQueueClient();
            CloudQueue queue = queueStorage.GetQueueReference(queuename);
            Trace.WriteLine("Creating queue...", "Information");
            Boolean queuecreated = false;
            while (queuecreated == false)
                    queuecreated = true;
                catch (StorageClientException e)
                    if (e.ErrorCode == StorageErrorCode.TransportError)
                        Trace.TraceError(string.Format("Connect failure! The most likely reason is that the local " +
                            "Development Storage tool is not running or your storage account configuration is incorrect. " +
                            "Message: '{0}'", e.Message));
            return queue;

I borrowed part of this code from Azure SDK samples.

Next steps to explore:

– Add instrumentation to worker role

– Use more instances, and generate more message (an explosion-like pattern)

– Add multithreading support in the worker role

– Example using table and blob storage

And the big ones:

– Inject and run AjSharp (or AjTalk) code at worker roles

– Implements a distributed application using roles (distributed genetic algorithm, distributed fractal or ray-tracer, montecarlo simulation, etc…)

Keep tuned!

Angel “Java” Lopez