【问题标题】:Change Azure website app settings from code从代码更改 Azure 网站应用设置
【发布时间】:2014-05-26 16:39:49
【问题描述】:

是否可以从应用程序本身更改网站的应用程序设置?

这不是日常操作,而是自助式重新配置选项。非开发人员可以更改特定设置,这应该会导致重启,就像我可以在网站配置页面(应用设置部分)上手动执行的操作一样

【问题讨论】:

    标签: azure azure-web-app-service appsettings


    【解决方案1】:

    您还可以使用 Azure Fluent Api。

    using Microsoft.Azure.Management.Fluent;
    using Microsoft.Azure.Management.ResourceManager.Fluent;
    using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
    using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
    

    ...

    public void UpdateSetting(string key, string value)
    {
        string tenantId = "a5fd91ad-....-....-....-............";
        string clientSecret = "8a9mSPas....................................=";
        string clientId = "3030efa6-....-....-....-............";
        string subscriptionId = "a4a5aff6-....-....-....-............";
    
        var azureCredentials = new AzureCredentials(new
          ServicePrincipalLoginInformation
        {
            ClientId = clientId,
            ClientSecret = clientSecret
        }, tenantId, AzureEnvironment.AzureGlobalCloud);
    
        var _azure = Azure
       .Configure()
       .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
       .Authenticate(azureCredentials)
       .WithSubscription(subscriptionId);
    
        var appResourceId = "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/sites/xxx"; //Get From WebApp -> Properties -> Resource ID
    
        var webapp = _azure.WebApps.GetById(appResourceId);
    
        //Set App Setting Key and Value
        webapp.Update()
            .WithAppSetting(key, value)
            .Apply();
    }
    

    【讨论】:

    • 我在var _azure = Azure.Configure()... Azure 找不到编译时错误,我需要安装任何其他nuget 包来解决它吗?
    • 除了我在上面输入的参考资料之外,您应该不需要任何其他内容。
    【解决方案2】:

    一旦我找到了合适的库来做到这一点,这并不难,Microsoft Azure Web Sites Management Library

    var credentials = GetCredentials(/*using certificate*/);
    using (var client = new WebSiteManagementClient(credentials))
    {
        var currentConfig = await client.WebSites.GetConfigurationAsync(webSpaceName,
                                                                        webSiteName);
        var newConfig = new WebSiteUpdateConfigurationParameters
                        {
                            ConnectionStrings = null,
                            DefaultDocuments = null,
                            HandlerMappings = null,
                            Metadata = null,
                            AppSettings = currentConfig.AppSettings
        };
        newConfig.AppSettings[mySetting] = newValue;
        await client.WebSites.UpdateConfigurationAsync(webSpaceName, webSiteName,
                                                       newConfig);
    }
    

    【讨论】:

    • 您能否指导我从 azure 门户获取 webSpaceNamewebSiteName 的位置?
    • @Diego Mijelshon,您知道我们能否将“常规设置”更新为 Java。
    【解决方案3】:

    你读过Service Management REST API吗?文档提到它允许您以编程方式执行通过管理门户可用的大多数操作。

    【讨论】:

    【解决方案4】:

    除了 Diego 的回答,要在 WebApp(网站和/或 WebJobs)中使用 Azure 管理库,您需要配置 SSL,这有点棘手:

    Using Azure Management Libraries from Azure Web Jobs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-03
      • 2019-05-02
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多