Aspire

C# file-based apps

C# logo

This article explains how to add C# file-based app resources to your AppHost with the stable Aspire.Hosting.Dotnet integration. The examples cover C#, TypeScript, and Go AppHosts.

C# file-based applications let you run single .cs files without a .csproj project file. This feature, built on .NET 10 SDK’s file-based app support, integrates directly with Aspire’s orchestration. Use AddDotnetProjectaddDotnetProjectAddDotnetProject to add a .cs file, .csproj file, or directory containing a project.

Add the integration to your AppHost before using the examples:

Terminal
aspire add Aspire.Hosting.Dotnet

Use file-based apps when you need to:

  • Prototype a service quickly without project scaffolding.
  • Run simple worker processes or background tasks.
  • Create lightweight APIs with minimal ceremony.
  • Experiment with Aspire integrations in a single file.

For apps with multiple source files, use AddDotnetProject with a .csproj path.

Call AddDotnetProject with a resource name and the relative path to a .cs file. The path is resolved relative to the AppHost directory:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
await builder.addDotnetProject("worker", "../worker/Program.cs");
await builder.build().run();

You can also point to a .csproj file or a directory that contains one:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
// Point to a .csproj file
await builder.addDotnetProject("api", "../api/Api.csproj");
// Point to a directory containing a .csproj file
await builder.addDotnetProject("frontend", "../frontend/");
await builder.build().run();

For supported project paths, build behavior, and publishing, see the .NET hosting reference.

File-based apps integrate fully with Aspire’s resource model. You can reference other resources and configure service discovery just like any other resource:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const cache = await builder.addRedis("cache");
const pg = await builder.addPostgres("pg");
const db = await pg.addDatabase("mydb");
await builder.addDotnetProject("worker", "../worker/Program.cs")
.withReference(cache)
.withReference(db)
.withExternalHttpEndpoints();
await builder.build().run();

The file-based app receives connection strings and service discovery information through environment variables, like other resources added with AddDotnetProject.

AddDotnetProject accepts optional launch settings. The following example selects a launch profile and explicitly keeps endpoint inference from the launch profile and Kestrel configuration enabled:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
await builder.addDotnetProject("api", "../api/Api.cs", {
launchProfileName: "https",
excludeLaunchProfile: false,
excludeKestrelEndpoints: false,
});
await builder.build().run();

The following options are available:

OptionDescription
LaunchProfileNameThe name of the launch profile to use from launchSettings.json.
ExcludeLaunchProfileWhen true, ignores launch profile settings.
ExcludeKestrelEndpointsWhen true, doesn’t automatically add Kestrel endpoints.

You can also write the AppHost itself as a file-based C# app. Reference the AppHost packages with #:package directives:

apphost.cs
#:package Aspire.Hosting.AppHost@17.0.0
#:package Aspire.Hosting.Redis@17.0.0
#:package Aspire.Hosting.Dotnet@17.0.0
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
builder.AddDotnetProject("worker", "../worker/Program.cs")
.WithReference(cache);
builder.Build().Run();

Use #:package directives to add NuGet package references directly in the source file instead of a .csproj.

Visual Studio can only launch programs that are loaded as projects in the solution. A file-based .cs resource isn’t a loaded project, so when the AppHost runs under a Visual Studio debug session, Aspire runs the file-based app as a plain process (dotnet run --file) instead of handing it to the IDE. The resource still starts and joins the AppHost’s resource graph — it just doesn’t get IDE-managed debugging in that scenario.

The Visual Studio Code extension explicitly advertises support for launching file-based apps, so file-based resources debug normally when the AppHost runs under VS Code.

Be aware of the following limitations:

  • Single-file only — file-based apps are limited to a single .cs file per resource.
  • .NET 10 SDK required — file-based app execution requires the .NET 10 SDK or later.