AppHost とは?
Aspire の AppHost は、アプリケーションのサービスとその関係をコードファーストで宣言する場所です。散在した設定ファイルを管理する代わりに、アーキテクチャをコードで記述します。ローカルでのオーケストレーションは Aspire が処理するため、機能開発に集中できます。
TypeScript で 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 frontend] 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 frontend] 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 と通信します。
既存の Python ベース アプリにこのアーキテクチャを追加するには、既存のアプリに Aspire を追加する から始めて、次に Python との統合 を確認してください。
architecture-beta service db(logos:postgresql)[PostgreSQL] service api(logos:nodejs-icon)[API service] service frontend(logos:react)[React frontend] 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 frontend] 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 frontend] 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();AddProject<Projects.Api>() を使って .NET プロジェクトを参照します。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addProject("api", "../api/Api.csproj", "https") .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();addProject() を使って API プロジェクトを参照します。
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();FastAPI のような ASGI アプリには、AddUvicornApp() と WithUv() を使います。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addUvicornApp("api", "../api", "main:app") .withUv() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();FastAPI のような ASGI アプリには、addUvicornApp() と withUv() を使います。
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();Node.js アプリケーションには、AddNodeApp() と WithNpm() を使います。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addNodeApp("api", "../api", "server.js") .withNpm() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();Node.js アプリケーションには、addNodeApp() と withNpm() を使います。
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();Go アプリケーションには AddGolangApp() を使います。
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();Spring Boot アプリケーションには AddSpringApp() を使います。
AppHost は、分散アプリケーションを宣言的にモデル化します。上の各コード サンプルは、PostgreSQL データベース、API サービス、React フロントエンド から成る同じ 3 層アーキテクチャを示していますが、API の実装と、利用できる場合は AppHost の言語が異なります。
どの場合でも、PostgreSQL データベースと React フロントエンドは同じです。Aspire のリソース参照がリソース間の依存関係を表現し、アプリ モデルがサービスを正しい順序で起動させます。
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();展開されている行で行っていること:
DistributedApplication.CreateBuilder(args)を使って分散アプリケーション ビルダーを作成します。Build()を呼び出して、構成を実行可能な AppHost として具体化します。Run()を呼び出してオーケストレーションを開始し、サービスは依存関係の順序に従って起動します。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
14 collapsed lines
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addProject("api", "../api/Api.csproj", "https") .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();展開されている行で行っていること:
await createBuilder()を使って分散アプリケーション ビルダーを作成します。build()を呼び出して、構成を実行可能な AppHost として具体化します。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();展開されている行で行っていること:
DistributedApplication.CreateBuilder(args)を使って分散アプリケーション ビルダーを作成します。Build()を呼び出して、構成を実行可能な AppHost として具体化します。Run()を呼び出してオーケストレーションを開始し、サービスは依存関係の順序に従って起動します。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
15 collapsed lines
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addUvicornApp("api", "../api", "main:app") .withUv() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();展開されている行で行っていること:
await createBuilder()を使って分散アプリケーション ビルダーを作成します。build()を呼び出して、構成を実行可能な AppHost として具体化します。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();展開されている行で行っていること:
DistributedApplication.CreateBuilder(args)を使って分散アプリケーション ビルダーを作成します。Build()を呼び出して、構成を実行可能な AppHost として具体化します。Run()を呼び出してオーケストレーションを開始し、サービスは依存関係の順序に従って起動します。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
15 collapsed lines
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addNodeApp("api", "../api", "server.js") .withNpm() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();展開されている行で行っていること:
await createBuilder()を使って分散アプリケーション ビルダーを作成します。build()を呼び出して、構成を実行可能な AppHost として具体化します。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();展開されている行で行っていること:
DistributedApplication.CreateBuilder(args)を使って分散アプリケーション ビルダーを作成します。Build()を呼び出して、構成を実行可能な AppHost として具体化します。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();仕組み:
AddPostgres(“db”)は、dbという名前の PostgreSQL コンテナーを登録します。.AddDatabase(“appdata”)は、そのサーバー上にappdataという名前のデータベースを作成します。.WithDataVolume()は、コンテナーの再起動後もデータが保持されるようにボリュームを用意します。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
10 collapsed lines
// Add API service and reference the databaseconst api = await builder.addProject("api", "../api/Api.csproj", "https") .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();仕組み:
await builder.addPostgres(“db”)は、dbという名前の PostgreSQL サーバー リソースを登録します。.addDatabase(“appdata”)は、そのサーバーにappdataデータベースを追加します。.withDataVolume()は、データベース コンテナーの永続ストレージを有効にします。
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();仕組み:
AddPostgres(“db”)は、dbという名前の PostgreSQL コンテナーを登録します。.AddDatabase(“appdata”)は、そのサーバー上にappdataという名前のデータベースを作成します。.WithDataVolume()は、コンテナーの再起動後もデータが保持されるようにボリュームを用意します。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
11 collapsed lines
// Add API service and reference the databaseconst api = await builder.addUvicornApp("api", "../api", "main:app") .withUv() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();仕組み:
await builder.addPostgres(“db”)は、dbという名前の PostgreSQL サーバー リソースを登録します。.addDatabase(“appdata”)は、そのサーバーにappdataデータベースを追加します。.withDataVolume()は、データベース コンテナーの永続ストレージを有効にします。
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();仕組み:
AddPostgres(“db”)は、dbという名前の PostgreSQL コンテナーを登録します。.AddDatabase(“appdata”)は、そのサーバー上にappdataという名前のデータベースを作成します。.WithDataVolume()は、コンテナーの再起動後もデータが保持されるようにボリュームを用意します。
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
11 collapsed lines
// Add API service and reference the databaseconst api = await builder.addNodeApp("api", "../api", "server.js") .withNpm() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();仕組み:
await builder.addPostgres(“db”)は、dbという名前の PostgreSQL サーバー リソースを登録します。.addDatabase(“appdata”)は、そのサーバーにappdataデータベースを追加します。.withDataVolume()は、データベース コンテナーの永続ストレージを有効にします。
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();仕組み:
AddPostgres(“db”)は、dbという名前の PostgreSQL コンテナーを登録します。.AddDatabase(“appdata”)は、そのサーバー上にappdataという名前のデータベースを作成します。.WithDataVolume()は、コンテナーの再起動後もデータが保持されるようにボリュームを用意します。
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 コンテナーを登録します。.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();行っていること:
AddProject<Projects.Api>(“api”)は、API プロジェクトをapiという名前のサービスとして登録します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常になるまで起動を待機します。
import { createBuilder } from './.modules/aspire.js';
6 collapsed lines
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addProject("api", "../api/Api.csproj", "https") .withReference(postgres) .waitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();行っていること:
addProject(“api”, ”../api/Api.csproj”, “https”)は、API プロジェクトをapiという名前のサービスとして登録します。withReference(postgres)は、データベース依存関係を API リソースに接続します。waitFor(postgres)は、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.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();行っていること:
AddUvicornApp(“api”, ”../api”, “main:app”)は、Uvicorn ベースの Python アプリをapiという名前のサービスとして登録します。WithUv()は、アプリが uv パッケージ マネージャーを使うように構成します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常になるまで起動を待機します。
import { createBuilder } from './.modules/aspire.js';
6 collapsed lines
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addUvicornApp("api", "../api", "main:app") .withUv() .withReference(postgres) .waitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();行っていること:
addUvicornApp(“api”, ”../api”, “main:app”)は、Uvicorn ベースの Python アプリをapiという名前のサービスとして登録します。withUv()は、uv ベースの依存関係管理を有効にします。withReference(postgres)は、データベース接続情報を注入します。waitFor(postgres)は、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.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();行っていること:
AddNodeApp(“api”, ”../api”, “server.js”)は、Node.js アプリをapiという名前のサービスとして登録します。WithNpm()は、依存関係のインストールに npm を使うように構成します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、PostgreSQL が正常になるまで起動を待機します。
import { createBuilder } from './.modules/aspire.js';
6 collapsed lines
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addNodeApp("api", "../api", "server.js") .withNpm() .withReference(postgres) .waitFor(postgres);
5 collapsed lines
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();行っていること:
addNodeApp(“api”, ”../api”, “server.js”)は、Node.js アプリをapiという名前のサービスとして登録します。withNpm()は、npm ベースの依存関係インストールを構成します。withReference(postgres)は、データベース接続情報を注入します。waitFor(postgres)は、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.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();行っていること:
AddGolangApp(“api”, ”../api”)は、Go アプリをapiという名前のサービスとして登録します。WithHttpEndpoint(env: “PORT”)は、ポートを構成し、PORT環境変数を設定します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、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.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();行っていること:
AddSpringApp(“api”, ”../api”, “otel.jar”)は、Spring Boot アプリをapiという名前のサービスとして登録します。WithHttpEndpoint(port: 8080)は、Spring Boot アプリをポート8080で公開します。WithReference(postgres)は、接続情報を API の構成に注入します。WaitFor(postgres)は、API の起動を PostgreSQL が正常になるまで待機させます。
これで 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();要点:
AddViteApp(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。WithHttpEndpoint(env: “PORT”)は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。WithReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
import { createBuilder } from './.modules/aspire.js';
11 collapsed lines
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addProject("api", "../api/Api.csproj", "https") .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();要点:
addViteApp(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。withHttpEndpoint({ env: “PORT” })は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。withReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
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();要点:
AddViteApp(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。WithHttpEndpoint(env: “PORT”)は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。WithReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
import { createBuilder } from './.modules/aspire.js';
12 collapsed lines
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addUvicornApp("api", "../api", "main:app") .withUv() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();要点:
addViteApp(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。withHttpEndpoint({ env: “PORT” })は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。withReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
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();要点:
AddViteApp(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。WithHttpEndpoint(env: “PORT”)は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。WithReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
import { createBuilder } from './.modules/aspire.js';
12 collapsed lines
const builder = await createBuilder();
// Add database resourceconst postgres = await builder.addPostgres("db") .addDatabase("appdata") .withDataVolume();
// Add API service and reference the databaseconst api = await builder.addNodeApp("api", "../api", "server.js") .withNpm() .withReference(postgres) .waitFor(postgres);
// Add frontend service and reference the APIawait builder.addViteApp("frontend", "../frontend") .withHttpEndpoint({ env: "PORT" }) .withReference(api);
await builder.build().run();要点:
addViteApp(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。withHttpEndpoint({ env: “PORT” })は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。withReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
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();要点:
AddViteApp(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。WithHttpEndpoint(env: “PORT”)は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。WithReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
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(“frontend”, ”../frontend”)は、フロントエンドをfrontendという名前のサービスとして登録します。WithHttpEndpoint(env: “PORT”)は、アプリを公開し、PORT環境変数でリスナーを制御できるようにします。WithReference(api)は、API のベース アドレスをフロントエンドの構成に注入します。
要するに、バックエンドを先に定義し(DB → API)、その後 UI から API を参照します。AppHost は依存関係グラフ、接続の流れ、起動順序を表現します。
構成とネットワーク
Section titled “構成とネットワーク”これらの依存関係や接続は、Aspire によって自動的に管理されます。AppHost は接続文字列やエンドポイントのような構成値を生成し、必要に応じて各サービスに注入します。AppHost でリソースを追加するときは、db、api、frontend のように名前を付けます。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)[frontend] 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)[frontend] 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)[frontend] 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)[frontend] 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)[frontend] 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はConnectionStringReferenceを公開します。これはホスト、ポート、データベース、ユーザー、パスワードをまとめた、Aspire が理解できる強く型付けされた情報です。apiはその参照への依存関係を宣言し、Aspire はローカル実行時とデプロイ時の両方で、シークレット、パラメーター、接続文字列を含む設定値を注入します。apiは HTTP エンドポイントが割り当てられた後、自身のベース URL を表すEndpointReferenceを公開します。frontendはそのエンドポイントに依存し、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 開発時オーケストレーター
ディレクトリmy-apphost/
ディレクトリ.modules/ 生成された SDK
- …
- apphost.ts 開発時オーケストレーター
- aspire.config.json
- package.json
- tsconfig.json
AppHost のライフサイクル イベント
Section titled “AppHost のライフサイクル イベント”ライフサイクル イベントにフックすることで、起動時やリソース割り当て時にカスタム ロジックを実行できます。
BeforeStartEvent: AppHost がサービス起動を開始する前に発生します。AfterEndpointsAllocatedEvent: サービスのエンドポイントが割り当てられた後に発生します。AfterResourcesCreatedEvent: すべてのリソースが作成された後に発生します。
より細かなライフサイクル制御については、既知のライフサイクル イベントをご覧ください。
ベスト プラクティス
Section titled “ベスト プラクティス”- まずは AppHost をシンプルに保ち、必要になったときにだけ複雑さを追加します。
.WithReference(...)を使って依存関係を明示的に定義し、接続をわかりやすくします。- 開発、テスト、本番で構成を分けて管理します。
- デバッグやログ確認をしやすくするために、リソースには明確で説明的な名前を付けます。