Creating Hosted Service on Cloud Environment Using Windows Azure Management API

 I have seen a few articles on creation of a hosted service using C#. Most articles describe creation of a hosted service in a cloud environment very nicely. But this article will explain creation of a hosted service in Azure as well as some error descriptions. (I struggled a lot, while adding a certificate, accessing a certificate and resolving the errors conflict error, bad request (400) and hosted service name is invalid etc.).

I would like to mention a few things in here related to creation of a hosted service:

  1. Service Name; actually a service name will be assigned to the DNS Prefix property in the cloud staging or production environment.
  2. It is recommended to have a minimum of four to five characters.
  3. According to Azure standards, we should add "DNS" as a prefix with the service name.
  4. It should not contain the "/","\","." characters. It does allow the "-" character.
  5. The text assigned to the label field will be used as the hosted service name in the cloud environment.

Otherwise you will get an error like "Bad Request (400)-Invalid hosted service name". Your URI should be like this

"https://management.core.windows.net/" + subscriptionId + "/services/hostedservices";

Step 1: Passing inputs to CreateHostedServiceMethod

protected void BtnCreateHostedService_Click(object sender, EventArgs e)
{

     try
     {
          String subscriptionId = "Your subscription ID";
          String thumbprint = "Certificate Thumbprint";
          String serviceName = "DNSVM"+Unique variable.
          String label ="New Hosted Service"+Unique Variable;
          String description = New Hosted Service"+Unique Variable;
          String location = "Anywhere US";
          String affinityGroup = string.Empty;
          CreateAzureHostedService example = new CreateAzureHostedService();
          String requestId = example.CreateHostedService(
          subscriptionId, thumbprint, serviceName, label,
          description, location, affinityGroup);

     }

      catch (Exception ex)
      {
           LBLErrorMssg.Text = ex.Message;
      }
}

Step 2: Create Hosted Service method.

public String CreateHostedService(String subscriptionId, String thumbprint, String serviceName, String label, String description, String location, String affinityGroup)
{
      String uri =
      String.Format(createHostedServiceFormat, subscriptionId);
      XDocument payload = CreatePayload(serviceName, label,
      description, location, affinityGroup);
      ServiceManagementOperation operation =
      new ServiceManagementOperation(thumbprint);
      String requestId = operation.Invoke(uri, payload);
      return requestId;
}
String
 createHostedServiceFormat ="https://management.core.windows.net/{0}/services/hostedservices";

Step 3:  Here, the more important thing is the XML http web request body.

If you are creating a Http request body in runtime code then it should be like this:

private XDocument CreatePayload(String serviceName, String label, String description,String location, String affinityGroup)
{
     String base64LabelName = ConvertToBase64String(label);
     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 final result should be:

<?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>

Step 4: Label content should be in the Base64String format

private String ConvertToBase64String(String value)
{
     Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(value);
     String base64String = Convert.ToBase64String(bytes);
     return base64String;

Step 5: Getting a response from the management API

public String Invoke(String uri, XDocument payload)
{
     Uri operationUri = new Uri(uri);
     HttpWebRequest httpWebRequest =
     CreateHttpWebRequest(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;

}

Getting a request ID from the GetResponse Method will ensure that the hosted service has been created successfully.

Please refer azure management REST api documentation for the updates:

https://docs.microsoft.com/en-us/rest/api/azure/

https://docs.microsoft.com/en-us/rest/api/authorization/

https://docs.microsoft.com/en-us/rest/api/advisor/



Comments

Popular posts from this blog

Wrapping information in a symbol-Microsoft Tag

Best Ways For WCF Exception Handling

UiPath Tutorials - Part 3 (Important concepts of UiPath)