你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

快速入门:使用 Azure 应用配置创建 Aspire 解决方案

在这个快速入门中,你使用Azure 应用程序配置将Aspire项目的应用设置存储和管理外部化。 你使用 Azure 应用程序配置 的 Aspire 集成库来配置应用配置资源,并在每个分布式应用中使用应用配置。

先决条件

在本地测试应用

Aspire Starter 模板包含与最小 API 项目通信的前端 Web 应用。 API项目向前端提供虚假天气数据。 前端应用使用服务发现来连接API项目。 AppHost 负责协调 Aspire 解决方案中的所有资源。

  1. 从解决方案根节点运行 AppHost:

    aspire run
    

    可在浏览器中看到 Aspire 仪表板。

    包含 Web 前端和 API 服务资源的 Aspire 仪表板的屏幕截图。

  2. 单击网页前端的 URL。 你会看到一个包含欢迎消息的页面。

    包含欢迎消息的 Web 应用的屏幕截图。

将 Azure 应用配置添加到 Aspire 解决方案

  1. 去AppHost目录。 执行以下命令添加 Aspire.Hosting.Azure.AppConfiguration NuGet 包。

    aspire add Aspire.Hosting.Azure.AppConfiguration
    

    欲了解更多信息,请参见 aspire add 和 Azure 应用程序配置 托管集成。

  2. 打开 AppHost.cs 文件并添加以下代码。

    var builder = DistributedApplication.CreateBuilder(args);
    
    // Add an Azure App Configuration resource
    var appConfiguration = builder.AddAzureAppConfiguration("appconfiguration");
    

    重要

    调用 AddAzureAppConfiguration时,指示应用在应用启动时动态生成 Azure 资源。 应用程序必须配置相应的订阅和位置。 有关详细信息,请参阅 本地 Azure 预配。 如果使用最新的 Aspire SDK,可以通过 Aspire 仪表板配置订阅信息。 Aspire 仪表板“更改 Azure 上下文”对话框的屏幕截图。

    注释

    你必须在 Azure 订阅上被分配所有者或用户访问管理员角色。 这些角色是预配过程中创建角色分配所必需的。

    小窍门

    通过将现有应用配置存储的名称和资源组作为参数传递给 AsExisting(),引用该存储:

    var storeName = builder.AddParameter("appConfigurationName");
    var resourceGroup = builder.AddParameter("appConfigurationResourceGroup");
    
    var appConfiguration = builder.AddAzureAppConfiguration("appconfiguration")
        .AsExisting(storeName, resourceGroup);
    

    欲了解更多信息,请参见“使用现有 Azure 资源”。

  3. 添加应用配置资源的引用,并配置 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
    
  4. 从解根出发,运行 aspire run。 你可以看到 Azure 应用程序配置 资源正在被配置。

    预配 Azure 应用配置资源的 Aspire 仪表板屏幕截图。

  5. 等待几分钟,你会看到 Azure 应用配置资源已预配并正在运行。

    运行 Azure 应用配置资源的 Aspire 仪表板的屏幕截图。

  6. 单击 Aspire 仪表板上的部署 URL 转到 Azure 门户。 可以看到部署已完成,可以转到 Azure 应用配置资源。

    Azure 门户的屏幕截图,其中显示了应用配置部署已完成。

添加键值

将以下键值添加到应用配置存储区,并保留 标签 和 内容类型 及其默认值。 有关如何使用 Azure 门户或 CLI 将键值添加到存储区的详细信息,请转到创建键值。

Key 价值
TestApp:Settings:Message Azure 应用配置,您好!

将 Azure 应用配置添加到 Aspire 解决方案

  1. 去 AppHost 目录。 执行以下命令添加 Aspire.Hosting.Azure.AppConfiguration NuGet 包。

    aspire add Aspire.Hosting.Azure.AppConfiguration
    

    有关详细信息,请参阅 aspire add 和 Azure 应用配置托管集成。

  2. 打开 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 配置永久性容器存储的模拟器资源,以便无需每次重新创建密钥值。

  3. 添加应用配置资源的引用,并配置 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
    
  4. 启动容器运行时。 在本教程中,我们使用 Docker Desktop。

  5. 从解根出发,运行 aspire run。 转到 Aspire 仪表板。 可以看到应用配置模拟器资源正在运行。

    Aspire 仪表板的屏幕截图,展示应用配置仿真器资源。

    容器开始运行应用配置模拟器。

    运行容器的 docker 桌面的屏幕截图。

添加键值

  1. 单击 appconfiguration 资源的 URL。 你能看到应用配置仿真器的用户界面。

  2. Create单击右上角的按钮。

    应用配置模拟器 UI 的屏幕截图。

  3. 添加以下键值。

    Key 价值
    TestApp:Settings:Message Azure 应用配置,您好!
  4. 单击“Save”按钮。

    用于创建新键值的“应用配置模拟器”UI 的屏幕截图。

在 Web 应用程序中使用应用配置

  1. 进入 Web 项目的目录。 执行以下命令添加 Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration NuGet 包。

    dotnet add package Aspire.Microsoft.Extensions.Configuration.AzureAppConfiguration
    
  2. 打开 Program.cs 文件并添加以下代码。

    var builder = WebApplication.CreateBuilder(args);
    
    // Use Azure App Configuration
    builder.AddAzureAppConfiguration("appconfiguration"); // use the resource name defined in the AppHost project
    
  3. 打开 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;
        }
    }
    
  4. 停止当前 aspire run 进程,再次运行 aspire run ,然后在Aspire仪表盘中选择网页前端URL。

    显示资源的 Aspire 仪表板的屏幕截图。

  5. 会看到一个页面,其中包含来自 Azure 应用配置的欢迎消息。

    包含来自 Azure 应用配置的欢迎消息的 Web 应用的屏幕截图。

有关消费应用如何接收和使用应用配置端点的更多信息,请参见“连接至Azure 应用程序配置”。

后续步骤

在本快速入门中,请执行以下操作:

  • 在 Aspire 解决方案中添加了 Azure 应用配置资源。
  • 使用 App Configuration Aspire 集成库从 Azure 应用配置读取键值。
  • 使用在应用配置中配置的设置显示网页。

若要了解如何将 Aspire 应用配置为动态刷新配置设置,请继续学习下一教程。

若要了解如何在 Aspire 应用中使用功能标志,请继续学习下一教程。

若要详细了解 Azure 应用配置模拟器,请继续阅读以下文档。