Steve Spencer's Blog

Blogging on Azure Stuff

Retrieving Role Instance Count in Azure From a Different Role

When using the following code from a worker role the trace information shows that there are one worker role instance and zero web role instances

public override void Run()
while (true)
Trace.WriteLine(string.Format("WorkerRole Instances {0}",

Trace.WriteLine(string.Format("WebRole Instances {0}",

This is because an internal endpoint is required on the role in order for the role environment to be able to retrieve the instance count. So add a new end point to the webrole and set it as internal. Running the code again, then shows both roles with 1 instance running.

See the Role.Instances MSDN topic: