2016-10-25 160 views
0

我可以使用Azure管理库来扩展Azure Web应用程序/ api应用程序吗?我基本上想要实现扩展和缩小Web应用程序来处理节流。因此,我需要通过C#代码来扩展Web应用程序和api应用程序。任何指向任何apprpriate库/ ReST API的指针?Azure网站缩放

随着下面提到的答案的帮助,我了解如何更新APP服务计划以扩展,但是,我无法为我的APP服务找到webHostingPlanName。我尝试了下面的代码,并且托管计划总是出现为空。

 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 
+0

的可能的复制[如何扩展Azure云服务上下从的WebAPI REST API] (HTTP://计算器。com/questions/19197445/how-to-scale-azure-cloud-service-up-and-down-from-rest-api-with-webapi) –

回答

1

是的,我们可以用Microsoft.WindowsAzure.Management.Websites库扩大和网络应用程序的下降。 使用方法WebSiteManagementClient.WebHostingPlans.UpdateAsync(webspaceName, webHostingPlanName, webHostingPlanUpdateParameters)来实现它。

我已经做了一个演示来做到这一点。以下是我的详细步骤:

1.安装Microsoft.WindowsAzure.Management.WebSites,我们可以从link获取它。

2.创建WebsiteManagementClient对象

我使用该证书来创建网站图像客户端。

  • 安装VS之后,使用make文件夹下的makecert.exe创建一个证书。

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

enter image description here

  • 上传。证书文件Azure的门户网站,然后拿到指纹

enter image description here 3.使用代码来生成WebsiteManagementClient对象

  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的WebHostingPlanUpdateParameters

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

5.Update代码为

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

注意WebHostingPlans:如果您尝试运行在Azure项目。请参阅文档Using Certificates in Azure Websites Applications添加一个应用程序的设置,其值设置为证书的指纹命名WEBSITE_LOAD_CERTIFICATES将使你的web应用可访问

+0

扩展也是可能的吗?还是只能放大和缩小? – Purbasha

+0

@Purbasha它可以用于扩展和扩展。 SKU和WorkerSize用于扩展.NumberOfWorkers用于扩展。但SKU没有价值溢价。如果想通过Premium定价层来扩展App服务计划。尝试在预览中使用[新库](https://www.nuget.org/packages/Microsoft.Azure.Management.WebSites)。 –

+0

我无法为我的APP服务找到webHostingPlanName。我尝试了下面的代码,并且主机方案总是空出来 – Purbasha