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
1public override void Run()
2{
3 while (true)
4 {
5 Thread.Sleep(10000);
6 Trace.WriteLine(string.Format("WorkerRole Instances {0}",
7 RoleEnvironment.Roles\["WorkerRole1"\].Instances.Count),
8 "Information");
9
10 Trace.WriteLine(string.Format("WebRole Instances {0}",
11 RoleEnvironment.Roles\["WebRole1"\].Instances.Count),
12 "Information");
13 }
14}
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:
http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.role.instances.aspx