SIGN UP MEMBER LOGIN:    
ARTICLE

Fetching Web Roles details using Windows Azure Management API

Posted by Dhananjay Kumar Articles | Cloud Computing August 21, 2011
In this post, we will discuss the way to fetch below information about a Web Role or Worker Role using Windows Azure Management API.
Reader Level:

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.

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

    WebWinAzu2.gif
     
  3. Get the stream and convert response stream in string

    WebWinAzu3.gif

    You will get the XML response in below format,

    WebWinAzu4.gif

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

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

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/

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=

ysG4C6oCWq2B07V0DG7dbs/sStGySqLVLtAVkzr0L4bLN1ZEzltTpfC3h+VNR54cu7+
34ONADABF4dfGN1sL9Q=="
);
            
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.

Login to add your contents and source code to this article
share this article :
post comment
 

Helpful Article...............

Posted by Shalini Juneja Aug 22, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor