Fetching Web Roles details using Windows Azure Management API

If you are writing some tool to manage Windows Azure portal then fetching information about Roles may be frequent requirement for you.

In this post, we will discuss the way to fetch below information about a Web Role or Worker Role using Windows Azure Management API.

  1. RoleName
  2. InstanceName
  3. InstanceStatus

Any client making call to Azure portal using Management API has to authenticate itself before making call. Authenticating being done between Azure portal and client calling REST based Azure Management API through the certificates.

Read here to create certificate for Azure subscription

Read here to upload certificate

Very first let us create class representing Roles

    public class RoleInstance

        public string RollName { getset; }

        public string InstanceName { getset; }

        public string InstanceStatus { getset; }

Essentially you need to perform four steps,

  1. You need to create a web request to your subscription id.

  2. While making request you need to make sure you are calling the correct version and adding the cross ponding certificate of your subscription.

  3. Get the stream and convert response stream in string


    You will get the XML response in below format,


    In returned XML all the Roles and their information's are returned as below,

  4. Last step is to parse using LINQ to XML to fetch details of Role

For your reference full source code is as below,

using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;

namespace ConsoleApplication1

 class Program
static void Main(string[] args)

var request = (HttpWebRequest)WebRequest.Create("https://management.core.windows.net/

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse


CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference

CloudBlob cloudBlob = cloudBlobContainer.GetBlobReference("debugmode.cer");

byte[] byteData = cloudBlob.DownloadByteArray();
X509Certificate2 certificate = new X509Certificate2(byteData);
var response = request.GetResponse().GetResponseStream();
var xmlofResponse = new StreamReader(response).ReadToEnd();
//XDocument doc = XDocument.Parse(xmlofResponse);
XElement el = XElement.Parse(xmlofResponse);
XNamespace ns = "http://schemas.microsoft.com/windowsazure";
var servicesName = from r in el.Descendants(ns + "RoleInstance")
select new RoleInstance 
                                   RollName  = r.Element(ns + 
                                   InstanceName = r.Element(ns + 
                                   InstanceStatus = r.Element(ns + 

 foreach (var r in servicesName )
Console.WriteLine(r.InstanceName + r.InstanceStatus);


I hope this post was useful. Thanks for reading.

Up Next
    Ebook Download
    View all
    View all