Fetching Hosting Services Name Using Windows Azure Management API


TheWindows Azure Management API is a REST based API and allows you to perform almost all management level tasks with an Azure subscription.

Any client making a call to an Azure portal using a Management API has to authenticate itself before making a 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 a class representing HostedService. There is property representing name of hosted service.

        public  class HostedServices
        {
            public string serviceName { getset; }
        }


Essentially you need to perform four steps:

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

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

    calling version in Windows Azure

    Note: I have uploaded debugmode.cer to my azure portal at subscription level.
     
  3. Get the stream and convert response stream in string

    conversion in Windows Azure

    You will get the XML response in the following format:

    XML response in Windows Azure
     
  4. Once XML is there in form of string you need to extract ServiceName from XML element using LINQ to XML.

    LINQ to XML in Windows Azure

On running you should get all the hosted service. For me there is only one hosted service debugmode9 under my Azure subscription.

hosting services in Windows Azure

For your reference full source code is as below:

 using System;
 
using System.IO;
 
using System.Linq;
 
using System.Net;
 
using System.Security.Cryptography.X509Certificates;
 
using System.Xml.Linq; 
  
 
namespace ConsoleApplication26
 {
     
class Program
     {
         static void Main(string[] args)
         {            
            var request = (HttpWebRequest)WebRequest.Create("https://management.
core.windows.net/697714da-b267-4761-bced-b75fcde0d7e1/services/hostedservices"
);
            request.Headers.Add("x-ms-version:2009-10-01");
            request.ClientCertificates.Add
                            (X509Certificate2.CreateFromCertFile
                            (
@"D:\debugmode.cer"));
             var response = request.GetResponse().GetResponseStream();
             
var xmlofResponse = new StreamReader(response).ReadToEnd();
 
             XDocument doc = XDocument.Parse(xmlofResponse);
             XNamespace ns = "http://schemas.microsoft.com/windowsazure";
             var servicesName = from r in doc.Descendants(ns + "HostedService")
                                select new HostedServices
                                {
                                    serviceName = r.Element(ns + "ServiceName").Value
                                };           
             foreach (var a in servicesName)
             {
                 
Console.WriteLine(a.serviceName);
             }                   
             
Console.ReadKey(true);
         }
     }
 }


I hope this article was useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all