共用方式為


.NET .NET Aspire 概觀

.NET .NET Aspire provides tools, templates, and packages to help you build observable, production-ready apps. Delivered through NuGet packages, .NET.NET Aspire simplifies common challenges in modern app development. Today's apps often rely on multiple services like databases, messaging, and caching, many supported by .NET.NET Aspire Integrations. For the official support information, see the .NET.NET Aspire Support Policy.

為什麼 .NET.NET Aspire?

.NET .NET Aspire 可改善建置具有各種專案和資源的應用程式體驗。 透過模擬已部署案例的開發時間生產力增強功能,您可以快速開發互連的應用程式。 專為彈性而設計,.NET.NET Aspire 可讓您以慣用的工具和工作流程取代或擴充元件。 主要功能包括:

  • Dev-Time Orchestration: .NET.NET Aspire provides features for running and connecting multi-project applications, container resources, and other dependencies for local development environments.
  • 整合:.NET Aspire 整合是 NuGet 套件,適用於常用的服務,例如 Redis 或 Postgres,標準化介面可確保它們與您的應用程式一致且順暢地連線。
  • 工具:.NET Aspire 隨附 Visual Studio、Visual Studio Code和 .NET CLI 的專案範本和工具體驗,以協助您建立和與 .NET.NET Aspire 項目互動。

開發過程協調

In .NET.NET Aspire, "orchestration" primarily focuses on enhancing the local development experience by simplifying the management of your app's configuration and interconnections. 請務必注意,.NET.NET Aspire的協調流程並非用來取代生產環境中所使用的強固系統,例如 Kubernetes。 相反地,它是一組抽象概念,可簡化服務探索、環境變數和容器組態的設定,而不需要處理低階實作詳細數據。 使用 .NET.NET Aspire,您的程式代碼在任何開發計算機上都有一致的啟動載入體驗,而不需要複雜的手動步驟,讓您在開發階段更容易管理。

.NET .NET Aspire orchestration assists with the following concerns:

  • 應用程式組合:指定組成應用程式的 .NET 專案、容器、可執行檔和雲端資源。
  • 服務探索和連接字串管理:應用程式主機會插入正確的連接字串、網路組態和服務探索資訊,以簡化開發人員體驗。

例如,使用 .NET Aspire,下列程式碼會建立本機 Redis 容器資源、等候其可供使用,然後透過呼叫一些協助方法,在 "frontend" 專案中設定適當的連接字串。

// Create a distributed application builder given the command line arguments.
var builder = DistributedApplication.CreateBuilder(args);

// Add a Redis server to the application.
var cache = builder.AddRedis("cache");

// Add the frontend project to the application and configure it to use the 
// Redis server, defined as a referenced dependency.
builder.AddProject<Projects.MyFrontend>("frontend")
       .WithReference(cache)
       .WaitFor(cache);

如需詳細資訊,請參閱 .NET.NET Aspire協調流程概觀。

重要

呼叫 AddRedis 會在本機開發環境中建立新的 Redis 容器。 如果您想要使用現有的 Redis 實例,您可以使用 AddConnectionString 方法來參考現有的連接字串。 如需詳細資訊,請參閱 參考現有資源

.NET .NET Aspire integrations

.NET .NET Aspire 整合器 是 NuGet 套件,旨在簡化與熱門服務和平台的連線,例如 Redis 或 PostgreSQL。 .NET .NET Aspire 整合可透過標準化模式為您處理雲端資源設定和互動,例如新增健康情況檢查和遙測。 整合有兩方面 - 「主機」整合 代表您要連接的服務,而 「用戶端」整合 代表該服務的用戶端或使用者。 換句話說,對於許多裝載套件而言,有一個對應的用戶端套件可處理程式碼內的服務連線。

每個整合都是設計來與 .NET.NET Aspire 應用程式主機搭配運作,其設定會由 參考具名資源自動注入。 換句話說,如果 Example.ServiceFoo 參考 Example.ServiceBarExample.ServiceFoo 會繼承整合的必要組態,以允許它們自動彼此通訊。

For example, consider the following code using the .NET.NET Aspire Service Bus integration:

builder.AddAzureServiceBusClient("servicebus");

AddAzureServiceBusClient 方法會處理下列問題:

  • ServiceBusClient 註冊為 DI 容器中的單一實體,以連線到 Azure Service Bus。
  • 直接在程式碼中或透過設定檔套用 ServiceBusClient 組態。
  • Enables corresponding health checks, logging, and telemetry specific to the Azure Service Bus usage.

如需可用整合的完整清單,請參閱 .NET.NET Aspire 整合 概觀頁面。

項目範本和工具

.NET Aspire 提供一組專案範本和工具體驗,適用於 Visual Studio、Visual Studio Code和 .NET CLI。 這些範本旨在協助您建立與 .NET Aspire 項目互動,或將 .NET Aspire 新增至現有的程式代碼基底。 The templates include a set of opinionated defaults to help you get started quickly - for example, it has boilerplate code for turning on health checks and logging in .NET apps. 這些預設值是完全可自定義的,因此您可以編輯和調整它們以符合您的需求。

.NET .NET Aspire templates also include boilerplate extension methods that handle common service configurations for you:

builder.AddServiceDefaults();

如需 AddServiceDefaults 之用途的詳細資訊,請參閱 .NET.NET Aspire 服務預設值

When added to your Program.cs file, the preceding code handles the following concerns:

  • OpenTelemetry: Sets up formatted logging, runtime metrics, built-in meters, and tracing for ASP.NET Core, gRPC, and HTTP. 如需詳細資訊,請參閱 .NET.NET Aspire 遙測
  • 預設健康狀態檢查:新增工具可查詢以監視應用程式的預設健康情況檢查端點。 如需詳細資訊,請參閱 C#中的 應用程式健康檢查。
  • 服務發現功能:啟用應用程式的 服務發現功能,並進行相應的配置 HttpClient

後續步驟