问题描述
我可以使用 Azure 管理库扩展 Azure Web 应用程序/api 应用程序吗?我基本上想实现 Web 应用程序的扩展和缩减以处理限制.因此,我需要通过 C# 代码扩展 Web 应用程序和 api 应用程序.任何指向任何适当的库/ReST API 的指针?
在下面提到的答案的帮助下,我了解了如何更新 APP 服务计划以扩展,但是,我无法为我的 APP 服务找到 webHostingPlanName.我尝试使用以下代码,托管计划总是为空.
var regionName = "myregion";var 凭证 = GetCredentials();var websiteManagementClient = CloudContext.Clients.CreateWebSiteManagementClient(credentials);var webSpaces = websiteManagementClient.WebSpaces.List();var webSpace = webSpaces.FirstOrDefault(x => x.GeoRegion == regionName);如果(网络空间 == 空){throw new Exception(string.Format("No webspace for region {0} found", regionName));}var webHostingPlans = websiteManagementClient.WebHostingPlans.List(webSpace.Name);var webHostingPlan = webHostingPlans.FirstOrDefault();//这个总是空的,我知道这个区域有APP服务
是的,我们可以使用 Microsoft.WindowsAzure.Management.Websites 库来扩展和缩减 Web 应用程序.使用方法 WebSiteManagementClient.WebHostingPlans.UpdateAsync(webspaceName, webHostingPlanName, webHostingPlanUpdateParameters)
来实现.
我做了一个演示来做到这一点.以下是我的详细步骤:
1.安装我们可以从
- 上传.将证书文件发送到 Azure 门户,然后获取指纹
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) 返回空值;X509Certificate2 证书 = certCollection[0];返回证书;}
var cert = GetCert(字符串指纹)var subscriptionId =您的订阅 ID"var webSiteManagementClient = new WebSiteManagementClient(new CertificateCloudCredentials(subscriptionId, cert));
4.构造WebHostingPlanUpdateParameters
var webHostingPlanUpdateParameters = new WebHostingPlanUpdateParameters{NumberOfWorkers = 1,//实例数SKU = SkuOptions.标准,WorkerSize = WorkerSizeOptions.Small};
5. 使用代码更新 WebHostingPlans
client.WebHostingPlans.UpdateAsync(webspaceName, webHostingPlanName, webHostingPlanUpdateParameters);
注意:如果您尝试在 Azure 上运行该项目.请参阅文档 在 Azure 网站应用程序中使用证书.添加一个名为 WEBSITE_LOAD_CERTIFICATES 的应用设置,并将其值设置为证书的指纹,将使您的 Web 应用程序可以访问它
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
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
- Upload the. Cert file to Azure portal then get the thumbprint
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);
这篇关于Azure 网站缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!