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,
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
- Service Name : This is name of the service you want to create
- Label : This is service label
- Description : This is service description
- 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.
- One method to create payload. This payload will be passed as XML to perform POST operation
- 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.
Follow @debug_mode
Leave a Reply