How to get and set Azure app service configuration settings from C# code?

1.8k Views Asked by At

I have an Azure app service. And I have C# application. I want to get and set configuration settings (appsettings) for my Azure app service from my C# application.

I can do it from PowerShell using az webapp config appsettings list and az webapp config appsettings set. I want to achieve the same from C#.

My current solution is to launch PowerShell from C# (using new Process()) and use az webapp. That is ugly, errors are hard to catch, and it is awkward to get to run on both Windows and Linux.

I have looked at the NuGet package Microsoft.Azure.Management.Fluent. It has a method IWebAppBase.GetSettings which lets me read the settings. But I can find no way to change the settings. The NuGet package says that it is being phased out, but I cannot find a replacement package for managing app services.

Is there a nice NuGet I should use?

1

There are 1 best solutions below

4
Leonardo Oliveira On

You will have to use the SDK to do this.

libs:

  • Microsoft.Azure.Management.AppService.Fluent
  • Microsoft.Azure.Common
  • Microsoft.Azure.Management.Fluent

you will have to do something like this

    var credentials = SdkContext.AzureCredentialsFactory
    .FromServicePrincipal("",
        "",
        "",
        AzureEnvironment.AzureGlobalCloud);

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

    var _websiteClient = new WebSiteManagementClient(restClient);

     // get
    var configs = await _websiteClient.WebApps.ListApplicationSettingsAsync("RG", "WEBAPP");

    // add config
    configs.Properties.Add("newkey", "newValue");

    // update
    var result = await _websiteClient.WebApps.UpdateApplicationSettingsAsync("RG", "WEBAPP", configs);