Search code examples
azureazure-worker-rolesservicebus

Easiest way of getting the instance count of current Azure Cloudservice


I'm running this Azure Cloudservice.

To make sure I can leverage the Servicebus in a safe way, I have implemented the advice of this blog post: http://blogs.msdn.com/b/clemensv/archive/2012/07/30/transactions-in-windows-azure-with-service-bus-an-email-discussion.aspx

Instead of polling a database table constantly, I send a message to a queue to trigger a background process to look in the database and send the message.

But of course this alone, would not be safe, so I also have to periodically check the database on a schedule to make sure, I didn't miss anything.

But since I'm running multiple instances, I would like to spread this schedule, to make it more efficient, I thought I could do this, by getting the last integer from the Instance name. I know how to get this number using Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CurrentRoleInstance.Id

But I don't know how to get the total number of instances, I need that, in order to spread the schedule equally.

Does anybody know how to get this, without having to use Azure's management API's ?


Solution

  • Try Role.Instances property. Here's code from this page only to find all instances:

    foreach (RoleInstance roleInst in RoleEnvironment.CurrentRoleInstance.Role.Instances)    
    {
       Trace.WriteLine("Instance ID: " + roleInst.Id);    
    }