Create Hosted Service using Windows Azure Management API

In this post I will discuss, how could we create Hosted Service in Windows Azure using Windows Azure Management API?

To create Hosted Service you need to perform POST operation on the below URI. You need to change subscription id with your subscription Id.

https://management.core.windows.net/<subscription-id>/services/hostedservices

While making the request, Request Header must contain information as below,

image

Request Body must contain XML as body. XML elements should be in the same order as mentioned below.


<?xml version="1.0" encoding="utf-8"?>
<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure">
<ServiceName>service-name</ServiceName>
<Label>base64-encoded-service-label</Label>
<Description>description</Description>
<Location>location</Location>
<AffinityGroup>affinity-group</AffinityGroup>
</CreateHostedService>

On successful creation of the hosted service status code 201 will get returned. Response contains no body.

For more details read MSDN reference here

To create hosted service I have created below function. To create service you need to pass below parameters

  1. Service Name : This is name of the service you want to create
  2. Label : This is service label
  3. Description : This is service description
  4. Location : This is data center in which service will get deployed


public  String CreateHostedService(String subscriptionId, String serviceName, String label, String description, String location)
{


XDocument payload = CreatePayloadToCreateService(serviceName, label, description, location);
string operationUri = "https://management.core.windows.net/"+subscriptionId+"/services/hostedservices";
String requestId = Invoke(payload,operationUri);
return requestId;
}



In above function I am calling two different functions.

  1. One method to create payload. This payload will be passed as XML to perform POST operation
  2. Invoke method will perform actual HTTP request to Windows Azure portal.

Below function will create payload and return as XML. LINQ to XML is being used to create payload XML.


private XDocument CreatePayloadToCreateService(String serviceName, String label,String description, String location)
{
String base64LabelName = ConvertToBase64String(label);
XNamespace wa = "http://schemas.microsoft.com/windowsazure";
XElement xServiceName = new XElement(wa + "ServiceName", serviceName);
XElement xLabel = new XElement(wa + "Label", base64LabelName);
XElement xDescription = new XElement(wa + "Description", description);
XElement xLocation = new XElement(wa + "Location", location);
//XElement xAffinityGroup = new XElement(wa + "AffinityGroup", affinityGroup);

XElement createHostedService = new XElement(wa + "CreateHostedService");
createHostedService.Add(xServiceName);
createHostedService.Add(xLabel);
createHostedService.Add(xDescription);
createHostedService.Add(xLocation);
//createHostedService.Add(xAffinityGroup);

XDocument payload = new XDocument();
payload.Add(createHostedService);

payload.Declaration = new XDeclaration("1.0", "UTF-8", "no");

return payload;
}


Other method used in CreateHosetdService function was Invoke.This function is used to make HTTP request to Azure portal.


public String Invoke(XDocument payload,string operationUri)
{



HttpWebRequest httpWebRequest = Helper.CreateHttpWebRequest(new Uri(operationUri), "POST");
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
using (StreamWriter streamWriter =
new StreamWriter(requestStream,
System.Text.UTF8Encoding.UTF8))
{
payload.Save(streamWriter,
SaveOptions.DisableFormatting);
}
}

String requestId;
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
requestId = response.Headers["x-ms-request-id"];
}

return requestId;
}


In this way you can create hosted service. I hope this post is useful. Thanks for reading.

6 responses to “Create Hosted Service using Windows Azure Management API”

  1. […] Create Hosted Service using Windows Azure Management API (Dhananjay Kumar) […]

  2. […] Kumar (@debug_mode) described how to Create [a] Hosted Service using [the] Windows Azure Management API on […]

  3. […] Create Hosted Service using Windows Azure Management API […]

  4. krithika vittal

    where is the certificate authentication to be carried out ??

  5. krithika vittal

    and which jar has the xdocument class ?

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 )

Connecting to %s

Create a website or blog at WordPress.com