1

Can I scale azure web apps/api apps using Azure Management libraries? I basically want to implement scaling up and down of web apps to handle throttling. Hence, I need to scale the web app and api apps via C# code. Any pointers to any apprpriate library/ ReST API?

With teh help of the answer mentioned below, I understand how to update the APP Service plan to scale out, however, I am not able to find webHostingPlanName for my APP Service. I tried with the below code, and hosting plan always comes out to be null.

        var regionName = "myregion";

        var credentials = GetCredentials();

        var websiteManagementClient = CloudContext.Clients.CreateWebSiteManagementClient(credentials);

        var webSpaces = websiteManagementClient.WebSpaces.List();
        var webSpace = webSpaces.FirstOrDefault(x => x.GeoRegion == regionName);
        if (webSpace == null)
        {
            throw new Exception(string.Format("No webspace for region {0} found", regionName));
        }

        var webHostingPlans = websiteManagementClient.WebHostingPlans.List(webSpace.Name);
        var webHostingPlan = webHostingPlans.FirstOrDefault();// this is null always, I know an APP service exists in this region
Purbasha
  • 215
  • 2
  • 11
  • Possible duplicate of [How to Scale Azure cloud service up and down from Rest API with WebAPI](http://stackoverflow.com/questions/19197445/how-to-scale-azure-cloud-service-up-and-down-from-rest-api-with-webapi) – Russell Young Oct 25 '16 at 05:14

1 Answers1

2

Yes, We can use Microsoft.WindowsAzure.Management.Websites library to scale up and down of web apps. Using method WebSiteManagementClient.WebHostingPlans.UpdateAsync(webspaceName, webHostingPlanName, webHostingPlanUpdateParameters) to implement it.

I had done a demo to do this. The following are the my detail steps:

1.Install the Microsoft.WindowsAzure.Management.WebSites that we can get it from the link.

2.Create the WebsiteManagementClient object.

I used the cert to create the websitemagement client.

  • Create a cert with makecert.exe that is under the VS folder after install the VS.

    makecert -sky exchange -r -n "CN=[CertificateName]" -pe -a sha1 -len 2048 -ss My "[CertificateName].cer
    

enter image description here

  • Upload the. Cert file to Azure portal then get the thumbprint

enter image description here 3. Using the code to generate the WebsiteManagementClient object

          public static X509Certificate2 GetCert(string thumbprint)
    {

        X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        certStore.Open(OpenFlags.ReadOnly);
   X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
        if (certCollection.Count <= 0) return null;
        X509Certificate2 cert = certCollection[0];
        return cert;
    }

var cert = GetCert(string thumbprint) var subscriptionId ="Your subscriptionId" var webSiteManagementClient = new WebSiteManagementClient(new CertificateCloudCredentials(subscriptionId, cert));

4.Construct the WebHostingPlanUpdateParameters

var webHostingPlanUpdateParameters = new WebHostingPlanUpdateParameters
                {
                    NumberOfWorkers = 1, //the number of the instances
                    SKU = SkuOptions.Standard, 
                    WorkerSize = WorkerSizeOptions.Small
                };

5.Update the WebHostingPlans with code

client.WebHostingPlans.UpdateAsync(webspaceName, webHostingPlanName, webHostingPlanUpdateParameters);

Note: If you try to run the project on the Azure. Please refer to document Using Certificates in Azure Websites Applications. Adding an app setting named WEBSITE_LOAD_CERTIFICATES with its value set to the thumbprint of the certificate will make it accessible to your web application

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Scaling out is possible as well? or only scale up and down? – Purbasha Oct 25 '16 at 09:44
  • @Purbasha It can be used for both scaling out and scaling up. SKU and WorkerSize are used for scaling up .NumberOfWorkers is used for scaling out . But SKU has no value Premium. If want to scale up the App service plan with Premium pricing tier . Have a try to use to the [new library](https://www.nuget.org/packages/Microsoft.Azure.Management.WebSites) in the preview. – Tom Sun - MSFT Oct 25 '16 at 15:15
  • I am not able to find webHostingPlanName for my APP Service. I tried with the below code, and hosting plan always comes out to be null – Purbasha Oct 26 '16 at 06:43
  • Please have a try to debug the code and watch the value of the `webHostingPlans` and `webSpace.Name` to check it. – Tom Sun - MSFT Oct 26 '16 at 14:51