|
| 1 | +# .NET Aspire Orchestration Guide |
| 2 | + |
| 3 | +This guide explains how the BookStore solution uses **.NET Aspire** to orchestrate distributed services, manage local development resources, and simplify cloud-native application composition. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The BookStore solution is built as a distributed system composed of: |
| 8 | +1. **API Service**: Event-sourced backend |
| 9 | +2. **Web Frontend**: Blazor interactive UI |
| 10 | +3. **Infrastructure**: PostgreSQL, Redis, and Azure Blob Storage |
| 11 | + |
| 12 | +.NET Aspire serves as the glue that binds these components together, handling: |
| 13 | +- **Service Discovery**: Automatically injecting connection strings and service URLs. |
| 14 | +- **Resource Management**: Spinning up containers for databases and emulators during development. |
| 15 | +- **Observability**: Providing a unified dashboard for logs, traces, and metrics. |
| 16 | + |
| 17 | +## AppHost: The Orchestrator |
| 18 | + |
| 19 | +The `BookStore.AppHost` project is the entry point for the distributed application. It defines the architectural blueprint of the system in C#. |
| 20 | + |
| 21 | +### Defined Resources |
| 22 | + |
| 23 | +The AppHost defines the following resources in `Program.cs`: |
| 24 | + |
| 25 | +#### 1. Databases & Storage |
| 26 | +- **PostgreSQL**: A containerized PostgreSQL instance. |
| 27 | + ```csharp |
| 28 | + var postgres = builder.AddPostgres("postgres") |
| 29 | + .WithPgAdmin(); // Adds PgAdmin for database management |
| 30 | + var bookStoreDb = postgres.AddDatabase("bookstore"); |
| 31 | + ``` |
| 32 | +- **Redis**: Distributed cache for the API and hybrid caching. |
| 33 | + ```csharp |
| 34 | + var cache = builder.AddRedis("cache"); |
| 35 | + ``` |
| 36 | +- **Azure Storage**: Uses the **Azurite** emulator for local development, providing Blob storage compatible with Azure. |
| 37 | + ```csharp |
| 38 | + var storage = builder.AddAzureStorage("storage").RunAsEmulator(); |
| 39 | + var blobs = storage.AddBlobs("blobs"); |
| 40 | + ``` |
| 41 | + |
| 42 | +#### 2. Services |
| 43 | +- **API Service** (`Projects.BookStore_ApiService`): The backend API. It declares dependencies on the database, blob storage, and cache. |
| 44 | + ```csharp |
| 45 | + var apiService = builder.AddProject<Projects.BookStore_ApiService>("apiservice") |
| 46 | + .WithReference(bookStoreDb) |
| 47 | + .WithReference(blobs) |
| 48 | + .WithReference(cache) |
| 49 | + .WaitFor(postgres); // Startup dependency |
| 50 | + ``` |
| 51 | +- **Web Frontend** (`Projects.BookStore_Web`): The Blazor app. It depends on the API service to fetch data. |
| 52 | + ```csharp |
| 53 | + builder.AddProject<Projects.BookStore_Web>("webfrontend") |
| 54 | + .WithReference(apiService) |
| 55 | + .WaitFor(apiService); |
| 56 | + ``` |
| 57 | + |
| 58 | +### Service Discovery |
| 59 | + |
| 60 | +Aspire automates connection management using the resource names defined in the AppHost. |
| 61 | +- **Databases**: The API service receives the connection string for "bookstore" automatically. |
| 62 | +- **HTTP Services**: The frontend receives the base URL for "apiservice" via the service discovery mechanism, allowing `HttpClient` to simply use `http://apiservice`. |
| 63 | +
|
| 64 | +## Service Defaults: Shared Concerns |
| 65 | + |
| 66 | +The `BookStore.ServiceDefaults` project encapsulates cross-cutting concerns that every service needs. Both the API and Web projects reference this library and call `builder.AddServiceDefaults()` in their startup. |
| 67 | + |
| 68 | +### Features |
| 69 | +- **OpenTelemetry**: Automatically configures logging, tracing, and metrics export to the Aspire Dashboard. |
| 70 | +- **Health Checks**: Adds standard `/health` and `/alive` endpoints. |
| 71 | +- **Service Discovery**: Configures `Microsoft.Extensions.ServiceDiscovery` to resolve Aspire resource names. |
| 72 | + |
| 73 | +## Local Development Experience |
| 74 | + |
| 75 | +To start the entire solution, simply run the AppHost project: |
| 76 | + |
| 77 | +```bash |
| 78 | +aspire run |
| 79 | +# or |
| 80 | +dotnet run --project src/BookStore.AppHost/BookStore.AppHost.csproj |
| 81 | +``` |
| 82 | + |
| 83 | +### The Aspire Dashboard |
| 84 | + |
| 85 | +When the application starts, the **Aspire Dashboard** launches automatically. It provides a centralized view of: |
| 86 | +- **Resources**: Status and endpoints of all running services and containers. |
| 87 | +- **Console Logs**: Real-time stdout/stderr from all projects. |
| 88 | +- **Structured Logs**: searchable table of log entries. |
| 89 | +- **Traces**: Distributed traces showing the flow of requests between frontend, API, and database. |
| 90 | +- **Metrics**: Real-time graphs for CPU, memory, and custom metrics. |
| 91 | + |
| 92 | +## Production Considerations |
| 93 | + |
| 94 | +While Aspire is excellent for local development, it also facilitates deployment. The AppHost can generate manifests for deployment to environments like **Azure Container Apps** or **Kubernetes**. |
| 95 | + |
| 96 | +See the [Aspire Deployment Guide](aspire-deployment-guide.md) for detailed deployment instructions. |
0 commit comments