关于 aspire
面向 .NET 多服务应用开发者的 .NET Aspire 实践技能,涵盖本地服务编排、共享默认配置、可观测性和部署资产生成。
主要功能
- 01AppHost 本地服务编排
- 02共享服务默认配置
- 03数据库与消息代理集成
- 04仪表板日志、指标与追踪
- 05生成部署资产
适用场景
- 01在本地启动多个服务及依赖项
- 02为服务统一配置健康检查和遥测
- 03生成容器或 Kubernetes 部署清单
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ codewithmukesh/ dotnet-claude-kit --skill aspire安装后在 Claude Code 中输入/aspire确认已加载。
详细介绍
.NET Aspire
核心原则
- AppHost 负责编排;它自身绝不会被部署 — Aspire 的核心职责是提供本地开发体验:一同启动服务、数据库和消息代理。现代 Aspire 也会生成部署资产(
aspire publish用于生成 docker-compose/Kubernetes 清单,aspire deploy用于部署到 Azure Container Apps)——但 AppHost 进程本身仍是开发/构建时工具,而不是生产环境运行时。 - 服务默认值是基线配置 —
ServiceDefaults项目在一个位置为所有服务配置 OpenTelemetry、运行状况检查和弹性。 - 使用 Aspire 集成 — Aspire 内置了 PostgreSQL、Redis、RabbitMQ、SQL Server 等集成。它们会自动处理连接字符串、运行状况检查和跟踪。
- 仪表板是你的可观测性工具 — 在本地开发中,使用 Aspire 仪表板进行跟踪、日志记录和指标监控,而不是在本地搭建 Seq/Grafana。
模式
AppHost 配置
// AppHost/Program.cs
var builder = DistributedApplication.CreateBuilder(args);
// Infrastructure resources
var postgres = builder.AddPostgres("postgres")
.WithPgAdmin()
.AddDatabase("myappdb");
var redis = builder.AddRedis("redis")
.WithRedisInsight();
var rabbitmq = builder.AddRabbitMQ("messaging")
.WithManagementPlugin();
// Application projects
var api = builder.AddProject<Projects.MyApp_Api>("api")
.WithReference(postgres)
.WithReference(redis)
.WithReference(rabbitmq)
.WithExternalHttpEndpoints();
var worker = builder.AddProject<Projects.MyApp_Worker>("worker")
.WithReference(postgres)
.WithReference(rabbitmq);
builder.Build().Run();
服务默认值
// ServiceDefaults/Extensions.cs — Standard Aspire service defaults
// Configures OpenTelemetry (metrics + tracing), health checks, service discovery, and resilience
public static class Extensions
{
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
builder.ConfigureOpenTelemetry();
builder.AddDefaultHealthChecks();
builder.Services.AddServiceDiscovery();
builder.Services.ConfigureHttpClientDefaults(http =>
{
http.AddStandardResilienceHandler();
http.AddServiceDiscovery();
});
return builder;
}
// ConfigureOpenTelemetry: adds logging, metrics (ASP.NET, HttpClient, Runtime),
// tracing (ASP.NET, HttpClient, EF Core), and OTLP exporter if configured
// AddDefaultHealthChecks: adds a "self" liveness check tagged ["live"]
}
在项目中使用服务默认值
// MyApp.Api/Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
// Add Aspire integrations
builder.AddNpgsqlDbContext<AppDbContext>("myappdb");
builder.AddRedisDistributedCache("redis");
var app = builder.Build();
app.MapDefaultEndpoints(); // health check endpoints
app.Run();
服务间通信
// AppHost — configure service references
var orderApi = builder.AddProject<Projects.OrderApi>("order-api");
var paymentApi = builder.AddProject<Projects.PaymentApi>("payment-api")
.WithReference(orderApi); // paymentApi can discover orderApi
// In PaymentApi — use service discovery
builder.Services.AddHttpClient<OrderClient>(client =>
{
client.BaseAddress = new Uri("https+http://order-api");
});
使用 Aspire 的解决方案结构
MyApp.slnx
├── MyApp.AppHost/ # Aspire orchestrator
│ └── Program.cs
├── MyApp.ServiceDefaults/ # Shared service configuration
│ └── Extensions.cs
├── src/
│ ├── MyApp.Api/ # Web API project
│ └── MyApp.Worker/ # Background worker
└── tests/
└── MyApp.Api.Tests/
反模式
不要部署 AppHost 进程
// BAD — running the AppHost executable in production as an orchestrator
// The AppHost is a dev/build-time tool, not a production runtime
// GOOD — deploy the generated assets, not the AppHost:
// aspire publish → docker-compose / Kubernetes manifests from the app model
// aspire deploy → direct deployment (e.g., Azure Container Apps)
使用 Aspire 时不要硬编码连接字符串
// BAD — hardcoding connection strings defeats Aspire's purpose
builder.Services.AddDbContext<AppDbContext>(o =>
o.UseNpgsql("Host=localhost;Database=myapp;..."));
// GOOD — use Aspire integration (connection string injected automatically)
builder.AddNpgsqlDbContext<AppDbContext>("myappdb");
不要跳过服务默认值
// BAD — manually configuring each service
builder.Services.AddOpenTelemetry()...
builder.Services.AddHealthChecks()...
// GOOD — use shared service defaults
builder.AddServiceDefaults();
决策指南
| 场景 | 建议 |
|---|---|
| 包含多个服务的本地开发 | Aspire AppHost |
| 单项目本地开发 | 使用 dotnet run 即可,Aspire 可选 |
| 共享服务配置 | ServiceDefaults 项目 |
| 本地开发用数据库 | Aspire AddPostgres() / AddSqlServer() |
| 服务发现 | Aspire 内置的服务发现 |
| 生产环境部署 | aspire publish(compose/K8s 清单)或 aspire deploy(ACA);绝不要部署 AppHost 本身 |
| 本地开发中的可观测性 | Aspire 仪表板(自动配置) |
评论
支持 Markdown