diff --git a/src/frontend/config/sidebar/integrations.topics.ts b/src/frontend/config/sidebar/integrations.topics.ts index 73b3ae413..8d7edeba0 100644 --- a/src/frontend/config/sidebar/integrations.topics.ts +++ b/src/frontend/config/sidebar/integrations.topics.ts @@ -1365,6 +1365,20 @@ export const integrationTopics: StarlightSidebarTopicsUserConfig = { }, ], }, + { + label: '.NET / C#', + collapsed: true, + items: [ + { + label: 'Get started', + slug: 'integrations/frameworks/dotnet/dotnet-get-started', + }, + { + label: 'Set up .NET / C# apps in the AppHost', + slug: 'integrations/frameworks/dotnet/dotnet-host', + }, + ], + }, { label: 'Go', collapsed: true, diff --git a/src/frontend/src/content/docs/integrations/frameworks/dotnet/dotnet-get-started.mdx b/src/frontend/src/content/docs/integrations/frameworks/dotnet/dotnet-get-started.mdx new file mode 100644 index 000000000..d7489189c --- /dev/null +++ b/src/frontend/src/content/docs/integrations/frameworks/dotnet/dotnet-get-started.mdx @@ -0,0 +1,59 @@ +--- +title: Get started with the .NET / C# app integration +description: Understand how the Aspire Dotnet hosting integration runs C# projects and file-based apps by path from your AppHost and how to choose the right setup path. +--- + +import { LinkButton, Steps } from '@astrojs/starlight/components'; +import ThemeImage from '@components/ThemeImage.astro'; +import csharpIcon from '@assets/icons/csharp.svg'; + + + +The Aspire `Aspire.Hosting.Dotnet` integration lets you add C# projects and file-based C# apps to your AppHost **by path**, without referencing a project from the AppHost's own solution. It's the C# peer of the `Aspire.Hosting.Go`, `Aspire.Hosting.Python`, and `Aspire.Hosting.JavaScript` hosting integrations. + +:::caution[Experimental] +`AddDotnetProject` / `addDotnetProject` is experimental and exposed under the `ASPIREDOTNETPROJECT001` diagnostic. Its API surface may change in future releases. +::: + +## Why use the Dotnet hosting integration + +Adding C# apps by path through `Aspire.Hosting.Dotnet` gives you: + +- **Path-based orchestration for C#.** Model a C# project or a file-based `.cs` app in the AppHost the same way you'd model a Go, Python, or JavaScript app added by path, without adding the project to the AppHost's solution. +- **File-based app support.** Add a single `.cs` file-based app (requires .NET 10 or later) directly, matching the file-based apps experience for `dotnet run --file`. +- **Familiar launch and configuration behavior.** Endpoints, environment variables, and service discovery are configured from the project's `launchSettings.json` and Kestrel configuration, matching `AddProject`. + +## How the pieces fit together + +The Dotnet integration is a **hosting integration**. You install it in the AppHost and use `AddDotnetProject` / `addDotnetProject` to add a C# project or file-based app resource by path. + + + +1. ### Set up C# apps in the AppHost + + Add the `Aspire.Hosting.Dotnet` hosting integration to your AppHost, then use `AddDotnetProject` / `addDotnetProject` to model a C# project or file-based app resource by path. The host reference covers package installation, path resolution, and launch behavior. + + + Set up .NET / C# apps in the AppHost + + + + +## See also + +- [.NET documentation](https://learn.microsoft.com/dotnet/) +- [.NET / C# AppHost setup reference](/integrations/frameworks/dotnet/dotnet-host/) +- [Aspire integrations overview](/integrations/overview/) diff --git a/src/frontend/src/content/docs/integrations/frameworks/dotnet/dotnet-host.mdx b/src/frontend/src/content/docs/integrations/frameworks/dotnet/dotnet-host.mdx new file mode 100644 index 000000000..e08076ff5 --- /dev/null +++ b/src/frontend/src/content/docs/integrations/frameworks/dotnet/dotnet-host.mdx @@ -0,0 +1,193 @@ +--- +title: Set up .NET / C# apps in the AppHost +seoTitle: 'Set up .NET / C# apps in the Aspire AppHost: hosting integration' +description: Learn how to use the Aspire Dotnet hosting integration to add C# projects and file-based C# apps by path in an Aspire solution. +--- + +import { Tabs, TabItem } from '@astrojs/starlight/components'; +import LearnMore from '@components/LearnMore.astro'; +import ThemeImage from '@components/ThemeImage.astro'; +import csharpIcon from '@assets/icons/csharp.svg'; + + + +This article is the reference for the Aspire Dotnet hosting integration. It enumerates the AppHost APIs — with examples for both `AppHost.cs` and `apphost.mts` — that you use to add C# projects and file-based C# apps **by path** in your [`AppHost`](/get-started/app-host/) project. + +If you're new to the Dotnet integration, start with the [Get started with the .NET / C# app integration](/integrations/frameworks/dotnet/dotnet-get-started/) guide. + +:::caution[Experimental] +`AddDotnetProject` / `addDotnetProject` is experimental and exposed under the `ASPIREDOTNETPROJECT001` diagnostic. Its API surface may change in future releases. +::: + +:::note[Prerequisites] +The **.NET SDK** must be available on the `PATH` of the machine running the AppHost. File-based C# apps (`.cs`) require **.NET 10 or later**. +::: + +## Installation + +To start building an Aspire app that adds a C# project or file-based app by path, install the [📦 Aspire.Hosting.Dotnet](https://www.nuget.org/packages/Aspire.Hosting.Dotnet) NuGet package: + + + + +```bash title="Terminal" +aspire add Aspire.Hosting.Dotnet +``` + + + Learn more about [`aspire add`](/reference/cli/commands/aspire-add/) in the + command reference. + + +Or, choose a manual installation approach: + +```csharp title="AppHost.cs" +#:package Aspire.Hosting.Dotnet@* +``` + +```xml title="AppHost.csproj" + +``` + + + + +```bash title="Terminal" +aspire add Aspire.Hosting.Dotnet +``` + + + Learn more about [`aspire add`](/reference/cli/commands/aspire-add/) in the + command reference. + + +This updates your `aspire.config.json` with the Dotnet hosting integration package: + +```json title="aspire.config.json" ins={3} +{ + "packages": { + "Aspire.Hosting.Dotnet": "13.5.0" + } +} +``` + + + + +## Add a C# project or file-based app by path + +Use `AddDotnetProject` / `addDotnetProject` to add a C# project or file-based app resource by path. The `path` argument can point at a project file (`.csproj`), a directory containing a single `.csproj`, or a file-based app (`.cs`). If the path isn't absolute, it's computed relative to the AppHost directory. + + + + +```csharp title="AppHost.cs" +var builder = DistributedApplication.CreateBuilder(args); + +var api = builder.AddDotnetProject("api", "../api/api.csproj") + .WithHttpEndpoint(port: 8080) + .WithExternalHttpEndpoints(); + +builder.Build().Run(); +``` + + + + +```typescript title="apphost.mts" +import { createBuilder } from './.aspire/modules/aspire.mjs'; + +const builder = await createBuilder(); + +const api = await builder.addDotnetProject('api', '../api/api.csproj'); +await api.withHttpEndpoint({ port: 8080 }); +await api.withExternalHttpEndpoints(); + +await builder.build().run(); +``` + + + + +The resource launches with `dotnet run --project ` for a project file, or `dotnet run --file ` for a file-based app. Endpoints, environment variables, and service discovery are configured from the project's `launchSettings.json` and Kestrel configuration, matching `AddProject`. + +## Add a file-based C# app + +A file-based app is a single `.cs` file run directly with `dotnet run --file`, without a `.csproj`. File-based apps require **.NET 10 or later**. + + + + +```csharp title="AppHost.cs" +var builder = DistributedApplication.CreateBuilder(args); + +builder.AddDotnetProject("inventoryservice", @"..\InventoryService.cs"); + +builder.Build().Run(); +``` + + + + +```typescript title="apphost.mts" +import { createBuilder } from './.aspire/modules/aspire.mjs'; + +const builder = await createBuilder(); + +await builder.addDotnetProject('inventoryservice', '../InventoryService.cs'); + +await builder.build().run(); +``` + + + + +## Configure options + +Pass a configuration action to set additional options such as the launch profile: + + + + +```csharp title="AppHost.cs" +var builder = DistributedApplication.CreateBuilder(args); + +builder.AddDotnetProject("inventoryservice", @"..\InventoryService.cs", o => o.LaunchProfileName = "https"); + +builder.Build().Run(); +``` + + + + +```typescript title="apphost.mts" +import { createBuilder } from './.aspire/modules/aspire.mjs'; + +const builder = await createBuilder(); + +await builder.addDotnetProject('inventoryservice', '../InventoryService.cs', { + launchProfileName: 'https', +}); + +await builder.build().run(); +``` + + + + +The resource is added as an [`ExecutableResource`](/app-host/executable-resources/) rather than a `ProjectResource`, so it doesn't need to be referenced from the AppHost's own solution. Before the resource starts, Aspire validates that the path resolves to a `.csproj` or `.cs` file (or a directory containing a single `.csproj`) and, for file-based apps, that the active .NET SDK version is 10 or later. + +## See also + +- [.NET documentation](https://learn.microsoft.com/dotnet/) +- [Get started with the .NET / C# app integration](/integrations/frameworks/dotnet/dotnet-get-started/) +- [Aspire integrations overview](/integrations/overview/) +- [Aspire GitHub repo](https://github.com/microsoft/aspire) diff --git a/src/frontend/src/content/docs/languages-and-runtimes/index.mdx b/src/frontend/src/content/docs/languages-and-runtimes/index.mdx index 3d399358b..f0b4c86db 100644 --- a/src/frontend/src/content/docs/languages-and-runtimes/index.mdx +++ b/src/frontend/src/content/docs/languages-and-runtimes/index.mdx @@ -49,7 +49,7 @@ The sections below use the labels that are most helpful when you're trying to an Use Aspire with C# projects, file-based apps, and other .NET workloads. - Start with [C# file-based apps](/integrations/dotnet/csharp-file-based-apps/) or [Project resources](/integrations/dotnet/project-resources/). + Start with [C# file-based apps](/integrations/dotnet/csharp-file-based-apps/) or [Project resources](/integrations/dotnet/project-resources/). To add a C# project or file-based app **by path** without referencing it from the AppHost's solution, use the [.NET / C# integration](/integrations/frameworks/dotnet/dotnet-get-started/). diff --git a/src/frontend/src/data/integration-docs.json b/src/frontend/src/data/integration-docs.json index a12098aaa..f1247d023 100644 --- a/src/frontend/src/data/integration-docs.json +++ b/src/frontend/src/data/integration-docs.json @@ -167,6 +167,10 @@ "match": "Aspire.Hosting.Docker", "href": "/integrations/compute/docker/" }, + { + "match": "Aspire.Hosting.Dotnet", + "href": "/integrations/frameworks/dotnet/dotnet-get-started/" + }, { "match": "Aspire.Hosting.Elasticsearch", "href": "/integrations/databases/elasticsearch/elasticsearch-get-started/"