To create Hosted Service you
need to perform a POST operation on the following URI. You need to change subscription
id with your subscription Id.
https://management.core.windows.net/<subscription-id>/services/hostedservices
When making the request, the Request Header must contain information as below:
The Request Body must contain XML
as the 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 be returned. The response contains no body.
For more details read 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 the preceding 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.
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;
}
The other method used in the CreateHosetdService function
was Invoke. This function is used to make HTTP requests 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.