从代码更改Azure网站应用程序设置

时间:2014-05-26 16:39:49

标签: azure azure-web-sites appsettings

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

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

4 个答案:

答案 0 :(得分:9)

一旦我找到了合适的lib,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);
}

答案 1 :(得分:6)

您还可以使用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();
}

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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

Using Azure Management Libraries from Azure Web Jobs