首页 > 解决方案 > 为什么存储帐户未在函数应用中列出?

问题描述

我正在使用 Microsoft.Azure.Management.*.Fluent API 将资源部署到天蓝色。正在部署的资源依次为 { Resource Group, Storage Account, App Service Plan, Function App}。

我很好奇为什么我的存储帐户没有分配给我的功能应用程序

//Create Resource Group
Logger.Info($"Checking for Resource Group: {ResourceGroupName}");
resourceGroup = (await azure.ResourceGroups.ListAsync()).FirstOrDefault(rg => rg.Name == ResourceGroupName);
if (resourceGroup != null)
{
    Logger.Info($"\tFound: {resourceGroup.Name}");
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    resourceGroup = azure.ResourceGroups.Define(ResourceGroupName).WithRegion(Region.USCentral).Create();
    Logger.Info(
        await azure.ResourceGroups.ContainAsync(ResourceGroupName)
        ? $"\t{ResourceGroupName} Created"
        : $"\tFailed to create Resource Group: {ResourceGroupName}."
    );
}


//Create Storage Account
Logger.Info($"Checking for storage account: {StorageAccountName} in resource group: {ResourceGroupName}");
storageAccount = (await azure.StorageAccounts.ListByResourceGroupAsync(ResourceGroupName))
        .FirstOrDefault(sa => sa.Name == StorageAccountName);
if (storageAccount != null)
{         
    Logger.Info($"\tFound: {storageAccount.Name}");              
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    storageAccount = await azure.StorageAccounts.Define(StorageAccountName)
        .WithRegion(Region.USCentral)
        .WithExistingResourceGroup(resourceGroup)
        .WithBlobEncryption()
        .WithGeneralPurposeAccountKindV2()
        .WithFileEncryption()
        .WithOnlyHttpsTraffic()
        .WithSku(StorageAccountSkuType.Standard_LRS)
        .WithHnsEnabled(true)
        .CreateAsync();

    Logger.Info(
        (await azure.StorageAccounts.ListByResourceGroupAsync(ResourceGroupName)).Any(x => x.Name == StorageAccountName)
        ? $"\tStorage Account: {StorageAccountName} Created Successfully"
        : $"\tFailed to create Storage Account: {StorageAccountName}."
    );
}


//Create App Service Plan
Logger.Info($"Checking for App Service Plan: {AppServicePlanName} in resource group: {ResourceGroupName}");
appServicePlan = (await azure.AppServices.AppServicePlans.ListByResourceGroupAsync(ResourceGroupName))
        .FirstOrDefault(asp => asp.Name == AppServicePlanName);
if (appServicePlan != null)
{  
    Logger.Info($"\tFound: {appServicePlan.Name}");
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    appServicePlan = await azure.AppServices.AppServicePlans
    .Define(AppServicePlanName)
    .WithRegion(Region.USCentral)
    .WithExistingResourceGroup(resourceGroup)
    .WithConsumptionPricingTier()
    .CreateAsync();

    Logger.Info(
        ((await azure.AppServices.AppServicePlans.ListByResourceGroupAsync(ResourceGroupName)).Any(x => x.Name == AppServicePlanName))
        ? $"\tApp Service Plan: {AppServicePlanName} Created Successfully"
        : $"\tFailed to create App Service Plan: {AppServicePlanName}."
    );
}

//Create Function App
Logger.Info($"Checking for Function App: {FunctionAppName} in resource group: {ResourceGroupName}");
functionApp = (await azure.AppServices.FunctionApps.ListByResourceGroupAsync(ResourceGroupName)).FirstOrDefault(x => x.Name == FunctionAppName);
if (functionApp != null)
{
    Logger.Info($"\tFound: {functionApp.Name}");
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    functionApp = await azure.AppServices.FunctionApps
        .Define(FunctionAppName)
        .WithExistingAppServicePlan(appServicePlan)
        .WithExistingResourceGroup(resourceGroup)
        .WithExistingStorageAccount(storageAccount)
        .WithRuntimeVersion("~3")
        .WithHttpsOnly(true)
        .WithWebSocketsEnabled(true)
        .WithPlatformArchitecture(PlatformArchitecture.X64)
        .WithSystemAssignedManagedServiceIdentity()
        .CreateAsync();

    Logger.Info(
        ((await azure.AppServices.FunctionApps.ListByResourceGroupAsync(ResourceGroupName)).Any(x => x.Name == FunctionAppName))
        ? $"\tFunction app: {FunctionAppName} Created Successfully"
        : $"\tFailed to create Function App: {FunctionAppName}."
    );
}
Logger.Info("\n");

在构建函数应用程序时,我指定.WithExistingStorageAccount(storageAccount),我相信它将存储帐户添加到函数应用程序。

虽然,当我检查返回值时,functionApp.StorageAccountnull. 我登录门户并查看函数应用的部署模板中的存储帐户值,发现 azureStorageAccounts 集合也是空的。

标签: c#azureazure-resource-manager

解决方案


资源组实际上添加到 azure 函数中 - 但不是作为 ARM 模板本身的一部分。

使用添加的存储帐户.WithExistingStorageAccount(storageAccount)作为键值对添加到应用程序设置。

azureStorageAccounts属性是另一个资源 的属性Microsoft.Web/sites/config,它依赖于函数应用。


推荐阅读