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
1 Answer
Reset to default 0I 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 :
版权声明:本文标题:c# - How to programmatically change the value for a environment variable in Azure function app? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738394112a2084409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论