既存のアプリに Aspire を追加する
新しいテンプレートを基準にソリューションを作り直す代わりに、すでにあるアプリへ Aspire を追加しましょう。最短ルートは aspire init と AI コーディング エージェントの組み合わせです。これによりサービスを自動検出し、 AppHost へ配線できます。より細かく制御したい場合は、下に手動手順も用意しています。
なぜ既存のアプリに Aspire を追加するのか?
Section titled “なぜ既存のアプリに Aspire を追加するのか?”分散アプリケーションが大きくなるほど、ローカル開発は壊れやすいスクリプト、コピペされた接続文字列、起動順序の暗黙知の寄せ集めになりがちです。 Aspire は、すでに持っているリソースに対して単一のオーケストレーション層を提供します。関係性をコードで一度定義すれば、サービス検出、構成の注入、起動順序、ダッシュボードでの可視化を Aspire が処理します。
Aspire は段階的にも導入できます。まずは、コンテナー、データベース、キャッシュ、キュー、バックグラウンド ワーカー、ローカル開発コマンドのように、手作業では整合を保ちにくい部分からモデリングしてください。準備ができたらテレメトリを追加し、アプリの成長に合わせてモデルを深めていけます。
開始する前に、次を確認してください:
- Aspire CLI をインストール済み
- Aspire を追加する既存のアプリケーションまたはワークスペースがあること
- 既存サービスが必要とするランタイムとツール
AppHost 固有の要件
Section titled “AppHost 固有の要件”- .NET SDK 10.0 以降
- Visual Studio 2022 17.13 以降、 Visual Studio Code、または JetBrains Rider(任意)
- Node.js 22 以降
- npm、 yarn、または pnpm
推奨: 「aspireify」スキル付きの AI コーディング エージェントを使う
Section titled “推奨: 「aspireify」スキル付きの AI コーディング エージェントを使う”既存アプリへ Aspire を追加する最速の方法は、 aspire init で骨組みを作ってから、配線を aspireify エージェント スキルに任せることです。このスキルは、リソース検出、依存関係の配線、 OpenTelemetry の設定、検証を自動で行います。
-
リポジトリのルートで
aspire initを実行します:Aspire を初期化する aspire initプロンプトが表示されたら AppHost 言語( C# または TypeScript)を選ぶか、
--language csharp/--language typescriptを指定します。コマンドは最小構成の AppHost ファイル、aspire.config.json、およびaspireifyスキルをエージェントのスキル ディレクトリへ作成します。 -
AI コーディング エージェントに
aspireifyスキルの実行を依頼します。エージェントは次を行います:- リポジトリをスキャンし、既存のプロジェクト、サービス、コンテナー、インフラストラクチャを検出する
- 検出したリソース、含める対象、その他の確認事項を開始前に質問する
WithReference、WaitFor、エンドポイント、ボリュームを使って AppHost へ配線する- 各サービスに ServiceDefaults を追加し、 OpenTelemetry を構成する
aspire startを実行してセットアップを検証する
-
エージェントが成功を報告したら、自分でも
aspire startを実行してダッシュボードを開き、想定どおりか確認します。問題があればエージェントに伝えてください。 Aspire にはトラブルシューティング用のツールが豊富にあります。
aspire init コマンドと aspireify スキルの詳細は、 CLI リファレンス: aspire init を参照してください。
AppHost を手動で配線する
Section titled “AppHost を手動で配線する”配線を完全に制御したい場合や、 aspireify スキルが内部で何をしているか理解したい場合は、次の手動手順に従ってください。これは初期セットアップ後に AppHost を拡張またはカスタマイズする際のリファレンスにもなります。
AppHost を選ぶ
Section titled “AppHost を選ぶ”AppHost はオーケストレーション層です。ここでの選択はオーケストレーションの記述方法を変えるものであり、 Aspire が何をオーケストレーションできるかを制限するものではありません。
Aspire には C# AppHost のスタイルが 2 つあります:
ファイル ベース AppHost - #:sdk と #:package ディレクティブを使う単一の apphost.cs ファイルです。 .csproj もソリューション統合も不要です。ポリグロット リポジトリや迅速なセットアップに最適です。
プロジェクト ベース AppHost - 既存の C# プロジェクトと一緒に .sln 内で管理される従来型の AppHost.csproj です。 ProjectReference 項目と生成される Projects 名前空間を使い、型付きの AddProject<T>() 呼び出しを行います。すでにリポジトリが .NET ソリューションで、 IDE 統合されたオーケストレーションを望む場合に最適です。
どちらのスタイルも同じ Aspire.AppHost.Sdk と同じホスティング API を使用します。
TypeScript AppHost は、リポジトリが Node.js ワークスペース中心の場合や、 TypeScript でパス ベースのオーケストレーションを記述したい場合に適しています。
apphost.tsで管理- npm、 pnpm、 yarn、 Bun など主要なパッケージ マネージャーで実行可能
- 既存のパッケージ マネージャーおよびモノレポのワークフローに自然に適合
AppHost をセットアップする
Section titled “AppHost をセットアップする”ファイル ベース AppHost(既定)
Section titled “ファイル ベース AppHost(既定)”ソリューションへプロジェクトを追加せず、軽量な単一ファイル オーケストレーターを使いたい場合は、ファイル ベース AppHost を利用します。 .sln が検出されない場合、 aspire init が既定でこのスタイルを作成します。
-
リポジトリ ルートで
aspire initを実行します。.slnがない場合、ファイル ベースのapphost.csが作成されます:ファイル ベース AppHost で Aspire を初期化する aspire init -
ホスティング統合を追加します:
ホスティング統合を追加する aspire add redis -
apphost.csでリソースを配線します:apphost.cs #:sdk Aspire.AppHost.Sdk@13.2.0#:package Aspire.Hosting.Redis@13.2.0#pragma warning disable ASPIRECSHARPAPPS001var builder = DistributedApplication.CreateBuilder(args);var cache = builder.AddRedis("cache");var api = builder.AddCSharpApp("api", "./src/Api/MyApp.Api.csproj").WithReference(cache).WithHttpHealthCheck("/health");var worker = builder.AddCSharpApp("worker", "./src/Worker/MyApp.Worker.csproj").WithReference(cache);builder.Build().Run();
セットアップ後の典型的なリポジトリ構成は次のとおりです:
- apphost.cs (new)
- aspire.config.json (new)
ディレクトリsrc/
ディレクトリApi/
- MyApp.Api.csproj
ディレクトリWorker/
- MyApp.Worker.csproj
プロジェクト ベース AppHost( .sln あり)
Section titled “プロジェクト ベース AppHost( .sln あり)”リポジトリがすでに複数プロジェクトを含む .NET ソリューション( .sln または .slnx)の場合は、この方法を使います。プロジェクト ベース AppHost は ProjectReference 項目と生成された Projects 名前空間を使って型付きの AddProject<T>() を呼び出すため、 IntelliSense、リファクタリング、ビルド順序の把握を含む完全な IDE サポートが得られます。
-
ソリューション ルートで
aspire initを実行します。.slnを検出して、プロジェクト ベース AppHost を自動作成します:.NET ソリューションで Aspire を初期化する aspire init -
オーケストレーション対象にする各サービスへのプロジェクト参照を AppHost から追加します:
プロジェクト参照を追加する dotnet add MyApp.AppHost reference src/Api/MyApp.Api.csprojdotnet add MyApp.AppHost reference src/Web/MyApp.Web.csprojdotnet add MyApp.AppHost reference src/Worker/MyApp.Worker.csproj -
ホスティング統合を追加します:
ホスティング統合を追加する aspire add redisaspire add postgres -
AppHost の
Program.csでリソースを配線します:MyApp.AppHost/Program.cs var builder = DistributedApplication.CreateBuilder(args);var cache = builder.AddRedis("cache").WithLifetime(ContainerLifetime.Persistent);var db = builder.AddPostgres("postgres").WithLifetime(ContainerLifetime.Persistent).AddDatabase("mydb");var api = builder.AddProject<Projects.MyApp_Api>("api").WithReference(db).WithReference(cache).WaitFor(db);builder.AddProject<Projects.MyApp_Web>("web").WithReference(api).WaitFor(api);builder.AddProject<Projects.MyApp_Worker>("worker").WithReference(cache).WithReference(db);builder.Build().Run();
セットアップ後の典型的なソリューション構成は次のとおりです:
- MyApp.sln
ディレクトリMyApp.AppHost/
- MyApp.AppHost.csproj
- Program.cs
ディレクトリMyApp.ServiceDefaults/
- MyApp.ServiceDefaults.csproj
- Extensions.cs
ディレクトリsrc/
ディレクトリApi/
- MyApp.Api.csproj
ディレクトリWeb/
- MyApp.Web.csproj
ディレクトリWorker/
- MyApp.Worker.csproj
カスタム型名、複数プロジェクト ソリューション、起動プロファイルを含む AddProject の完全なワークフローは、 Project resources を参照してください。
-
TypeScript 言語オプションを付けて、ワークスペース ルートで
aspire initを実行します:TypeScript AppHost で Aspire を初期化する aspire init --language typescript -
ホスティング統合を追加します:
ホスティング統合を追加する aspire add redisaspire add postgres -
apphost.tsでリソースを配線します:apphost.ts import {createBuilder } from './.modules/aspire.js';function createBuilder(): IDistributedApplicationBuilderCreates a new distributed application builder
constbuilder = awaitconst builder: IDistributedApplicationBuildercreateBuilder();function createBuilder(): IDistributedApplicationBuilderCreates a new distributed application builder
constcache = awaitconst cache: RedisResourcebuilder.const builder: IDistributedApplicationBuilderaddRedis('cache');IDistributedApplicationBuilder.addRedis(name: string, options?: {port?: number;password?: string | ParameterResource;}): RedisResource (+1 overload)Adds a Redis container resource
constdb = (awaitconst db: PostgresDatabaseResourcebuilder.const builder: IDistributedApplicationBuilderaddPostgres('postgres')).IDistributedApplicationBuilder.addPostgres(name: string, options?: {userName?: string | ParameterResource;password?: string | ParameterResource;port?: number;}): PostgresServerResource (+1 overload)Adds a PostgreSQL server resource
addDatabase('mydb');PostgresServerResource.addDatabase(name: string, options?: {databaseName?: string;}): PostgresDatabaseResource (+1 overload)Adds a PostgreSQL database
constapi = awaitconst api: ProjectResourcebuilderconst builder: IDistributedApplicationBuilder.addProject('api', './src/Api/MyApp.Api.csproj')IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {launchProfileOrOptions?: ProjectResourceOptions;}): ProjectResource (+1 overload)Adds a .NET project resource
.withReference(ProjectResource.withReference(source: EndpointReference | string | uri, options?: {connectionName?: string;optional?: boolean;name?: string;} | undefined): ProjectResource (+2 overloads)Adds a reference to another resource
db)const db: PostgresDatabaseResource.withReference(ProjectResource.withReference(source: EndpointReference | string | uri, options?: {connectionName?: string;optional?: boolean;name?: string;} | undefined): ProjectResource (+2 overloads)Adds a reference to another resource
cache)const cache: RedisResource.waitFor(ProjectResource.waitFor(dependency: IResource, waitBehavior?: WaitBehavior): ProjectResourceWaits for another resource to be ready
db);const db: PostgresDatabaseResourceawaitbuilderconst builder: IDistributedApplicationBuilder.addViteApp('web', './services/web')IDistributedApplicationBuilder.addViteApp(name: string, appDirectory: string, options?: {runScriptName?: string;}): ViteAppResource (+1 overload)Adds a Vite application resource
.withReference(ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {connectionName?: string;optional?: boolean;name?: string;} | undefined): ViteAppResource (+2 overloads)Adds a reference to another resource
api)const api: ProjectResource.waitFor(ExecutableResource.waitFor(dependency: IResource, waitBehavior?: WaitBehavior): ViteAppResourceWaits for another resource to be ready
api);const api: ProjectResourceawaitbuilder.const builder: IDistributedApplicationBuilderbuild().IDistributedApplicationBuilder.build(): DistributedApplicationBuilds the distributed application
run();DistributedApplication.run(cancellationToken?: cancellationToken): voidRuns the distributed application
セットアップ後の典型的なワークスペース構成は次のとおりです:
- apphost.ts (new)
ディレクトリ.modules/ (new)
- …
- aspire.config.json (new)
- package.json (new or updated)
ディレクトリservices/
ディレクトリweb/
- package.json
ディレクトリsrc/
- …
ディレクトリsrc/
ディレクトリApi/
- MyApp.Api.csproj
シナリオ: ホスティング統合を使った既存サービス
Section titled “シナリオ: ホスティング統合を使った既存サービス”この方法は、実行したいワークロードに対するファーストクラスなリソース型がすでに Aspire にある場合に使います。これによりアプリケーション モデルは、汎用シェル コマンドへ落とし込むのではなく、そのサービスが何で何に依存するかに集中できます。
代表例は Node.js アプリ、 Vite フロントエンド、 Python ワーカー、 Uvicorn ベース API です。
#:sdk Aspire.AppHost.Sdk@13.3.0#:package Aspire.Hosting.Redis@13.3.0#:package Aspire.Hosting.Python@13.3.0#:package Aspire.Hosting.JavaScript@13.3.0
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var api = builder.AddUvicornApp("api", "../services/api", "main:app") .WithUv() .WithReference(cache) .WithExternalHttpEndpoints();
var worker = builder.AddPythonApp("worker", "../workers/inventory-sync", "worker.py") .WithReference(cache);
var web = builder.AddViteApp("web", "../services/web") .WithReference(api) .WaitFor(api);
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const cache: RedisResource
cache = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addRedis(name: string, options?: { port?: number; password?: string | ParameterResource;}): RedisResource (+1 overload)
Adds a Redis container resource
addRedis('cache');
const const api: UvicornAppResource
api = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addUvicornApp(name: string, appDirectory: string, app: string): UvicornAppResource
Adds a Uvicorn-based Python application resource
addUvicornApp('api', './services/api', 'main:app') .UvicornAppResource.withUv(options?: { install?: boolean; args?: string[];} | undefined): UvicornAppResource (+1 overload)
Configures uv package management for a Python application
withUv() .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): UvicornAppResource (+2 overloads)
Adds a reference to another resource
withReference(const cache: RedisResource
cache) .ExecutableResource.withExternalHttpEndpoints(): UvicornAppResource
Makes HTTP endpoints externally accessible
withExternalHttpEndpoints();
await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addPythonApp(name: string, appDirectory: string, scriptPath: string): PythonAppResource
Adds a Python script application resource
addPythonApp('worker', './workers/inventory-sync', 'worker.py') .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): PythonAppResource (+2 overloads)
Adds a reference to another resource
withReference(const cache: RedisResource
cache);
await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addViteApp(name: string, appDirectory: string, options?: { runScriptName?: string;}): ViteAppResource (+1 overload)
Adds a Vite application resource
addViteApp('web', './services/web') .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ViteAppResource (+2 overloads)
Adds a reference to another resource
withReference(const api: UvicornAppResource
api) .ExecutableResource.waitFor(dependency: IResource, waitBehavior?: WaitBehavior): ViteAppResource
Waits for another resource to be ready
waitFor(const api: UvicornAppResource
api);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();ワークロードに専用ホスティング API がまだない場合でも、 AddExecutable または addExecutable を使って実行可能リソースとしてモデル化すれば、同じアプリケーション モデルに参加させられます。
ファーストクラス ワークロードのガイダンスは、 JavaScript integration、 Python integration、 Multi-language architecture を参照してください。
シナリオ: 既存コンテナーと共有インフラストラクチャ
Section titled “シナリオ: 既存コンテナーと共有インフラストラクチャ”この方法は、重要な境界がランタイム環境そのものにある場合に使います。たとえば公開済みコンテナー イメージ、共有データベース、キャッシュ、キュー、既存インフラストラクチャ トポロジなどです。まず共有リソースをモデル化し、その後それらを利用するワークロードを接続して、接続性、構成、起動順序を明示化します。
対象インフラストラクチャに対するファーストクラス統合が Aspire にある場合は、最初にそれを追加します:
aspire add postgresaspire add redis#:sdk Aspire.AppHost.Sdk@13.3.0#:package Aspire.Hosting.PostgreSQL@13.3.0#:package Aspire.Hosting.Redis@13.3.0
var builder = DistributedApplication.CreateBuilder(args);
var db = builder.AddPostgres("postgres") .AddDatabase("orders");
var cache = builder.AddRedis("cache");
var api = builder.AddContainer("api", "ghcr.io/contoso/orders-api:latest") .WithReference(db) .WithReference(cache) .WithHttpEndpoint(port: 8080, targetPort: 8080, name: "http");
var web = builder.AddContainer("web", "ghcr.io/contoso/orders-web:latest") .WithReference(api) .WithHttpEndpoint(port: 3000, targetPort: 3000, name: "http");
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const db: PostgresDatabaseResource
db = (await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres('postgres')).PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;}): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase('orders');
const const cache: RedisResource
cache = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addRedis(name: string, options?: { port?: number; password?: string | ParameterResource;}): RedisResource (+1 overload)
Adds a Redis container resource
addRedis('cache');
const const api: ContainerResource
api = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addContainer(name: string, image: AddContainerOptions): ContainerResource
Adds a container resource
addContainer('api', { AddContainerOptions.image: string
image: 'ghcr.io/contoso/orders-api', AddContainerOptions.tag: string
tag: 'latest' }) .ContainerResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ContainerResource (+2 overloads)
Adds a reference to another resource
withReference(const db: PostgresDatabaseResource
db) .ContainerResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ContainerResource (+2 overloads)
Adds a reference to another resource
withReference(const cache: RedisResource
cache) .ContainerResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): ContainerResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ port?: number | undefined
port: 8080, targetPort?: number | undefined
targetPort: 8080, name?: string | undefined
name: 'http' });
await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addContainer(name: string, image: AddContainerOptions): ContainerResource
Adds a container resource
addContainer('web', { AddContainerOptions.image: string
image: 'ghcr.io/contoso/orders-web', AddContainerOptions.tag: string
tag: 'latest' }) .ContainerResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ContainerResource (+2 overloads)
Adds a reference to another resource
withReference(const api: ContainerResource
api) .ContainerResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): ContainerResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ port?: number | undefined
port: 3000, targetPort?: number | undefined
targetPort: 3000, name?: string | undefined
name: 'http' });
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();シナリオ: Docker Compose
Section titled “シナリオ: Docker Compose”このシナリオは、 Docker Compose がすでにシステムの形を表現できている場合に使います。 Compose ファイルを、ワークロード、共有インフラストラクチャ、公開ポート、依存関係のエッジを示す地図として扱い、その関係を AppHost へ再定義します。
目標は、各フィールドを 1 行ずつ翻訳することではなく、同じ関係性をより明確なリソース モデルとして表現することです。
services: postgres: image: postgres:latest environment: - POSTGRES_PASSWORD=postgres - POSTGRES_DB=mydb ports: - "5432:5432"
api: build: ./api environment: - DATABASE_URL=postgres://postgres:postgres@postgres:5432/mydb depends_on: - postgres
web: build: ./web environment: - API_URL=http://api:8080 depends_on: - api#:sdk Aspire.AppHost.Sdk@13.3.0#:package Aspire.Hosting.PostgreSQL@13.3.0
var builder = DistributedApplication.CreateBuilder(args);
var db = builder.AddPostgres("postgres") .AddDatabase("mydb");
var api = builder.AddDockerfile("api", "./api") .WithReference(db) .WithHttpEndpoint(port: 8080, targetPort: 8080, name: "http");
var web = builder.AddDockerfile("web", "./web") .WithReference(api) .WithHttpEndpoint(port: 3000, targetPort: 3000, name: "http");
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const db: PostgresDatabaseResource
db = (await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres('postgres')).PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;}): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase('mydb');
const const api: ContainerResource
api = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addDockerfile(name: string, contextPath: string, options?: { dockerfilePath?: string; stage?: string;}): ContainerResource (+1 overload)
Adds a container resource built from a Dockerfile
addDockerfile('api', './api') .ContainerResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ContainerResource (+2 overloads)
Adds a reference to another resource
withReference(const db: PostgresDatabaseResource
db) .ContainerResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): ContainerResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ port?: number | undefined
port: 8080, targetPort?: number | undefined
targetPort: 8080, name?: string | undefined
name: 'http' });
await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addDockerfile(name: string, contextPath: string, options?: { dockerfilePath?: string; stage?: string;}): ContainerResource (+1 overload)
Adds a container resource built from a Dockerfile
addDockerfile('web', './web') .ContainerResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ContainerResource (+2 overloads)
Adds a reference to another resource
withReference(const api: ContainerResource
api) .ContainerResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): ContainerResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ port?: number | undefined
port: 3000, targetPort?: number | undefined
targetPort: 3000, name?: string | undefined
name: 'http' });
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();これらのシナリオは出発点であり、排他的なモードではありません。実際の多くのアプリでは、ワークロード固有リソース、コンテナー、共有インフラストラクチャ、プロジェクト パス参照、ときどき必要なカスタム コマンドを 1 つのアプリケーション モデルに混在させます。重要なのは、依存関係、エンドポイント、構成、起動動作が明示化されることです。
さらに例を確認するには、 Project resources、 C# file-based apps、 Executable resources、 Migrate from Docker Compose を参照してください。
テレメトリ構成を追加する(任意)
Section titled “テレメトリ構成を追加する(任意)”テレメトリは AppHost 自体ではなく、それを出力するワークロード側で構成します。 Aspire はローカル オーケストレーション時に OTLP 宛先と共有ダッシュボードを各ワークロードへ提供しますが、各サービスでは引き続きランタイムに適した可観測性ライブラリを使用します。
アプリに C# サービスが含まれる場合、 ServiceDefaults は可観測性、レジリエンス、ヘルスチェックを追加する標準的な方法です。
-
ServiceDefaults プロジェクトを作成し、サービスから参照します:
ServiceDefaults を追加する dotnet new aspire-servicedefaults -n YourProject.ServiceDefaultsdotnet sln add YourProject.ServiceDefaultsdotnet add YourProject reference YourProject.ServiceDefaults -
サービスの
Program.csを更新します:Program.cs — ServiceDefaults を追加する var builder = WebApplication.CreateBuilder(args);builder.AddServiceDefaults();var app = builder.Build();app.MapDefaultEndpoints();app.Run();
詳しくは Service Defaults を参照してください。
アプリに Node.js または TypeScript サービスが含まれる場合は、サービス内で OpenTelemetry を構成し、 Aspire が提供する OTLP エンドポイントへ送信します。
-
OpenTelemetry パッケージをインストールします:
OpenTelemetry パッケージをインストールする npm install @opentelemetry/api @opentelemetry/sdk-node \@opentelemetry/auto-instrumentations-node \@opentelemetry/exporter-trace-otlp-grpc \@opentelemetry/exporter-metrics-otlp-grpc -
テレメトリのブートストラップ ファイルを作成します:
telemetry.ts import { NodeSDK } from '@opentelemetry/sdk-node';import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';const sdk = new NodeSDK({instrumentations: [getNodeAutoInstrumentations()],});sdk.start(); -
アプリのエントリ ポイントで最初にそれを import します:
src/server.ts import './telemetry';import express from 'express';const app = express();
他のランタイムでは、すでに利用しているランタイムに合う OpenTelemetry SDK またはインストルメンテーション ライブラリを使い、ローカル オーケストレーション時に Aspire が提供する OTLP エンドポイントへテレメトリを送信してください。
実行して検証する
Section titled “実行して検証する”AppHost で必要なリソースと関係性を表現できたら、 Aspire CLI で全体をまとめて起動します。
-
AppHost を含むディレクトリで、次を実行します:
Aspire でアプリケーションを実行する aspire run -
CLI が AppHost を検出し、リソースを起動し、ダッシュボード URL を出力するまで待ちます。
出力例 Finding apphosts...Dashboard: https://localhost:17068/login?t=examplePress CTRL+C to stop the apphost and exit. -
ブラウザーでダッシュボードを開き、次を確認します:
- すべてのリソースが正常に起動する
- サービス依存関係が想定どおりの順序で表示される
- ログ、トレース、メトリクスを確認できる
- エンドポイントと環境変数が正しい
-
フロントエンドから API 呼び出し、ワーカー ジョブ、データベース アクセスなど、実際に重視するアプリ フローを実行して確認します。
-
ターミナルで
⌃+C ⌃+C Control + C CtrlC Control + C CtrlC を押してシステムを停止します。
次のステップ
Section titled “次のステップ”ここまでで、コア ワークフローは完成です。アプリが必要とするリソースを記述し、それらに依存するワークロードを接続し、 Aspire にローカル開発時のシステム全体を実行させます。ここからは、アプリ全体を一度に作り替えるのではなく、段階的にセットアップを深めていけます。
- カスタム コマンドや非プロジェクト ワークロードについては Executable resources を確認する
- ローカル オーケストレーションの次へ進む準備ができたら Deploy your first app を進める
- エディターからアプリを実行・確認するには Aspire extension for VS Code を使う
- よくあるセットアップ問題は Troubleshooting guide を確認する
- 質問やフィードバックは Discord で共有する