How Web Role talks to Worker Role in Azure : Queue
The storage services in Azure is accessible through REST API, which is awesome to allow other technology stack to make use of it. However, I don’t want to call the REST API myself and deal with the low level puts, responses code, etc.
At the moment, the closest thing to a wrapper for the REST API is the StorageClient project included in the Azure SDK Demo Project.
By default the project is located in c:\program files\windows azure sdk\1.0\sample.zip. Extract it and there’s a solution called StorageClient.

Putting stuff in into the queue in a Web Role
public class AspNetPage_In_AWebRole : Page
{
protected void SubmitButton(object sender, eventargs e)
{
// create queue from config
QueueStorage queueStorage = QueueStorage.Create(GetQueueStorageAccountFromConfiguration());
MessageQueue queue = queueStorage.GetQueue("workItems");
// usually wrap in a retry mechanism to make sure
// it handles the situation when storage service is down
queue.CreateQueue();
// add stuff into the queue
queue.PutMessage(new Message("work item"));
}
}
There’s 2 construct for Message which takes either string or bytes. So it’s pretty handy for most situation. If only we can use BinarySerializer on MediumTrust. Transporting objects along through queues would be a breeze.
Getting stuff out on the worker role
public class AWorkerRole : RoleEntryPoint
{
public override void start()
{
// create queue from config - exactly the same from webrole
QueueStorage queueStorage = QueueStorage.Create(GetQueueStorageAccountFromConfiguration());
MessageQueue queue = queueStorage.GetQueue("workItems");
queue.CreateQueue();
while(true)
{
// usually wrapped in some sort of error handling
// to maintain a perpetual loop
// get work item from the queue
Message msg = queue.GetMessage();
if (msg != null)
{
// do stuff
queue.DeleteMessage(msg);
}
else
{
// poll again later
Thread.Sleep(1000);
}
}
}
public override RoleStatus GetHealthStatus()
{
// maybe check the queue length
return RoleStatus.Healthy;
}
}
I wish I don’t have to do the polling myself. I feel uneasy about looking at while(true) and the Thread.Sleep() line. Hopefully Microsoft will be handling the poll queue scheduling by release.
About this entry
You’re currently reading “How Web Role talks to Worker Role in Azure : Queue,” an entry on Ronald Widha
- Published:
- 09.05.09 / 9pm
- Category:
- Articles

Comments
Jump to comment form | comments rss [?] | trackback uri [?]