AppHost とは?
Aspire の AppHost は、アプリケーションのサービスやそれらの関係性を コードファーストで宣言するための場所です。分散した設定ファイルを管理する代わりに、アーキテクチャをコードで記述します。その後のローカル オーケストレーションは Aspire が担ってくれるため、機能開発に集中できます。
アーキテクチャの定義
Section titled “アーキテクチャの定義”フロントエンドが API と通信し、API がデータベースと通信する、シンプルな 3 層アーキテクチャを考えてみましょう:
architecture-beta service db(logos:postgresql)[PostgreSQL] service api(logos:dotnet)[API service] service frontend(logos:react)[React front end] api:R --> L:db frontend:R --> L:api
このアーキテクチャは、.NET API が PostgreSQL データベース に接続し、React フロントエンド がその API を利用する構成を示しています。.NET API は ASP.NET Core を使用し、Entity Framework または接続文字列を用いて PostgreSQL に接続します。React フロントエンドは Vite で構築され、HTTP 経由で API と通信します。
このアーキテクチャを手順どおりに構築するには、クイックスタートの 最初の Aspire アプリを作成するを参照してください。
architecture-beta service db(logos:postgresql)[PostgreSQL] service api(logos:python)[API service] service frontend(logos:react)[React front end] api:R --> L:db frontend:R --> L:api
このアーキテクチャは、Python API(FastAPI/Uvicorn を使用) が PostgreSQL データベース に接続し、React フロントエンド がその API を利用する構成を示しています。Python API は FastAPI や Flask などのフレームワークを使用し、psycopg2 や SQLAlchemy などのライブラリを用いて PostgreSQL に接続します。React フロントエンドは Vite で構築され、HTTP 経由で API と通信します。
このアーキテクチャを手順どおりに構築するには、クイックスタートの 最初の Aspire アプリを作成するを参照してください。
architecture-beta service db(logos:postgresql)[PostgreSQL] service api(logos:nodejs-icon)[API service] service frontend(logos:react)[React front end] api:R --> L:db frontend:R --> L:api
このアーキテクチャは、Node.js API が PostgreSQL データベースに接続し、React フロントエンド がその API を利用する構成を示しています。Node.js API は Express や Fastify などのフレームワークを使用し、pg や Prisma などのライブラリを用いて PostgreSQL に接続します。React フロントエンドは Vite で構築され、HTTP 経由で API と通信します。
architecture-beta service db(logos:postgresql)[PostgreSQL] service api(logos:go)[API service] service frontend(logos:react)[React front end] api:R --> L:db frontend:R --> L:api
このアーキテクチャは、Go API が PostgreSQL データベースに接続し、React フロントエンドがその API を利用する構成を示しています。Go API は標準ライブラリの net/http パッケージ、または Gin や Echo などのフレームワークを使用し、pgx や database/sql などのライブラリを用いて PostgreSQL に接続します。React フロントエンドは Vite で構築され、HTTP 経由で API と通信します。
architecture-beta service db(logos:postgresql)[PostgreSQL] service api(logos:java)[API service] service frontend(logos:react)[React front end] api:R --> L:db frontend:R --> L:api
このアーキテクチャは、Java API(Spring Boot を使用)が PostgreSQL データベースに接続し、React フロントエンド がその API を利用する構成を示しています。Java API は Spring Boot と Spring Data JPA を使用し、JDBC または Spring Data を用いて PostgreSQL に接続します。React フロントエンドは Vite で構築され、HTTP 経由で API と通信します。
このアーキテクチャは、次のように AppHost で表現できます:
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddProject<Projects.Api>("api") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddUvicornApp("api", "../api", "main:app") .WithUv() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddNodeApp("api", "../api", "server.js") .WithNpm() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddGolangApp("api", "../api") .WithHttpEndpoint(env: "PORT") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddSpringApp("api", "../api", "otel.jar") .WithHttpEndpoint(port: 8080) .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();AppHost は、分散アプリケーションを宣言的にモデル化します。上部の各タブは、PostgreSQL データベース、API サービス、React フロントエンドからなる同一の 3 層アーキテクチャを示していますが、API の実装が異なります。タブを切り替えると、API リソースの種類のみが変更されます:
.NET プロジェクトを参照するために AddProject<Projects.Api>() を使用します。
FastAPI などの ASGI アプリには、WithUv() と組み合わせて AddUvicornApp() を使用します。
Node.js アプリケーションには、WithNpm() と組み合わせて AddNodeApp() を使用します。
Go アプリケーションには AddGolangApp() を使用します。
Spring Boot アプリケーションには AddSpringApp() を使用します。
いずれの場合も、PostgreSQL データベースと React フロントエンドは同一のままです。Aspire の WithReference() メソッドはリソース間の依存関係を定義し、WaitFor() はサービスが正しい順序で起動することを保証します。
Aspire は、使用する言語やフレームワークに関係なく、サービス、リソース、そしてそれらの接続関係という一貫したモデルを提供します。
AppHost コードの分解
Section titled “AppHost コードの分解”ここでは、典型的な AppHost の主要な部分を取り上げ、各ステップが何をしているのかを説明します。
var builder = DistributedApplication.CreateBuilder(args);
14 collapsed lines
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddProject<Projects.Api>("api") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
15 collapsed lines
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddUvicornApp("api", "../api", "main:app") .WithUv() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
15 collapsed lines
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddNodeApp("api", "../api", "server.js") .WithNpm() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
15 collapsed lines
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddGolangApp("api", "../api") .WithHttpEndpoint(env: "PORT") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
15 collapsed lines
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddSpringApp("api", "../api", "otel.jar") .WithHttpEndpoint(port: 8080) .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();展開されている行では、次の処理を行っています:
DistributedApplication.CreateBuilder(args)を使用して、分散アプリケーション ビルダーを作成します。Build()を呼び出して、構成を実行可能な AppHost として具体化します。Run()を呼び出してオーケストレーションを開始し、サービスは依存関係の順序に従って起動されます。
AppHost は分散アプリケーションの設計図であり、残りの処理は Aspire が管理します。
PostgreSQL リソースの追加
Section titled “PostgreSQL リソースの追加”ビルダーの準備ができたら、リソースやサービスを定義します。次のスニペットは、PostgreSQL サーバーとデータベースを追加する方法を示しています:
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
10 collapsed lines
// Add API service and reference the databasevar api = builder.AddProject<Projects.Api>("api") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
11 collapsed lines
// Add API service and reference the databasevar api = builder.AddUvicornApp("api", "../api", "main:app") .WithUv() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
11 collapsed lines
// Add API service and reference the databasevar api = builder.AddNodeApp("api", "../api", "server.js") .WithNpm() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
11 collapsed lines
// Add API service and reference the databasevar api = builder.AddGolangApp("api", "../api") .WithHttpEndpoint(env: "PORT") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
11 collapsed lines
// Add API service and reference the databasevar api = builder.AddSpringApp("api", "../api", "otel.jar") .WithHttpEndpoint(port: 8080) .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();仕組みの説明:
AddPostgres("db")は、dbという名前の PostgreSQL コンテナーを登録します。- これは
IResourceBuilder<PostgresServerResource>を返すため、Fluent スタイルで設定を連結できます。
- これは
.AddDatabase("appdata")は、そのサーバー上にappdataという名前のデータベースを作成します。.WithDataVolume()は、コンテナーの再起動後もデータが保持されるように、ボリュームをプロビジョニングします。
詳しくは公式の PostgreSQL integrationをご覧ください。
API リソースの追加と依存関係の宣言
Section titled “API リソースの追加と依存関係の宣言”次に、API サービスを登録し、PostgreSQL リソースに接続します:
6 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddProject<Projects.Api>("api") .WithReference(postgres) .WaitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();6 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddUvicornApp("api", "../api", "main:app") .WithUv() .WithReference(postgres) .WaitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();6 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddNodeApp("api", "../api", "server.js") .WithNpm() .WithReference(postgres) .WaitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();6 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddGolangApp("api", "../api") .WithHttpEndpoint(env: "PORT") .WithReference(postgres) .WaitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();6 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddSpringApp("api", "../api", "otel.jar") .WithHttpEndpoint(port: 8080) .WithReference(postgres) .WaitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();これで行われること:
AddProject<Projects.Api>("api")は、API プロジェクトをapiという名前のサービスとして登録します。WithReference(postgres)は、接続情報(ホスト、ポート、資格情報、接続文字列)を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常状態(healthy)になるまで API の起動を遅らせ、起動タイミングに依存した脆さを避けます。
AddUvicornApp("api", "../api", "main:app")は、Uvicorn ベースの Python アプリをapiという名前のサービスとして登録し、エントリーポイントにmain:appを指定します。WithUv()は、依存関係のインストールに uv パッケージマネージャーを使用するようにアプリを構成します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常な状態になるまで API の起動を遅延させます。
AddNodeApp("api", "../api", "server.js")は、Node.js アプリをapiという名前のサービスとして登録し、エントリーポイントにserver.jsを指定します。WithNpm()は、依存関係のインストールに npm を使用するようにアプリを構成します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常な状態になるまで API の起動を遅延させます。
AddGolangApp("api", "../api")は、Go アプリをapiという名前のサービスとして登録します。WithHttpEndpoint(env: "PORT")は、ポートを構成し、PORT 環境変数を設定します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常な状態になるまで API の起動を遅延させます。
AddSpringApp("api", "../api", "../agents/opentelemetry-javaagent.jar")は、OpenTelemetry エージェントのパスを指定して、Spring Boot アプリをapiという名前のサービスとして登録します。WithHttpEndpoint(port: 8080)は、Spring Boot アプリをポート 8080 で公開します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常な状態になるまで API の起動を遅延させます。
これで api サービスが定義できたので、次はフロントエンドを接続できます。
フロントエンド リソースの追加
Section titled “フロントエンド リソースの追加”フロントエンド プロジェクトを登録し、API への依存関係を宣言すると、AppHost が API のアドレスを自動的に提供します。
11 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddProject<Projects.Api>("api") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();12 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddUvicornApp("api", "../api", "main:app") .WithUv() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();12 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddNodeApp("api", "../api", "server.js") .WithNpm() .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();12 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddGolangApp("api", "../api") .WithHttpEndpoint(env: "PORT") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();12 collapsed lines
var builder = DistributedApplication.CreateBuilder(args);
// Add database resourcevar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddSpringApp("api", "../api", "otel.jar") .WithHttpEndpoint(port: 8080) .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIbuilder.AddViteApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT") .WithReference(api);
builder.Build().Run();ポイント:
.AddViteApp("front end", "../frontend")は、(Vite ベースの)フロントエンド プロジェクトをfront endという名前のサービスとして登録します。.WithHttpEndpoint(env: "PORT", targetPort: 3000)は、アプリケーションをポート3000で公開します。PORT環境変数で上書きすることも可能です。.WithReference(api)は、API のベース アドレスをフロントエンドの構成に注入します
要するに、バックエンド(DB → API)を先に定義し、その後 UI から API を参照するという流れです。 AppHost は、依存関係のグラフ、接続の流れ、起動順序をまとめて管理します。
構成とネットワーク
Section titled “構成とネットワーク”これらの依存関係や接続は、Aspire によって自動的に管理されます。AppHost は、接続文字列やエンドポイントといった構成値を生成し、必要に応じて各サービスへ注入します。 AppHost でリソースを追加する際には、 db, api, front end のように名前を付けますが、Aspire はこれらの名前を DNS 解決に使用するため、サービス同士は予測可能なアドレスで通信できます。また、利用側のサービスは、これらの名前を基に構成情報の注入を受け取ります。
architecture-beta service db(logos:postgresql)[pg] service epr(iconoir:server-connection)[Endpoint Reference] service api(logos:dotnet)[api] service ctr(iconoir:server-connection)[Connection String Reference] service frontend(logos:react)[front end] db:L <-- R:ctr ctr:L <-- R:api api:L <-- R:epr epr:L <-- R:frontend
.NET API は PostgreSQL から ConnectionStringReference を受け取り、React フロントエンドが利用する EndpointReference を公開します。これにより、PostgreSQL → .NET API → React フロントエンド という明確な依存関係のチェーンが構築されます。
architecture-beta service db(logos:postgresql)[pg] service epr(iconoir:server-connection)[Endpoint Reference] service api(logos:python)[api] service ctr(iconoir:server-connection)[Connection String Reference] service frontend(logos:react)[front end] db:L <-- R:ctr ctr:L <-- R:api api:L <-- R:epr epr:L <-- R:frontend
Python API は PostgreSQL から ConnectionStringReference を受け取り、React フロントエンドが利用する EndpointReference を公開します。これにより、PostgreSQL → Python API → React フロントエンド という明確な依存関係のチェーンが構築されます。
architecture-beta service db(logos:postgresql)[pg] service epr(iconoir:server-connection)[Endpoint Reference] service api(logos:nodejs-icon)[api] service ctr(iconoir:server-connection)[Connection String Reference] service frontend(logos:react)[front end] db:L <-- R:ctr ctr:L <-- R:api api:L <-- R:epr epr:L <-- R:frontend
Node.js API は PostgreSQL から ConnectionStringReference を受け取り、React フロントエンドが利用する EndpointReference を公開します。これにより、PostgreSQL → Node.js API → React フロントエンド という明確な依存関係のチェーンが構築されます。
architecture-beta service db(logos:postgresql)[pg] service epr(iconoir:server-connection)[Endpoint Reference] service api(logos:go)[api] service ctr(iconoir:server-connection)[Connection String Reference] service frontend(logos:react)[front end] db:L <-- R:ctr ctr:L <-- R:api api:L <-- R:epr epr:L <-- R:frontend
Go API は PostgreSQL から ConnectionStringReference を受け取り、React フロントエンドが利用する EndpointReference を公開します。これにより、PostgreSQL → Go API → React フロントエンド という明確な依存関係のチェーンが構築されます。
architecture-beta service db(logos:postgresql)[pg] service epr(iconoir:server-connection)[Endpoint Reference] service api(logos:java)[api] service ctr(iconoir:server-connection)[Connection String Reference] service frontend(logos:react)[front end] db:L <-- R:ctr ctr:L <-- R:api api:L <-- R:epr epr:L <-- R:frontend
Java API は PostgreSQL から ConnectionStringReference を受け取り、React フロントエンドが利用する EndpointReference を公開します。これにより、PostgreSQL → Java API → React フロントエンド という明確な依存関係のチェーンが構築されます。
これらのリソースがどのようにやり取りするか
pg、接続文字列(ホスト、ポート、データベース、ユーザー、パスワード)をまとめた、Aspire が理解できる強く型付けされたConnectionStringReference(host, port, database, user, password) を公開します。apiはその参照への依存関係を宣言し、Aspire はローカル実行時・デプロイ時の両方において、シークレット、パラメーター、接続文字列を含む設定値を注入する独自の構成フローを通じて、接続文字列を設定に注入します。apiは、HTTP エンドポイントが割り当てられた後に、自身のベース URL としてEndpointReferenceを公開します。front endはそのエンドポイントに依存し、Aspire が API のベース URL を注入するため、アドレスをハードコードする必要がありません。
AppHost の仕組み
Section titled “AppHost の仕組み”AppHost を実行すると、Aspire は次のような中核的な役割を担います:
- サービス検出: AppHost に宣言されたサービスやリソースを検出します。
- 依存関係の解決: 宣言された依存関係に基づき、正しい順序でサービスを起動します。
- 構成情報の注入: 接続文字列、エンドポイント、その他の構成値を自動的に注入します。
- 状態監視: サービスの状態を監視し、必要に応じて再起動します。
Aspire のオーケストレーションや リソースモデルについて、さらに詳しくご覧ください。
AppHost の構成
Section titled “AppHost の構成”テンプレートの AppHost は、次のような構成になっています:
ディレクトリAspireApp.AppHost
- apphost.cs 開発時オーケストレーター
- apphost.run.json
ディレクトリAspireApp.AppHost
ディレクトリProperties
- launchSettings.json
- appsettings.Development.json
- appsettings.json
- AspireApp.AppHost.csproj
- AppHost.cs 開発時オーケストレーター
AppHost のライフサイクル イベント
Section titled “AppHost のライフサイクル イベント”AppHost では、起動時やリソース割り当ての過程でカスタム ロジックを実行するために、ライフサイクル イベントにフックできます。
BeforeStartEvent: AppHost がサービスの起動を開始する前に発生します。AfterEndpointsAllocatedEvent: サービスのエンドポイントが割り当てられた後に発生します。AfterResourcesCreatedEvent: すべてのリソースが作成された後に発生します。
より細かなライフサイクル制御については、 既知のライフサイクル イベントをご覧ください。
ベスト プラクティス
Section titled “ベスト プラクティス”- まずは AppHost をシンプルに保ち、必要に応じて段階的に複雑さを追加しましょう。
.WithReference(...)を使って明示的に依存関係を定義し、接続関係を分かりやすくします。- 開発・テスト・本番で構成を分けて管理しましょう。
- デバッグやログを容易にするため、リソースには分かりやすく説明的な名前を付けましょう。