-
Notifications
You must be signed in to change notification settings - Fork 155
Add comprehensive documentation for ExecutableResource and AddExecutable API #4188
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
Merged
+206
−0
Merged
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cdc178a
Initial plan
Copilot 5f5a0d0
Add new ExecutableResource documentation article
Copilot abf751c
Address review feedback: update date, fix list punctuation, replace A…
Copilot e6c968e
Fix punctuation for all complete sentences in lists throughout the do…
Copilot cb941af
Address review feedback: fix bullet punctuation, simplify method para…
Copilot 315ea45
Apply suggestions from code review
IEvangelist 96aec56
Apply suggestions from code review
IEvangelist c4360cf
Fix WithArgs usage throughout documentation - remove non-existent Wit…
Copilot c85e7c7
Apply suggestions from code review
IEvangelist c6b362c
Update docs/app-host/executable-resources.md
IEvangelist ece56b5
Update executable-resources.md
IEvangelist 06f822e
Combine resource dependency sections and fix production deployment text
Copilot 2906d40
Update executable-resources.md
IEvangelist 52a32df
Fix numbered list formatting to use all "1." as per Microsoft Writing…
Copilot 58d3b8e
Add ai-generated metadata to executable-resources documentation
Copilot 2c8615a
Change ai-usage metadata to ai-assisted
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
--- | ||
title: Host external executables in .NET Aspire | ||
description: Learn how to use ExecutableResource and AddExecutable to host external executable applications in your .NET Aspire app host. | ||
ms.date: 01/04/2025 | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
adegeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Host external executables in .NET Aspire | ||
|
||
In .NET Aspire, you can host external executable applications alongside your .NET projects using the <xref:Aspire.Hosting.ExecutableResourceBuilderExtensions.AddExecutable%2A> method. This capability is useful when you need to integrate non-.NET applications or tools into your distributed application, such as Node.js applications, Python scripts, or specialized CLI tools. | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## When to use executable resources | ||
|
||
Use executable resources when you need to: | ||
|
||
- Host non-.NET applications that don't have containerized equivalents | ||
- Integrate command-line tools or utilities into your application | ||
- Run external processes that other resources depend on | ||
- Develop with tools that provide local development servers | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Common examples include: | ||
|
||
- **Frontend development servers**: Tools like Azure Static Web Apps CLI, Vite, or webpack dev server | ||
- **Language-specific applications**: Node.js apps, Python scripts, or Go applications | ||
- **Database tools**: Migration utilities or database seeders | ||
- **Build tools**: Asset processors or code generators | ||
|
||
## Basic usage | ||
|
||
The <xref:Aspire.Hosting.ExecutableResourceBuilderExtensions.AddExecutable%2A> method requires a resource name, the executable path, and optionally command-line arguments and a working directory: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
// Basic executable without arguments | ||
var nodeApp = builder.AddExecutable("frontend", "node", "server.js", "."); | ||
|
||
// Executable with command-line arguments | ||
var pythonApp = builder.AddExecutable("api", "python", "-m", "uvicorn") | ||
.WithArgs("main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"); | ||
|
||
builder.Build().Run(); | ||
``` | ||
|
||
### Method parameters | ||
|
||
The <xref:Aspire.Hosting.ExecutableResourceBuilderExtensions.AddExecutable%2A> method accepts the following parameters: | ||
|
||
- **name**: A unique identifier for the resource | ||
- **command**: The executable name or path (for example, `"node"`, `"python"`, or `"/usr/bin/myapp"`) | ||
- **workingDirectory**: The directory where the executable should run | ||
- **args**: Optional command-line arguments passed to the executable | ||
|
||
## Configure command-line arguments | ||
|
||
You can provide command-line arguments in several ways: | ||
|
||
### Arguments in the AddExecutable call | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
// Arguments provided directly | ||
var app = builder.AddExecutable("swa-cli", "swa", "start", ".") | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.WithArgs("dist", "--api-location", "api", "--port", "4280"); | ||
``` | ||
|
||
### Arguments using WithArgs | ||
|
||
Use the <xref:Aspire.Hosting.ExecutableResourceBuilderExtensions.WithArgs%2A> method to add arguments after creating the resource: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var app = builder.AddExecutable("webpack-dev", "npm", ".", "run") | ||
.WithArgs("dev") | ||
.WithArgs("--", "--port", "3000"); | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
### Dynamic arguments with WithEnvironment | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
For arguments that depend on other resources, use environment variables: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var database = builder.AddPostgres("postgres").AddDatabase("mydb"); | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var migrator = builder.AddExecutable("migrator", "dotnet", ".", "run") | ||
.WithReference(database) | ||
.WithEnvironment(context => | ||
{ | ||
var connectionString = database.Resource.ConnectionStringExpression; | ||
context.EnvironmentVariables["CONNECTION_STRING"] = connectionString; | ||
}); | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Work with resource dependencies | ||
|
||
Executable resources can reference other resources and access their connection information: | ||
|
||
### Basic resource references | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var redis = builder.AddRedis("cache"); | ||
var postgres = builder.AddPostgres("postgres").AddDatabase("appdb"); | ||
|
||
var app = builder.AddExecutable("worker", "python", "worker.py", ".") | ||
.WithReference(redis) // Provides ConnectionStrings__cache | ||
.WithReference(postgres); // Provides ConnectionStrings__appdb | ||
``` | ||
|
||
### Access specific endpoint information | ||
|
||
For more control over how connection information is passed to your executable: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var redis = builder.AddRedis("cache"); | ||
|
||
var app = builder.AddExecutable("app", "node", "app.js", ".") | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.WithReference(redis) | ||
.WithEnvironment(context => | ||
{ | ||
// Provide individual connection details | ||
context.EnvironmentVariables["REDIS_HOST"] = redis.Resource.PrimaryEndpoint.Property(EndpointProperty.Host); | ||
context.EnvironmentVariables["REDIS_PORT"] = redis.Resource.PrimaryEndpoint.Property(EndpointProperty.Port); | ||
context.EnvironmentVariables["REDIS_URL"] = $"redis://{redis.Resource.PrimaryEndpoint}"; | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
``` | ||
|
||
## Practical example: Azure Static Web Apps CLI | ||
|
||
Here's a complete example using the Azure Static Web Apps CLI to host a frontend application with a backend API: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
// Backend API | ||
var api = builder.AddProject<Projects.MyApi>("api") | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.WithExternalHttpEndpoints(); | ||
|
||
// Frontend with Azure SWA CLI | ||
var frontend = builder.AddExecutable("swa", "swa", ".", "start") | ||
.WithArgs("dist", "--api-location", "http://localhost:5000", "--port", "4280") | ||
.WithEnvironment("SWA_CLI_API_URI", api.GetEndpoint("http")) | ||
.WithExplicitStart(); // Start manually after API is ready | ||
|
||
builder.Build().Run(); | ||
``` | ||
|
||
## Configure endpoints | ||
|
||
Executable resources can expose HTTP endpoints that other resources can reference: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var frontend = builder.AddExecutable("vite-dev", "npm", ".", "run") | ||
.WithArgs("dev", "--", "--port", "5173", "--host", "0.0.0.0") | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.WithHttpEndpoint(port: 5173, name: "http"); | ||
|
||
// Another service can reference the frontend | ||
var e2eTests = builder.AddExecutable("playwright", "npx", ".", "playwright") | ||
.WithArgs("test") | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.WithEnvironment("BASE_URL", frontend.GetEndpoint("http")); | ||
``` | ||
|
||
## Environment configuration | ||
|
||
Configure environment variables for your executable: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var app = builder.AddExecutable("api", "uvicorn", ".", "main:app") | ||
.WithArgs("--reload", "--host", "0.0.0.0") | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.WithEnvironment("DEBUG", "true") | ||
.WithEnvironment("LOG_LEVEL", "info") | ||
.WithEnvironment(context => | ||
{ | ||
// Dynamic environment variables | ||
context.EnvironmentVariables["START_TIME"] = DateTimeOffset.UtcNow.ToString(); | ||
}); | ||
``` | ||
|
||
## Publishing with PublishAsDockerfile | ||
|
||
For production deployment, executable resources need to be containerized. Use the <xref:Aspire.Hosting.ExecutableResourceBuilderExtensions.PublishAsDockerfile%2A> method to specify how the executable should be packaged: | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var app = builder.AddExecutable("frontend", "npm", ".", "start") | ||
.WithArgs("--port", "3000") | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.PublishAsDockerfile(); | ||
``` | ||
|
||
When you call `PublishAsDockerfile()`, .NET Aspire generates a Dockerfile during the publish process. You can customize this by providing your own Dockerfile: | ||
|
||
### Custom Dockerfile for publishing | ||
|
||
Create a `Dockerfile` in your executable's working directory: | ||
|
||
```dockerfile | ||
FROM node:18-alpine | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
WORKDIR /app | ||
COPY package*.json ./ | ||
RUN npm ci --only=production | ||
COPY . . | ||
EXPOSE 3000 | ||
CMD ["npm", "start"] | ||
``` | ||
|
||
Then reference it in your app host: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var app = builder.AddExecutable("frontend", "npm", ".", "start") | ||
.PublishAsDockerfile([new DockerfileBuildArg("NODE_ENV", "production")]); | ||
``` | ||
|
||
## Advanced scenarios | ||
|
||
### Multiple executable arguments | ||
|
||
For complex command lines, you can chain multiple `WithArgs` calls: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var app = builder.AddExecutable("complex-tool", "myapp", ".") | ||
.WithArgs("--config", "config.json") | ||
.WithArgs("--verbose") | ||
.WithArgs("--output", "/tmp/results") | ||
.WithArgs("--workers", "4"); | ||
``` | ||
|
||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### Conditional execution | ||
|
||
Use explicit start control for executables that should only run under certain conditions: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var database = builder.AddPostgres("postgres").AddDatabase("appdb"); | ||
|
||
var migrator = builder.AddExecutable("migrator", "dotnet", ".", "ef") | ||
.WithArgs("database", "update") | ||
.WithReference(database) | ||
.WithExplicitStart(); // Only run when manually started | ||
|
||
var api = builder.AddProject<Projects.MyApi>("api") | ||
.WithReference(database) | ||
.WaitFor(migrator); // API waits for migrator to complete | ||
``` | ||
|
||
## Best practices | ||
|
||
When working with executable resources: | ||
|
||
1. **Use explicit paths**: For better reliability, use full paths to executables when possible | ||
2. **Handle dependencies**: Use `WithReference` to establish proper dependency relationships | ||
3. **Configure explicit start**: Use `WithExplicitStart()` for executables that shouldn't start automatically | ||
4. **Prepare for deployment**: Always use `PublishAsDockerfile()` for production scenarios | ||
5. **Environment isolation**: Use environment variables rather than command-line arguments for sensitive configuration | ||
6. **Resource naming**: Use descriptive names that clearly identify the executable's purpose | ||
|
||
## See also | ||
|
||
- [App host overview](../fundamentals/app-host-overview.md) | ||
- [Add Dockerfiles to the app model](withdockerfile.md) | ||
- [Node.js apps in .NET Aspire](../get-started/build-aspire-apps-with-nodejs.md) | ||
- [Python apps in .NET Aspire](../get-started/build-aspire-apps-with-python.md) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.