gpt4 book ai didi

c# - Azure Function c# - 检索订阅的 WebApp 列表

转载 作者:行者123 更新时间:2023-12-03 06:30:43 29 4
gpt4 key购买 nike

我们希望在 C# 中创建一个 azure 函数,用于检索订阅中包含的 azure Web 应用程序列表(基本上,我们希望为每个 Web 应用程序动态调用相同的 API 端点来更改 api 的子域)。

可以使用 C# 检索同一 Azure 函数订阅中包含的 Web 应用程序列表吗?

通常我们连接到主数据库,我们查询sys.databases来收集dbname并了解webapp名称。但我们正在寻找一种最聪明的方法。

最佳答案

如果您使用 C#,我会考虑使用 ArmClient 类来检索您要查找的内容。

安装这些(我已经安装了其他一些,但从这里开始,看看你进展如何,可能还需要其他几个)Nuget 包...

Azure.Identity;
Azure.ResourceManager;
Azure.ResourceManager.AppService

...然后,使用 DefaultCredential 方法(如果您从未使用过它,请在此处阅读 -> https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/README.md ),您可以查询您的订阅 webApps ...

using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.AppService;
using System;
using System.Threading.Tasks;

namespace AzureManagement
{
internal class Program
{
static void Main(string[] args)
{
GetAzureResources().Wait();
}

static async Task GetAzureResources()
{
var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential);
var subscription = await armClient.GetDefaultSubscriptionAsync();

var webSitesEnumerator = subscription.GetWebSitesAsync().GetAsyncEnumerator();

try
{
while (await webSitesEnumerator.MoveNextAsync())
{
var webSite = webSitesEnumerator.Current;

Console.WriteLine($"Web App Name ........ {webSite.Data.Name}");
Console.WriteLine($"Default Host Name ... {webSite.Data.DefaultHostName}\n");
}
}
finally
{
await webSitesEnumerator.DisposeAsync();
}

Console.ReadLine();
}
}
}

上面显然不是一个函数应用程序,但核心代码仍然可以为您工作,并且可以根据需要进行移植。

注意:我可以告诉您如何吸鸡蛋,但是,一旦部署到 Azure,您就需要做必要的工作以确保函数应用具有所需的功能访问以检索您正在查找的所有资源信息。如果您对此不熟悉,请阅读托管身份概念。设置非常简单 -> https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity

关于c# - Azure Function c# - 检索订阅的 WebApp 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75261900/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com