Create new Windows Azure Service and Get Deployment Info Sample Code using Windows Azure Service Management API

Here is C# sample code to access Windows Azure Services using Windows Azure Management API in C#. I have managed following two functions:

- To get deployment details for a specific Windows Azure Application on Windows Azure

 
  GetDeploymentDetails(UserSubscriptionID, MgmtCertThumbprint, UserServiceName, "", DeploymentType);

- To create a new service to host Windows Azure Application on Windows Azure

 
  CreateNewDeployment(UserSubscriptionID, MgmtCertThumbprint, UserServiceName, UserServiceLabelName);
 
 

First please be sure that you have a certificate installed in your development machine and same certificate is deployed to Windows Azure Portal in "Management Certificate" Section. 

Please check that you have certificate installed in your machine as below:

Then you can also verify that the certificate is also available in Windows Azure Management Portal as below:

Step 2: Now you can use the following code in your application (Be sure to input Subscription ID, Certificate Thumbprint, Service Name and Service Label in the code):

 using System.Text;
 using System.Security.Cryptography.X509Certificates;
 using System.Net;
 using System.IO;
 using System;
 
 
 namespace ManagementAPI
 {
 public class RequestState
 {
 const int BufferSize = 1024;
 public StringBuilder RequestData;
 public byte[] BufferRead;
 public WebRequest Request;
 public Stream ResponseStream;
 
 public RequestState()
 {
 BufferRead = new byte[BufferSize];
 RequestData = new StringBuilder(String.Empty);
 Request = null;
 ResponseStream = null;
 }
 }
 
 class Program
 {
 static void Main(string[] args)
 {
 string UserSubscriptionID = "PLEASE_PROVIDE_YOUR_SUBSCRIPTION_ID"; // Your subscription id.
 string MgmtCertThumbprint = "CERTIFICATE_THUMB_PRINT";
 string UserServiceName = "AZURE_SERVICE_NAME"; 
 string UserServiceLabelName = "AZURE_SERVICE_NAME_LABEL";
 string DeploymentType = "production"; // Use "production" or "staging"
 
 GetDeploymentDetails(UserSubscriptionID, MgmtCertThumbprint, UserServiceName, "", DeploymentType);
 
 CreateNewDeployment(UserSubscriptionID, MgmtCertThumbprint, UserServiceName, UserServiceLabelName);
 Console.ReadKey();
 }
 
 private static void GetDeploymentDetails(string subID, string certThumb, string hostedServiceName, string hostedServiceLabel, string deploymentType)
 {
 X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
 certificateStore.Open(OpenFlags.ReadOnly);
 X509Certificate2Collection certs = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, certThumb, false);
 if (certs.Count == 0)
 {
 Console.WriteLine("Couldn't find the certificate with thumbprint:" + certThumb);
 return;
 }
 certificateStore.Close();
 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
 new Uri("https://management.core.windows.net/" + subID + "/services/hostedservices/" + hostedServiceName + "/deploymentslots/" + deploymentType)); 
 request.Method = "GET";
 request.ClientCertificates.Add(certs[0]);
 request.ContentType = "application/xml";
 request.Headers.Add("x-ms-version", "2010-10-28");
 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 {
 // Parse the web response.
 Stream responseStream = response.GetResponseStream();
 StreamReader reader = new StreamReader(responseStream);
 
 // Display the raw response.
 Console.WriteLine("Deployment Details:");
 Console.WriteLine(reader.ReadToEnd());
 Console.ReadKey();
 
 // Close the resources no longer needed.
 responseStream.Close();
 reader.Close();
 }
 Console.ReadKey();
 }
 
 private static void CreateNewDeployment(string subID, string certThumb, string hostedServiceName, string hostedServiceLabel)
 {
 X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
 certificateStore.Open(OpenFlags.ReadOnly);
 X509Certificate2Collection certs = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, certThumb, false);
 if (certs.Count == 0)
 {
 Console.WriteLine("Couldn't find the certificate with thumbprint:" + certThumb);
 return;
 }
 certificateStore.Close();
 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
 new Uri("https://management.core.windows.net/" + subID + "/services/hostedservices"));
 request.Method = "POST";
 request.ClientCertificates.Add(certs[0]);
 request.ContentType = "application/xml";
 request.Headers.Add("x-ms-version", "2010-10-28");
 
 StringBuilder sbRequestXML = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
 sbRequestXML.Append("<CreateHostedService xmlns=\"https://schemas.microsoft.com/windowsazure\">");
 sbRequestXML.AppendFormat("<ServiceName>{0}</ServiceName>", hostedServiceName);
 sbRequestXML.AppendFormat("<Label>{0}</Label>", EncodeToBase64String(hostedServiceLabel));
 sbRequestXML.Append("<Location>Anywhere US</Location>");
 sbRequestXML.Append("</CreateHostedService>");
 
 byte[] formData =
 UTF8Encoding.UTF8.GetBytes(sbRequestXML.ToString());
 request.ContentLength = formData.Length;
 
 using (Stream post = request.GetRequestStream())
 {
 post.Write(formData, 0, formData.Length);
 }
 Console.WriteLine("Message: Hosted Service " + hostedServiceName + "creation successfull!");
 try
 {
 RequestState state = new RequestState();
 state.Request = request;
 IAsyncResult result = request.BeginGetResponse(new AsyncCallback(RespCallback), state);
 }
 catch (Exception ex)
 {
 Console.WriteLine("Error: " + ex.Message);
 }
 Console.ReadKey();
 }
 public static string EncodeToBase64String(string original)
 {
 return Convert.ToBase64String(Encoding.UTF8.GetBytes(original));
 }
 private static void RespCallback(IAsyncResult result)
 {
 RequestState state = (RequestState)result.AsyncState; // Grab the custom state object
 WebRequest request = (WebRequest)state.Request;
 HttpWebResponse response = 
 (HttpWebResponse)request.EndGetResponse(result); // Get the Response
 string statusCode = response.StatusCode.ToString();
 string reqId = response.GetResponseHeader("x-ms-request-id"); 
 Console.WriteLine("Creation Return Value: " + statusCode);
 Console.WriteLine("RequestId: " + reqId); 
 }
 
 }
 }
 

That's it!!