admin管理员组

文章数量:1188402

I need to set a custom value for a environment variable using C#.

I have been checking the Azure.ResourceManager.AppService library but I don't see any way to update environment variables or accessing to function app settings.

I need to set a custom value for a environment variable using C#.

I have been checking the Azure.ResourceManager.AppService library but I don't see any way to update environment variables or accessing to function app settings.

Share Improve this question edited Jan 26 at 22:53 qkfang 3751 silver badge15 bronze badges asked Jan 24 at 18:30 JamoJamo 5045 silver badges27 bronze badges 1
  • Use functionApp.UpdateApplicationSettingsAsync to modify app settings by updating the Properties dictionary with your key-value pair. – Dasari Kamali Commented Jan 25 at 0:46
Add a comment  | 

1 Answer 1

Reset to default 0

I tried the code below to update or change the value of an environment variable in the Azure Function App. I used functionApp.UpdateApplicationSettingsAsync and successfully updated the value without any issues.

I set the AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID of the service principle in the system Environment variables to use with DefaultAzureCredentials.

I have assigned the Contributor role to the service principle in the resource group.

using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.AppService;
using Azure.Core;

class Program
{
    static async Task Main(string[] args)
    {
        string subscriptionId = "<SubscriptionID>";
        string resourceGroupName = "<ResourceGroupName>";
        string functionAppName = "kamnetfun";

        var credential = new DefaultAzureCredential();
        var armClient = new ArmClient(credential);

        var subscription = armClient.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
        var resourceGroups = subscription.GetResourceGroups();
        var resourceGroup = resourceGroups.FirstOrDefault(rg => rg.Data.Name == resourceGroupName);
        if (resourceGroup == null)
        {
            Console.WriteLine("Resource group not found.");
            return;
        }

        var webSiteCollection = resourceGroup.GetWebSites();
        var functionApp = webSiteCollection.FirstOrDefault(wa => wa.Data.Name == functionAppName);
        if (functionApp == null)
        {
            Console.WriteLine("Function App not found.");
            return;
        }

        var appSettings = await functionApp.GetApplicationSettingsAsync();
        var settings = appSettings.Value.Properties;
        settings["KamKey"] = "465"; 

        await functionApp.UpdateApplicationSettingsAsync(appSettings.Value);
        Console.WriteLine($"Environment variable 'KamKey' updated successfully in Function App: {functionAppName}");
    }
}

Output :

The value was successfully updated in the Azure Function App's environment variables.

Azure Function App :

本文标签: cHow to programmatically change the value for a environment variable in Azure function appStack Overflow