2014-11-05 47 views
12

我正在編寫一些自動部署Azure網站的代碼(包括在Azure中創建網站)。我正在使用Nuget中提供的Azure管理庫和Azure資源管理庫。它大部分已經到位,但是我還沒有找到一種方法來通過我見過的任何API來啓用「永遠在線」屬性。此屬性可以通過站點配置選項卡下的天藍色管理門戶進行設置。是否可以通過管理/資源管理API爲Azure網站啓用Always On功能?

我檢查:

  1. 性能上MSDN參考:http://msdn.microsoft.com/en-us/library/azure/dn236426.aspx
  2. PowerShell的的API(GET-azureresource,得到-azurewebsite,...),看看是否有一個基準爲Always On (沒有)
  3. REST調用管理門戶通過Fiddler發送。這裏有一篇關於POST中的Always On的文章,前往https://manage.windowsazure.com/Websites/UpdateConfig(據我所知,它不屬於管理或資源管理API的一部分)。 JSON正文中的確切路徑是/ siteConfig/AlwaysOn。

因此,問題是,是否可以通過「官方」API啓用/禁用始終開啓?

謝謝!

回答

11

我相信我找到了解決方案!

使用資源管理API,我可以通過siteConfig對象設置AlwaysOn屬性。在PowerShell中:

Set-AzureResource -ApiVersion 2014-04-01 -PropertyObject @{"siteConfig" = @{"AlwaysOn" = $false}} -Name mywebsite -ResourceGroupName myrg -ResourceType Microsoft.Web/sites

在.NET中的資源管理API,將與此類似。

產生的REST調用,以 https://management.azure.com/subscriptions/xxx/resourcegroups/yyy/providers/Microsoft.Web/sites/zzz?api-version=2014-04-01 { "location": "West Europe", "properties": { "siteConfig": { "AlwaysOn": true } }, "tags": {} }

+1

這確實爲我工作,謝謝!你應該把這個標記爲正確的答案:) – dprothero 2015-05-29 14:12:56

+0

我無法得到這個工作。有沒有任何先決條件,如網站停止? – 2015-09-11 03:31:16

+0

另請參見:http://stackoverflow.com/a/37149251/29,它顯示了放置'siteConfig'塊的另一個地方。 – 2016-10-17 18:26:38

2

使用更新ARM(Azure的資源管理器)PowerShell中,V1.0 +

GET-AzureRmResource:https://msdn.microsoft.com/en-us/library/mt652503.aspx

套裝,AzureRmResource: https://msdn.microsoft.com/en-us/library/mt652514.aspx

# Variables - substitute your own values here 
$ResourceGroupName = 'My Azure RM Resource Group Name' 
$WebAppName = 'My Azure RM WebApp Name' 
$ClientAffinityEnabled = $false 

# Property object for nested, not exposed directly properties 
$WebAppPropertiesObject = @{"siteConfig" = @{"AlwaysOn" = $true}} 

# Variables 
$WebAppResourceType = 'microsoft.web/sites' 

# Get the resource from Azure (consider adding sanity checks, e.g. is $webAppResource -eq $null) 
$webAppResource = Get-AzureRmResource -ResourceType $WebAppResourceType -ResourceGroupName $ResourceGroupName -ResourceName $WebAppName 

# Set a directly exposed property, in this case whether client affinity is enabled 
$webAppResource.Properties.ClientAffinityEnabled = $ClientAffinityEnabled 

# Pass the resource object into the cmdlet that saves the changes to Azure 
$webAppResource | Set-AzureRmResource -PropertyObject $WebAppPropertiesObject -Force 
+0

我得到這個錯誤:Get-AzureRmResource:對象引用未設置爲對象的實例。 在線:1 char:1 + Get-AzureRmResource + ~~~~~~~~~~~~~~~~~~~~~~~~ NullRemoteException(NullReferenceException + FullyQualifiedErrorId:System.NullReferenceException,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation。GetAzureResourceCmdl 和 – 2017-04-25 18:37:04

0

對於那些使用.Net API,這是

var cfg = await websiteClient.Sites.GetSiteConfigAsync(site.ResourceGroup, site.Name, cancellationToken).ConfigureAwait(false); 
if (!cfg.AlwaysOn.GetValueOrDefault()) 
{ 
    cfg.AlwaysOn = true; 
    await websiteClient.Sites.UpdateSiteConfigAsync(site.ResourceGroup, site.Name, cfg, cancellationToken).ConfigureAwait(false); 
}