azure - 使用 Azure 托管服务标识来扩展应用服务计划,但未列出

标签 azure azure-active-directory azure-functions azure-app-service-plans azure-managed-identity

我正在尝试设置一个 Azure 函数,该函数将在工作时间内将我们的应用服务计划扩展至更大,并在工作时间之外扩展至更小。我取自the answer to this question .

public static class ScaleUpWeekdayMornings
{
    [FunctionName(nameof(ScaleUpWeekdayMornings))]
    public static async Task Run([TimerTrigger("0 0 13 * * 1-5")]TimerInfo myTimer, ILogger log) // Uses GMT timezone
    {
        var resourceGroup = "DesignServiceResourceGroup";
        var appServicePlanName = "DesignService";
        var webSiteManagementClient = await Utilities.GetWebSiteManagementClient();
        var appServicePlanRequest = await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);
        appServicePlanRequest.Body.ToList().ForEach(x => log.LogInformation($">>>{x.Name}"));
        var appServicePlan = appServicePlanRequest.Body.Where(x => x.Name.Equals(appServicePlanName)).FirstOrDefault();
        if (appServicePlan == null)
        {
            log.LogError("Could not find app service plan.");
            return;
        }
        appServicePlan.Sku.Family = "P";
        appServicePlan.Sku.Name = "P2V2";
        appServicePlan.Sku.Size = "P2V2";
        appServicePlan.Sku.Tier = "Premium";
        appServicePlan.Sku.Capacity = 1;
        var updateResult = await webSiteManagementClient.AppServicePlans.CreateOrUpdateWithHttpMessagesAsync(resourceGroup, appServicePlanName, appServicePlan);
    }
}

public static class Utilities
{
    public static async Task<WebSiteManagementClient> GetWebSiteManagementClient()
    {
        var azureServiceTokenProvider = new AzureServiceTokenProvider();
        var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/");
        var tokenCredentials = new TokenCredentials(accessToken);
        return new WebSiteManagementClient(tokenCredentials)
        {
            SubscriptionId = "<realSubscriptionIdHere>",                
        };
    }
}

当我在本地运行它时,它可以工作,并且实际上在我们的 Azure 应用服务计划上执行扩展(我相信是通过 Azure CLI)。但是,当此 Azure Function 部署到 Azure 时,它​​找不到任何应用服务计划。它到达 appservicePlan == null block 并返回。我已为已部署的 Azure Function 打开托管服务标识,并在我想要扩展的应用服务中授予其贡献者权限。

Azure App Service permission

我错过了什么吗?为什么会这样

await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);

作为已发布的 Azure 函数的一部分运行时不返回任何内容?

最佳答案

问题是在应用服务(它是应用服务计划中的应用)上设置了权限。需要在资源组上进行设置才能读取应用服务计划。

关于azure - 使用 Azure 托管服务标识来扩展应用服务计划,但未列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57891809/

相关文章:

Azure:从 MySQL 数据库创建 O365 事件

c# - 在 Azure Function v1 中将 BrokeredMessage 移至死信后出现 MessageLockLostException

c# - 检测 IIS 何时在 Azure 中回收

azure - 启用 Azure AD 身份验证后无法访问我的网站

c# - 如何将用户分配到 Azure 资源组

amazon-web-services - Azure Web 应用程序和 Azure ad http 错误 500.79 内部服务器错误

json - 在逻辑应用程序中对 JSON 进行字符串化

.net - 云上的非 ASP.NET

c# - 如何在 ASP.NET MVC 上同时执行 Azure Active Directory 单点登录和表单例份验证

python - Azure函数Python docker容器: Unable to find an Azure Storage connection string to use for this binding