0

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?

Claus Appel
  • 1,015
  • 10
  • 28
  • In C# code, you can read app settings using `Microsoft.Extensions.Configuration.AzureAppConfiguration`, refer [this](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-dotnet-core-app) – Anand Sowmithiran Feb 10 '22 at 09:31
  • Can I WRITE app settings with that? It looks read-only. – Claus Appel Feb 10 '22 at 09:33
  • If you want to _update_ the app settings, then you need to use `Azure.Data.AppConfiguration`, [documentation](https://learn.microsoft.com/en-us/dotnet/api/overview/azure/data.appconfiguration-readme#update-an-existing-configuration-setting) – Anand Sowmithiran Feb 10 '22 at 09:56
  • Thanks! Now we are getting somewhere. Do you know what URI I need to give to that **ConfigurationClient**? Does it want an app service address like "https://myappname.azurewebsites.net"? I ask because I am getting login errors, and I suspect it's actually the address that's bad. – Claus Appel Feb 10 '22 at 12:57

1 Answers1

1

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);
  • Thanks. This actually helps me. But now I have the problem that `GetConfigurationAsync` returns an `SiteConfigResourceInner` object whose `AppSettings` property is null. If I initialize a new collection and send that to `UpdateConfigurationAsync`, it writes only my new values and removes all old key-value pairs. Do you know how to fix that? – Claus Appel Feb 11 '22 at 10:07
  • 1
    I passed the wrong method, I edited the code. sorry – Leonardo Oliveira Feb 11 '22 at 14:18
  • This works! Thank you! I have a follow-up question. I also want to be able to restart my web app (aka app service) from C#. Do you know which classes from this library I should use in order to achieve that? – Claus Appel Feb 14 '22 at 08:50
  • 1
    I figured out the answer to my question in my comment above. I can do `new AppServiceManager(restClient, subscriptionId, null)` and then `appServiceManager.WebApps.Inner.RestartAsync`. – Claus Appel Feb 14 '22 at 09:19