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.

  1. Read here to create certificate for Azure subscription
  2. Read here to upload certificate

Very first let us create class representing Roles


public class RoleInstance
    {

        public string RollName { get; set; }

        public string InstanceName { get; set; }

        public string InstanceStatus { get; set; }
    }

Essentially you need to perform four steps,

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

image

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

image

3. Get the stream and convert response stream in string

image

You will get the XML response in below format,

image

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

image

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

image

For your reference full source code is as below,


using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Collections.Generic;
using System.IO;
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/ursubscriptionid
/services/hostedservices/yourhostedservicename?embed-detail=true");
            request.Headers.Add("x-ms-version:2009-10-01");
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;
                                                                            AccountName=debugmodetest9;AccountKey=dfkj");
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("debugmodestreaming");
            CloudBlob cloudBlob = cloudBlobContainer.GetBlobReference("debugmode.cer");

            byte[] byteData = cloudBlob.DownloadByteArray();
            X509Certificate2 certificate = new X509Certificate2(byteData);
            request.ClientCertificates.Add(certificate);
            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 + "RoleName").Value,
                                   InstanceName = r.Element(ns + "InstanceName").Value,
                                   InstanceStatus = r.Element(ns + "InstanceStatus").Value,

                               };

 foreach (var r in servicesName )
            {
                Console.WriteLine(r.InstanceName + r.InstanceStatus);
            }
 Console.ReadKey(true);

}
 }

I hope this post was useful. Thanks for reading  Smile

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Tagged with: , , , ,
Posted in AZURE
2 comments on “Fetching Web Roles details using Windows Azure Management API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

  • With people who participated in WCF day .. Great hosting by @CsharpCorner amd great leadership of @mcbkruse http://t.co/HkHjCdveJC 1 hour ago
  • RT @petekrueger: I've been using @Icenium. I would definitely recommend checking it out if you're tackling cross-platform mobile: http://t.… 1 hour ago
  • मुझे ग़म है की मैंने जिंदगी में कुछ नहीं पाया , ये ग़म दिल से निकल जाये अगर तुम मिलने आ जाओ 3 hours ago
  • RT @msigeek: If you love pictures and like seeing the work what we do, then do follow @iclickd; drop in a mention so, we can follow you bac… 3 hours ago
  • You are doing PROFESSIONAL SUICIDE , leaving this job ..., Yes you are right ? to get back my PERSONAL LIKE , I may have to commit this ~~ 3 hours ago
Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,123 other followers

%d bloggers like this: