1
votes

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
1

1 Answers

2
votes

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