【问题标题】:Is it possible to enable Always On for Azure websites through management/resource management APIs?是否可以通过管理/资源管理 API 为 Azure 网站启用 Always On?
【发布时间】:2015-01-01 14:41:32
【问题描述】:

我正在编写一些自动部署 Azure 网站的代码(包括在 Azure 中创建网站)。我正在使用 Nuget 中提供的 Azure 管理库和 Azure 资源管理库。其中大部分已经到位,但是我无法通过我见过的任何 API 找到启用“始终开启”属性的方法。可以通过网站的“配置”选项卡下的 Azure 管理门户设置此属性。

我已经检查过:

  1. MSDN 上的属性参考:http://msdn.microsoft.com/en-us/library/azure/dn236426.aspx
  2. powershell API(get-azureresource、get-azurewebsite 等)查看是否有对 Always On 的引用(没有)
  3. 管理门户正在通过 Fiddler 发送的 REST 调用。这里有一个指向 https://manage.windowsazure.com/Websites/UpdateConfig 的 POST 中对 Always On 的引用(据我所知,这不是管理或资源管理 API 的一部分)。发送的 JSON 正文中的确切路径是 /siteConfig/AlwaysOn。

那么,问题是,是否可以通过“官方”API 启用/禁用 Always On?

谢谢!

【问题讨论】:

    标签: api rest azure azure-web-app-service alwayson


    【解决方案1】:

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

    使用资源管理 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": {} }

    【讨论】:

    • 这确实对我有用,谢谢!您应该将此标记为正确答案:)
    • 我无法让它工作。是否有任何先决条件,例如停止网站?
    • 另见:stackoverflow.com/a/37149251/29,它显示了另一个放置 siteConfig 块的位置。
    • Set-AzureResourceSet-AzureRmResource ?
    【解决方案2】:

    使用更新的 ARM(Azure 资源管理器)Powershell,v1.0+

    获取-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
    

    【讨论】:

    • 我收到此错误:Get-AzureRmResource:对象引用未设置为对象的实例。在 line:1 char:1 + Get-AzureRmResource + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-AzureRmResource], NullReferenceException + FullyQualifiedErrorId : System .NullReferenceException,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdl 等
    【解决方案3】:

    对于那些使用.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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2016-12-23
      相关资源
      最近更新 更多