-
Notifications
You must be signed in to change notification settings - Fork 61
docs: Add App Model API documentation for CORS, console apps, resource naming, and networking #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/add-docs-for-app-model-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,268
−372
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88cdec9
Initial plan
Copilot 48ed7ef
docs: Add App Model API documentation - CORS, console apps, naming, n…
Copilot e1a909e
docs: Address review feedback - update package versions to 10.0.0, fi…
Copilot 6579b54
docs: Address reviewer feedback - Minimal APIs, remove redundant pack…
Copilot 4d1b8d0
docs: Address all reviewer feedback - sidebar translations, non-.NET …
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
src/frontend/src/content/docs/app-host/console-apps.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| --- | ||
| title: Console and worker apps | ||
| description: Learn how to integrate console applications and background worker services into Aspire, including service discovery, resource references, and interactive console patterns. | ||
| --- | ||
|
|
||
| import { Aside, Steps, Tabs, TabItem } from '@astrojs/starlight/components'; | ||
| import LearnMore from '@components/LearnMore.astro'; | ||
|
|
||
| Aspire can orchestrate more than just ASP.NET Core web applications. You can add console applications and background worker services to the AppHost, allowing them to participate in service discovery, receive connection strings, and benefit from centralized orchestration alongside the rest of your application. | ||
|
|
||
| ## Adding a console app to the AppHost | ||
|
|
||
| Any .NET project — including console applications and Worker Service projects — can be added to the AppHost using `AddProject`. The project doesn't need to be a web app. For non-.NET apps (Python, Node.js, or arbitrary executables), use `AddExecutable`, `AddPythonApp`, or `AddNodeApp` instead. | ||
|
|
||
| <LearnMore> | ||
| For more information on adding non-.NET apps, see [Executable resources](/app-host/executable-resources/), [Python integration](/integrations/frameworks/python/), and [JavaScript integration](/integrations/frameworks/javascript/). | ||
| </LearnMore> | ||
|
|
||
| ```csharp title="C# — AppHost.cs" | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var redis = builder.AddRedis("cache"); | ||
|
|
||
| // Add a console app project to the AppHost | ||
| var processor = builder.AddProject<Projects.MyConsoleApp>("processor") | ||
| .WithReference(redis); | ||
|
|
||
| builder.Build().Run(); | ||
| ``` | ||
|
|
||
| The `processor` project receives the `ConnectionStrings__cache` environment variable at runtime, just like any other project type. | ||
|
|
||
| <Aside type="note"> | ||
| The console app project must target `net10.0` or later and reference the service defaults project to benefit from telemetry and service discovery. For more information, see [Service defaults](/fundamentals/service-defaults/). | ||
| </Aside> | ||
|
|
||
| ## Service discovery in console apps | ||
|
|
||
| Non-ASP.NET Core console apps can use the same service discovery infrastructure as web apps. Add the appropriate packages and configure them during startup. | ||
|
|
||
| <Steps> | ||
|
|
||
| 1. Add the required packages to your console app: | ||
|
|
||
| ```xml title="XML — MyConsoleApp.csproj" | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" /> | ||
| ``` | ||
|
|
||
| Or reference the service defaults project from your solution: | ||
|
|
||
| ```xml title="XML — MyConsoleApp.csproj" | ||
| <ProjectReference Include="..\MyApp.ServiceDefaults\MyApp.ServiceDefaults.csproj" /> | ||
| ``` | ||
|
|
||
| 2. Configure the host in your console app's _Program.cs_: | ||
|
|
||
| ```csharp title="C# — Program.cs" | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| var builder = Host.CreateApplicationBuilder(args); | ||
|
|
||
| // Add service defaults (telemetry, health checks, service discovery) | ||
| builder.AddServiceDefaults(); | ||
|
|
||
| // Register your services | ||
| builder.Services.AddHttpClient<CatalogClient>( | ||
| client => client.BaseAddress = new Uri("https+http://catalog")); | ||
|
|
||
| var app = builder.Build(); | ||
| await app.RunAsync(); | ||
| ``` | ||
|
|
||
| 3. In the AppHost, reference the services this app depends on: | ||
|
|
||
| ```csharp title="C# — AppHost.cs" | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var catalog = builder.AddProject<Projects.CatalogService>("catalog"); | ||
|
|
||
| var processor = builder.AddProject<Projects.MyConsoleApp>("processor") | ||
| .WithReference(catalog) | ||
| .WaitFor(catalog); | ||
|
|
||
| builder.Build().Run(); | ||
| ``` | ||
|
|
||
| </Steps> | ||
|
|
||
| The `WithReference(catalog)` call injects service discovery configuration so `processor` can resolve `https+http://catalog` to the running catalog service endpoint. | ||
|
|
||
| ## Background worker services | ||
|
|
||
| Worker Service projects are the recommended pattern for long-running background tasks with Aspire. They use `IHostedService` (or `BackgroundService`) and integrate naturally with the .NET Generic Host. | ||
|
|
||
| ### Creating a Worker Service | ||
|
|
||
| ```csharp title="C# — DataProcessor.cs" | ||
| public class DataProcessor : BackgroundService | ||
| { | ||
| private readonly ILogger<DataProcessor> _logger; | ||
| private readonly IServiceProvider _services; | ||
|
|
||
| public DataProcessor(ILogger<DataProcessor> logger, IServiceProvider services) | ||
| { | ||
| _logger = logger; | ||
| _services = services; | ||
| } | ||
|
|
||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| while (!stoppingToken.IsCancellationRequested) | ||
| { | ||
| _logger.LogInformation("Processing data at: {Time}", DateTimeOffset.UtcNow); | ||
|
|
||
| await DoWorkAsync(stoppingToken); | ||
|
|
||
| await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken); | ||
| } | ||
| } | ||
|
|
||
| private async Task DoWorkAsync(CancellationToken stoppingToken) | ||
| { | ||
| // Your background work here | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```csharp title="C# — Program.cs" | ||
| var builder = Host.CreateApplicationBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
|
|
||
| // Register resource connections | ||
| builder.AddRedisClient("cache"); | ||
|
|
||
| // Register the background worker | ||
| builder.Services.AddHostedService<DataProcessor>(); | ||
|
|
||
| var app = builder.Build(); | ||
| await app.RunAsync(); | ||
| ``` | ||
|
|
||
| ### Adding to the AppHost | ||
|
|
||
| ```csharp title="C# — AppHost.cs" | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var cache = builder.AddRedis("cache"); | ||
| var db = builder.AddPostgres("postgres").AddDatabase("app-db"); | ||
|
|
||
| var worker = builder.AddProject<Projects.DataWorker>("data-worker") | ||
| .WithReference(cache) | ||
| .WithReference(db) | ||
| .WaitFor(db); | ||
|
|
||
| builder.Build().Run(); | ||
| ``` | ||
|
|
||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <LearnMore> | ||
| For more information on the Redis and PostgreSQL hosting integrations, see [Redis integration](/integrations/caching/redis/) and [PostgreSQL integration](/integrations/databases/postgres/postgres-get-started/). | ||
| </LearnMore> | ||
|
|
||
| ## Explicit start and console interaction | ||
|
|
||
| By default, all resources start automatically when the AppHost runs. For console apps that require interactive input (reading from stdin), or that should only run on demand, use `WithExplicitStart`. | ||
|
|
||
| ```csharp title="C# — AppHost.cs" | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var interactiveTool = builder.AddProject<Projects.MyInteractiveTool>("tool") | ||
| .WithExplicitStart(); | ||
|
|
||
| builder.Build().Run(); | ||
| ``` | ||
|
|
||
| With `WithExplicitStart`, the resource appears in the Aspire dashboard but doesn't start automatically. You can start it manually from the dashboard or programmatically. | ||
|
|
||
| <Aside type="caution"> | ||
| Interactive console apps that read from stdin (e.g., `Console.ReadLine()`) are not fully supported in the Aspire dashboard view. The Aspire dashboard captures and displays stdout/stderr, but does not support forwarding user input to the process's stdin. For interactive input scenarios, consider running the app independently or restructure to avoid stdin. | ||
| </Aside> | ||
|
|
||
| ## Viewing console output | ||
|
|
||
| Aspire captures stdout and stderr from all managed resources and displays them in the dashboard's **Console logs** page. Console apps output is fully visible, including colored text (ANSI escape codes are rendered). | ||
|
|
||
| <LearnMore> | ||
| For more information, see [Console logs page](/dashboard/explore/#console-logs-page). | ||
| </LearnMore> | ||
|
|
||
| For structured logging in console apps, use `ILogger` instead of `Console.Write`: | ||
|
|
||
| ```csharp title="C# — Program.cs" | ||
| // Prefer structured logging | ||
| logger.LogInformation("Processing item {ItemId}", itemId); | ||
|
|
||
| // Instead of | ||
| Console.WriteLine($"Processing item {itemId}"); | ||
| ``` | ||
|
|
||
| Structured logs appear in both the **Console** and **Structured logs** tabs of the dashboard, making them easier to search and filter. | ||
|
|
||
| ## Using connection strings without service defaults | ||
|
|
||
| If you can't use the service defaults project (for example, in a minimal console app without a host), you can access connection strings directly from environment variables: | ||
|
|
||
| ```csharp title="C# — Program.cs" | ||
| // For .NET apps, read from configuration | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| var config = new ConfigurationBuilder() | ||
| .AddEnvironmentVariables() | ||
| .Build(); | ||
|
|
||
| var redisConnection = config.GetConnectionString("cache"); | ||
| // Or equivalently: | ||
| var redisConnection2 = Environment.GetEnvironmentVariable("ConnectionStrings__cache"); | ||
| ``` | ||
|
|
||
| <Aside type="tip"> | ||
| When using configuration APIs directly, use `GetConnectionString("cache")` rather than reading `ConnectionStrings__cache` directly. The configuration system maps the double-underscore separator to the colon used by `IConfiguration`, making `ConnectionStrings:cache` and `ConnectionStrings__cache` equivalent. For more information, see [Resource naming conventions](/app-host/resource-naming/). | ||
| </Aside> | ||
|
|
||
| ## See also | ||
|
|
||
| - [Service defaults](/fundamentals/service-defaults/) | ||
| - [Service discovery](/fundamentals/service-discovery/) | ||
| - [Resource naming conventions](/app-host/resource-naming/) | ||
| - [Executable resources](/app-host/executable-resources/) | ||
| - [.NET Worker Service](https://learn.microsoft.com/dotnet/core/extensions/workers) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new sidebar items include only an
entranslation, but other items in the same AppHost section provide bothenandja. Please addjatranslations for these new entries (or follow the established pattern for this section) to keep locale coverage consistent.