在C#中更新远程功能应用程序应用程序设置

时间:2020-06-18 10:33:55

标签: c# azure azure-functions application-settings

我当前正在编写计划的功能应用程序,部分需要更新我已授予服务主体访问权限的其他功能应用程序的应用程序设置。我需要更新的功能应用托管在同一应用服务计划中。

我知道我可以使用az functionapp config appsettings set cmdlet通过Powershell实现此目的,但是我想知道在C#中是否可以实现

2 个答案:

答案 0 :(得分:0)

Azure CLIAzure PowerShell Module只是执行Azure REST API的一种方式。因此,您可以使用C#HttpClient直接调用REST API。

Here,您可以检查如何使用邮递员调用REST API端点(即,如何获取令牌等)。要更改应用设置,您需要此端点Web Apps - Create Or Update Configuration-> properties.appSettings

答案 1 :(得分:0)

看起来有可能使用Microsoft.Azure.Management.AppService.FluentMicrosoft.Azure.Management.ResourceManager.Fluent 扩展库,它们是REST调用的包装。

using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using System;
using System.Threading.Tasks;

namespace Azure_Storage_Account_Key_Rotation
{
    internal async Task UpdateApplicationSetting(string appServiceResourceGroupName, string appServiceName, string appSettingName, string appSettingValue)
    {
        var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(Environment.GetEnvironmentVariable("AZURE_CLIENT_ID"),
                                                                                    Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET"),
                                                                                    Environment.GetEnvironmentVariable("AZURE_TENANT_ID"),
                                                                                    AzureEnvironment.AzureGlobalCloud);

        RestClient restClient = RestClient.Configure()
                                    .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                                    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                                    .WithCredentials(credentials)
                                    .Build();

        WebSiteManagementClient webSiteManagementClient = new WebSiteManagementClient(restClient) { SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID") };

        var appServiceSettings = await webSiteManagementClient.WebApps.ListApplicationSettingsAsync(appServiceResourceGroupName, appServiceName);

        if (appServiceSettings.Properties.ContainsKey(appSettingName))
        {
            appServiceSettings.Properties[appSettingName] = appSettingValue;
            await webSiteManagementClient.WebApps.UpdateApplicationSettingsAsync(appServiceResourceGroupName, appServiceName, appServiceSettings);
        }
    }
}