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

Time:02-11

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?

CodePudding user response:

  • You can access app settings in any class using the standard ASP.NET Core dependency injection pattern
public TestMethod()
 { 
   // retrieve nested App Service app setting 
   var myConfig = _configuration["My:Config:Data"]; 
   // retrieve App Service connection string  
   var myConnString = _configuration.GetConnectionString("MyDbConnStr");
 }
  • If you configure an app setting with the same name in App Service and in appsettings.json, the App Service value takes precedence over the appsettings.json value.
  • The local appsettings.json value lets you debug the app locally, but the App Service value lets your run the app in production with production settings.
  • To override a specific hierarchical configuration setting in App Service, set the app setting name with the same delimited format in the key.

Please refer MS Doc and this for more information.

CodePudding user response:

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);

    var config = await _websiteClient.WebApps.GetConfigurationAsync("resource group", "web app name");

    //change/add config
    config.AppSettings.Add(new Microsoft.Azure.Management.AppService.Fluent.Models.NameValuePair
    {
        Name = "teste",
        Value = "values"
    });

    //update config
    await _websiteClient.WebApps.UpdateConfigurationAsync("", "", config);
  • Related