你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在这个快速入门中,你使用Azure 应用程序配置将Aspire项目的应用设置存储和管理外部化。 你使用 Azure 应用程序配置 的 Aspire 集成库来配置应用配置资源,并在每个分布式应用中使用应用配置。
先决条件
- 拥有有效订阅的 Azure 帐户。 免费创建一个。
- 为 Aspire 设置开发环境。 C# AppHost 需要 .NET 10 SDK。
- 安装Aspire CLI。
- 使用 Aspire Starter 模板创建新的 Aspire 解决方案。
- 在模拟器步骤中,使用符合 OCI 标准的容器运行时,如 Docker Desktop。
在本地测试应用
Aspire Starter 模板包含与最小 API 项目通信的前端 Web 应用。 API项目向前端提供虚假天气数据。 前端应用使用服务发现来连接API项目。 AppHost 负责协调 Aspire 解决方案中的所有资源。
将 Azure 应用配置添加到 Aspire 解决方案
去AppHost目录。 执行以下命令添加
Aspire.Hosting.Azure.AppConfigurationNuGet 包。aspire add Aspire.Hosting.Azure.AppConfiguration欲了解更多信息,请参见
aspire add和 Azure 应用程序配置 托管集成。打开 AppHost.cs 文件并添加以下代码。
var builder = DistributedApplication.CreateBuilder(args); // Add an Azure App Configuration resource var appConfiguration = builder.AddAzureAppConfiguration("appconfiguration");重要
调用
AddAzureAppConfiguration时,指示应用在应用启动时动态生成 Azure 资源。 应用程序必须配置相应的订阅和位置。 有关详细信息,请参阅 本地 Azure 预配。 如果使用最新的 Aspire SDK,可以通过 Aspire 仪表板配置订阅信息。
注释
你必须在 Azure 订阅上被分配所有者或用户访问管理员角色。 这些角色是预配过程中创建角色分配所必需的。
小窍门
通过将现有应用配置存储的名称和资源组作为参数传递给
AsExisting(),引用该存储:var storeName = builder.AddParameter("appConfigurationName"); var resourceGroup = builder.AddParameter("appConfigurationResourceGroup"); var appConfiguration = builder.AddAzureAppConfiguration("appconfiguration") .AsExisting(storeName, resourceGroup);欲了解更多信息,请参见“使用现有 Azure 资源”。
添加应用配置资源的引用,并配置
webfrontend项目以等待其就绪。builder.AddProject<Projects.AspireApp_Web>("webfrontend") .WithExternalHttpEndpoints() .WithHttpHealthCheck("/health") .WithReference(apiService) .WaitFor(apiService) .WithReference(appConfiguration) // reference the App Configuration resource .WaitFor(appConfiguration); // wait for the App Configuration resource to enter the Running state before starting the resource从解根出发,运行
aspire run。 你可以看到 Azure 应用程序配置 资源正在被配置。等待几分钟,你会看到 Azure 应用配置资源已预配并正在运行。
单击 Aspire 仪表板上的部署 URL 转到 Azure 门户。 可以看到部署已完成,可以转到 Azure 应用配置资源。
添加键值
将以下键值添加到应用配置存储区,并保留 标签 和 内容类型 及其默认值。 有关如何使用 Azure 门户或 CLI 将键值添加到存储区的详细信息,请转到创建键值。
| Key | 价值 |
|---|---|
| TestApp:Settings:Message | Azure 应用配置,您好! |
将 Azure 应用配置添加到 Aspire 解决方案
去 AppHost 目录。 执行以下命令添加
Aspire.Hosting.Azure.AppConfigurationNuGet 包。aspire add Aspire.Hosting.Azure.AppConfiguration有关详细信息,请参阅
aspire add和 Azure 应用配置托管集成。打开 AppHost.cs 文件并添加以下代码。
var builder = DistributedApplication.CreateBuilder(args); // Add an Azure App Configuration resource var appConfiguration = builder.AddAzureAppConfiguration("appconfiguration") .RunAsEmulator(emulator => { // use the App Configuration emulator emulator.WithDataBindMount(); });重要
调用
RunAsEmulator时,它会拉取 应用配置模拟器映像 ,并将容器作为应用配置资源运行。 请确保计算机上具有符合 OCI 的容器运行时。 有关详细信息,请转到 Aspire 容器运行时。小窍门
可以调用
WithDataBindMount或WithDataVolume配置永久性容器存储的模拟器资源,以便无需每次重新创建密钥值。添加应用配置资源的引用,并配置
webfrontend项目以等待其就绪。builder.AddProject<Projects.AspireApp_Web>("webfrontend") .WithExternalHttpEndpoints() .WithHttpHealthCheck("/health") .WithReference(apiService) .WaitFor(apiService) .WithReference(appConfiguration) // reference the App Configuration resource .WaitFor(appConfiguration); // wait for the App Configuration resource to enter the Running state before starting the resource启动容器运行时。 在本教程中,我们使用 Docker Desktop。
从解根出发,运行
aspire run。 转到 Aspire 仪表板。 可以看到应用配置模拟器资源正在运行。容器开始运行应用配置模拟器。
添加键值
在 Web 应用程序中使用应用配置
进入
Web项目的目录。 执行以下命令添加Aspire.Microsoft.Extensions.Configuration.AzureAppConfigurationNuGet 包。dotnet add package Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration打开 Program.cs 文件并添加以下代码。
var builder = WebApplication.CreateBuilder(args); // Use Azure App Configuration builder.AddAzureAppConfiguration("appconfiguration"); // use the resource name defined in the AppHost project打开 Components/Pages/Home.razor 文件,并使用以下代码对其进行更新。
@page "/" @inject IConfiguration Configuration <PageTitle>Home</PageTitle> <h1>Hello, world!</h1> @if (!string.IsNullOrWhiteSpace(message)) { <div class="alert alert-info">@message</div> } else { <div class="alert alert-info">Welcome to your new app.</div> } @code { private string? message; protected override void OnInitialized() { string? msg = Configuration["TestApp:Settings:Message"]; message = string.IsNullOrWhiteSpace(msg) ? null : msg; } }停止当前
aspire run进程,再次运行aspire run,然后在Aspire仪表盘中选择网页前端URL。会看到一个页面,其中包含来自 Azure 应用配置的欢迎消息。
有关消费应用如何接收和使用应用配置端点的更多信息,请参见“连接至Azure 应用程序配置”。
后续步骤
在本快速入门中,请执行以下操作:
- 在 Aspire 解决方案中添加了 Azure 应用配置资源。
- 使用 App Configuration Aspire 集成库从 Azure 应用配置读取键值。
- 使用在应用配置中配置的设置显示网页。
若要了解如何将 Aspire 应用配置为动态刷新配置设置,请继续学习下一教程。
若要了解如何在 Aspire 应用中使用功能标志,请继续学习下一教程。
若要详细了解 Azure 应用配置模拟器,请继续阅读以下文档。