Windows Azure Management API enables you to manage Azure subscription through codes. In this post, I will show,

how you could list hosted services inside your subscription through code ?

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

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 a class representing HostedService. There is property representing name of hosted service.


 public    class HostedServices
        {
            public string serviceName { 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

Note: I have uploaded debugmode.cer to my azure portal at subscription level.

3. Get the stream and convert response stream in string

clip_image002

You will get the XML response in below format,

clip_image004

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

clip_image002[6]

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

clip_image004[6]

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 post was useful. Thanks for reading.

Advertisements

2 thoughts on “Fetching Hosting Services Name 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 )

Google+ photo

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

Connecting to %s