December 20, 2014
Hot Topics:

How to Keep Tab of your Windows Azure Hosted Services

  • December 16, 2011
  • By Vipul Patel
  • Send Email »
  • More Articles »

Introduction

Not only does Windows Azure provide a reliable platform to host and execute services, it even provides a set of Service management APIs to help create application which can be used to monitor these services. In this article, we will walk through the creation of a simple monitoring application which will list our Azure services running in the cloud.

Service Management APIs

The Windows Azure platform provides a set of REST-based APIs which can be used for managing services and deployments. The services offered can be used to control operations on storage accounts, hosted services, certificates, affinity groups, locations, and even track asynchronous requests.

When it comes to hosted services, the list of management APIs includes the following:

Let us now create a simple Windows application which will list hosted services.

Hands on

To start with, we will need to create a certificate which will need to use in our application. We can use makecert.exe which ships as part of Visual Studio SDK to create this certificate.

Execute the following command to create the certificate

makecert.exe -r -pe -a sha1 -n "CN=SampleCertificate" -ss My -len 2048 -sp "Microsoft Enhanced RSA and AES Cryptographic Provider" -sy 24 mycert.cer

This will create a certificate file in your current directory called mycert.cer.

Now, let's associate this certificate to our Azure subscription. Go to https://windows.azure.com and navigate to “Hosted Services, Storage Accounts & CDN Tab”. Click on “Management Certificates”.

image1

Click Add Certificate. A window will appear as under.

image2

Click Browse and navigate to the folder where the certificate was created in the step above.

Click OK and Windows Azure Management console will proceed to associate our certificate to our subscription.

Note the thumbprint of the certificate which will appear on the right side once the certificate is processed.

image3

Now, let's create our monitor application. We will use a simple console application as a template.

Start Visual Studio and create a new Windows Console Project called WindowsAzureMonitor.

Replace the boilerplate code in Program.cs to the following.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net; // For HttpWebRequest and HttpWebResponse classes.

using System.Security.Cryptography.X509Certificates; // For certificate-related classes.

using System.IO; // For Stream classes.

namespace WindowsAzureMonitor

{

class Program

{

static void Main(string[] args)

{

try

{

// When we created the certificate, it was automatically registered in the certificate store of the computer for the current user. Let us get the certificate from that certificate store.

X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);

certStore.Open(OpenFlags.ReadOnly);

 

// Find our certificate with the specified thumbprint we got from Windows Azure Management Console.

X509Certificate2Collection certCollection = certStore.Certificates.Find(

X509FindType.FindByThumbprint,

"BFDC86E134060A0C02391EFF98E5BE478EDF4594",

false);

// Close the certificate store.

certStore.Close();

// Check to see if a matching certificate was found.

if (0 == certCollection.Count)

{

throw new Exception("We did not find our certificate. Did you finish your certificate registration process successfully?");

}

X509Certificate2 certificate = certCollection[0];

// Create the request.

Uri requestUri = new Uri("https://management.core.windows.net/"

+ "3a686862-2f6c-4cd3-a69f-7d6b55bc12cc"

+ "/services/"

+ "hostedservices");

HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(requestUri);

// We need to add the certificate to the request.

httpWebRequest.ClientCertificates.Add(certificate);

 

// Specify the version information in the header of the request

httpWebRequest.Headers.Add("x-ms-version", "2011-10-01");

// Make the call using the web request.

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

if (httpWebResponse.StatusCode != HttpStatusCode.OK)

Console.WriteLine("We did not get OK as the HttpWebRequest Response Code");

// Parse the web response.

Stream responseStream = httpWebResponse.GetResponseStream();

StreamReader reader = new StreamReader(responseStream);

string response = reader.ReadToEnd();

// Let us strip down the response to filter the extra XML gunk we get

int serviceNameStartIndex = response.IndexOf(@"<ServiceName>", 0);

int serviceNameEndIndex = response.IndexOf(@"</ServiceName>", 0);

// Console.WriteLine("serviceNameStartIndex : " + serviceNameStartIndex);

// Console.WriteLine("serviceNameEndIndex : " + serviceNameEndIndex);

string serviceNames = response.Substring(serviceNameStartIndex + 13, serviceNameEndIndex - (serviceNameStartIndex + 13));

// Display the raw response.

Console.WriteLine("Listing serviceNames on your Azure account: " + serviceNames);

// Close the resources no longer needed.

httpWebResponse.Close();

responseStream.Close();

reader.Close();

}

catch

{

Console.WriteLine("We encountered an error executing. Did you follow the exact steps listed in the article?");

}

}

}

}

Note that for the code to work for your case, you will need to specify your Azure instance ID and your Certificate thumbprint which will be unique for you. You specify your Certificate thumbprint in the line below (highlighted in the code listing above).

// Find our certificate with the specified thumbprint we got from Windows Azure Management Console.

X509Certificate2Collection certCollection = certStore.Certificates.Find(

X509FindType.FindByThumbprint,

"BFDC86E134060A0C02391EFF98E5BE478EDF4594",

false);

Your Azure Subscription ID are specified in the line listed below (and highlighted above)

Uri requestUri = new Uri("https://management.core.windows.net/"

+ "3a686862-2f6c-4cd3-a69f-7d6b55bc12cc" // your Azure instance ID

+ "/services/"

+ "hostedservices"); // the operation you want to do.

When you execute the application, you should get your services listed.

For my case, here is the output I get

C:\Temp\cert>WindowsAzureMonitor.exe

Listing serviceNames on your Azure account: SuperAddService

This matches the services I have active on my account.

You now have a working application which can be considered a rudimentary monitoring application which lists your services. You can use other Management APIs to even check the state of your service.

Summary

In this article, we learned how to use Service Management APIs to keep tab of your application. I hope you have found this information useful.

About the author

Vipul Patel is a Software Engineer currently working at Microsoft Corporation. He is currently working in the Microsoft Lync team and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com


Tags: Windows Azure, tutorial, Azure Hosted Services, osted Services

Originally published on http://www.developer.com.


Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel