In this article I will discuss how we can create a Hosted Service in Windows Azure
using the Windows Azure Management API.
To create a Hosted Service you need to perform a POST operation to the following
URI. You need to change the subscription id to your subscription Id.
https://management.core.windows.net/%3Csubscription-id%3E/services/hostedservices
While making the request, the Request Header must contain information as below:
The Request Body must contain XML as a body. XML elements should be in the same order
as shown 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. The response contains no body.
For more details, read the MSDN reference here.
To create a hosted service I have created the following function. To create the service you
need to pass the following 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 function shown above 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.
The following function will create a payload and return
as XML. LINQ to XML is being used to create the 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;
}
Another method used in the CreateHosetdService function was Invoke. This
function is used to make a HTTP request to the 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 a hosted service. I hope this article is useful. Thanks
for reading.