C# AppHost project configuration
A C# AppHost is a standard .NET app that uses Microsoft.NET.Sdk. Two packages turn it into an AppHost:
- 📦 Aspire.Hosting.AppHost supplies the AppHost APIs and build targets.
- 📦 Aspire.Hosting.Dotnet supplies
AddDotnetProjectfor adding .NET projects and file-based apps as resources.
Add other hosting integrations, such as Redis or PostgreSQL, as additional packages.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> <!-- Omitted for brevity --> </PropertyGroup>
<ItemGroup> <PackageReference Include="Aspire.Hosting.AppHost" Version="17.0.0" /> <PackageReference Include="Aspire.Hosting.Dotnet" Version="17.0.0" /> </ItemGroup>
</Project>#:package Aspire.Hosting.AppHost@17.0.0#:package Aspire.Hosting.Dotnet@17.0.0
var builder = DistributedApplication.CreateBuilder(args);
// Omitted for brevityReference .NET projects
Section titled “Reference .NET projects”Add .NET projects to the AppHost with AddDotnetProject and the path to a project or file-based app. The AppHost doesn’t need a ProjectReference to the services it runs, so building the AppHost doesn’t build them. Keep the services in your solution and CI builds.
For more information, see .NET hosting.
Generated project metadata
Section titled “Generated project metadata”Legacy APIs such as AddProject<TProject> take a generated class from the Projects namespace instead of a path. The AppHost build generates a class for each ProjectReference marked with IsAspireProjectResource="true". Set ReferenceOutputAssembly="false" so the AppHost doesn’t compile against the service:
<ItemGroup> <ProjectReference Include="..\MyService\MyService.csproj" IsAspireProjectResource="true" ReferenceOutputAssembly="false" /></ItemGroup>The generated class name is based on the project name. To customize it, for example when two projects have the same name, set AspireProjectMetadataTypeName:
<ItemGroup> <ProjectReference Include="..\Microservice1\Presentation.Api\Presentation.Api.csproj" IsAspireProjectResource="true" ReferenceOutputAssembly="false" AspireProjectMetadataTypeName="MicroService1" /> <ProjectReference Include="..\Microservice2\Presentation.Api\Presentation.Api.csproj" IsAspireProjectResource="true" ReferenceOutputAssembly="false" AspireProjectMetadataTypeName="MicroService2" /></ItemGroup>This generates Projects.MicroService1 and Projects.MicroService2. A ProjectReference without IsAspireProjectResource="true" is an ordinary reference, such as a shared library.
AppHost build features
Section titled “AppHost build features”Orchestrator dependencies
Section titled “Orchestrator dependencies”The AppHost relies on the Aspire Dashboard and other orchestration dependencies, such as the developer control plane (DCP) packages. These dependencies are specific to the platform that the AppHost is built on.
When the AppHost project runs, the orchestrator relies on these dependencies to provide the necessary functionality to the AppHost. For more information, see Aspire orchestration overview.
Use the Aspire CLI bundle for orchestration dependencies
Section titled “Use the Aspire CLI bundle for orchestration dependencies”The AppHost build sources the binaries for these dependencies from the installed Aspire CLI bundle. Those binaries update when you update the Aspire CLI through aspire update --self.
C# AppHosts require the Aspire CLI bundle, so install the Aspire CLI before you build or run them. If AspireUseCliBundle is set to false, whether in the project, in Directory.Build.props, or through an environment variable, the build fails with error ASPIRE013, which can’t be suppressed. Remove that setting or set it to true. In Aspire versions before 17.0, AspireUseCliBundle defaults to false for all AppHosts, so set it to true to use the bundle.
With the CLI bundle:
- Your AppHost uses the DCP and Dashboard versions installed with the Aspire CLI.
- The AppHost build selects the executable used to launch the AppHost as follows:
- An explicit
AspireCliPathis always authoritative. - When
AspireCliInvocationModeis set toDnxorDnxPinned, the build selects a usablednxhost. - Otherwise, the default
Pathmode selects a compatibleaspireonPATH, then falls back to the Aspire CLI package paired withAspire.Hosting.AppHostthrough DNX.
- An explicit
AspireCliBundlePathisn’t an executable candidate. It explicitly identifies a bundle layout for DCP and Dashboard resolution.- If the resolved Aspire CLI has a bundle that hasn’t been extracted yet, the build runs
aspire setupthrough the selectedaspireor paired DNX invocation to extract it, then resolves the bundle again. This lets an AppHost build and launch from an IDE even when the CLI installation didn’t run the release install scripts. - If a required bundle layout can’t be resolved, the build emits error
ASPIRE009. Invalid explicitAspireCliPathandAspireCliBundlePathvalues remain authoritative and can produce this error. - If
DnxorDnxPinnedmode can’t finddnx, the run preflight emits errorASPIRE011when it prepares the launch command. A regular build doesn’t emit this diagnostic.
Running with aspire run or aspire start
Section titled “Running with aspire run or aspire start”When you start an AppHost with aspire run or aspire start, the CLI automatically passes the resolved bundle paths through the ASPIRE_DCP_PATH and ASPIRE_DASHBOARD_PATH environment variables.
Running with dotnet run
Section titled “Running with dotnet run”When you start the AppHost with dotnet run instead of aspire run, the AppHost resolves and invokes the Aspire CLI itself. Set AspireCliInvocationMode to control how that invocation happens:
Path(default) — resolves a compatibleaspirecommand fromAspireCliPath(if set) orPATH. If no compatible command is found, it falls back to invoking theAspire.Cliversion paired withAspire.Hosting.AppHostthroughdnx.Dnx— always invokes the Aspire CLI throughdnx, using the unversionedAspire.Clipackage. This honors an in-scope .NET local tool manifest (.config/dotnet-tools.json) if one pinsAspire.Cli, or resolves the latest published package when no manifest applies.DnxPinned— always invokes the Aspire CLI throughdnx, usingAspire.Cli@<version>pinned to the exactAspire.Cliversion paired withAspire.Hosting.AppHost. Use this when the CLI must match the AppHost package version exactly, ignoring any local tool manifest.
Set AspireCliInvocationMode in your AppHost .csproj:
<PropertyGroup> <AspireCliInvocationMode>Dnx</AspireCliInvocationMode></PropertyGroup>dotnet run hands the AppHost to aspire run, so use it only to run the AppHost locally. To generate deployment artifacts, use aspire publish instead of dotnet run --operation publish. An AppHost started in publish mode this way fails with an error that points you to aspire publish. For more information, see dotnet run --operation publish fails in publish mode.