以编程方式启动Azure网站插槽

时间:2015-12-07 13:53:46

标签: c# azure azure-web-sites

我正在尝试使用C#代码序列以编程方式为Azure网站启动一个插槽。 我试过使用以下代码:

public async Task StartWebsiteSlot()
{
   var subscriptionId = "{my Azure subscription id}";
   var certPath = "{my full path to the Azure management certificate}";
   var certificate = new X509Certificate2( certPath, {my password});

   var httpHandler = new WebRequestHandler();
   httpHandler.ClientCertificates.Add(certificate);
   httpHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;

   var url = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{groupname}/providers/Microsoft.Web/sites/{site name}/start?api-version=2015-08-01";
   var postContent = new StringContent( String.Empty );

   using (var client = new HttpClient(httpHandler))
   {
      var response = await client.PostAsync(url, postContent);
   }
 }

呼叫返回“Unathorized”。 我知道证书是可以的,因为我正在使用WebManagementClient来交换部署插槽。

我应该如何访问特定的Azure管理REST API?

3 个答案:

答案 0 :(得分:1)

this Nuget包允许您从C#管理网站。加载nuget包后,您可以使用RestartAsync()方法启动网站。请参阅下面的用法。

yourwebsiteclient.Websites.RestartAsync

希望这有帮助!

https://www.nuget.org/packages/Microsoft.WindowsAzure.Management.WebSites/

https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.management.websites.iwebsiteoperations.restartasync(v=azure.11).aspx

答案 1 :(得分:1)

David Ebbo在https://github.com/davidebbo/AzureWebsitesSamples/为此提供了解决方案 必须使用StartSiteAsync或StartSiteSlotAsync替换RestartAsync调用。

答案 2 :(得分:0)

看看这个问题: Change Azure website app settings from code

它使用了neolursa建议的Azure管理库。

棘手的部分是配置SSL:Using Azure Management Libraries from Azure Web Jobs