C# file-based apps
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:
aspire add Aspire.Hosting.DotnetWhen to use file-based apps
Section titled “When to use file-based apps”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.
Add C# app resource
Section titled “Add C# app resource”Call AddDotnetProject with a resource name and the relative path to a .cs file. The path is resolved relative to the AppHost directory:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
await builder.addDotnetProject("worker", "../worker/Program.cs");
await builder.build().run();var builder = DistributedApplication.CreateBuilder(args);
builder.AddDotnetProject("worker", "../worker/Program.cs");
builder.Build().Run();package main
import ( "log"
"apphost/modules/aspire")
builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
builder.AddDotnetProject("worker", "../worker/Program.cs")
app, err := builder.Build()if err != nil { log.Fatal(aspire.FormatError(err))}if err := app.Run(); err != nil { log.Fatal(aspire.FormatError(err))}You can also point to a .csproj file or a directory that contains one:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
// Point to a .csproj fileawait builder.addDotnetProject("api", "../api/Api.csproj");
// Point to a directory containing a .csproj fileawait builder.addDotnetProject("frontend", "../frontend/");
await builder.build().run();var builder = DistributedApplication.CreateBuilder(args);
// Point to a .csproj filebuilder.AddDotnetProject("api", "../api/Api.csproj");
// Point to a directory containing a .csproj filebuilder.AddDotnetProject("frontend", "../frontend/");
builder.Build().Run();package main
import ( "log"
"apphost/modules/aspire")
builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
builder.AddDotnetProject("api", "../api/Api.csproj")builder.AddDotnetProject("frontend", "../frontend/")
app, err := builder.Build()if err != nil { log.Fatal(aspire.FormatError(err))}if err := app.Run(); err != nil { log.Fatal(aspire.FormatError(err))}For supported project paths, build behavior, and publishing, see the .NET hosting reference.
Add resource dependencies
Section titled “Add resource dependencies”File-based apps integrate fully with Aspire’s resource model. You can reference other resources and configure service discovery just like any other resource:
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();var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");var db = builder.AddPostgres("pg").AddDatabase("mydb");
builder.AddDotnetProject("worker", "../worker/Program.cs") .WithReference(cache) .WithReference(db) .WithExternalHttpEndpoints();
builder.Build().Run();package main
import ( "log"
"apphost/modules/aspire")
builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
cache := builder.AddRedis("cache")db := builder.AddPostgres("pg").AddDatabase("mydb")
worker := builder.AddDotnetProject("worker", "../worker/Program.cs")worker.WithReference(cache)worker.WithReference(db)worker.WithExternalHttpEndpoints()
app, err := builder.Build()if err != nil { log.Fatal(aspire.FormatError(err))}if err := app.Run(); err != nil { log.Fatal(aspire.FormatError(err))}The file-based app receives connection strings and service discovery information through environment variables, like other resources added with AddDotnetProject.
Configure options
Section titled “Configure options”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:
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();var builder = DistributedApplication.CreateBuilder(args);
builder.AddDotnetProject("api", "../api/Api.cs", options =>{ options.LaunchProfileName = "https"; options.ExcludeLaunchProfile = false; options.ExcludeKestrelEndpoints = false;});
builder.Build().Run();package main
import ( "log"
"apphost/modules/aspire")
builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
builder.AddDotnetProject("api", "../api/Api.cs", aspire.AddDotnetProjectOptionsValue(&aspire.DotnetProjectOptions{ LaunchProfileName: aspire.StringPtr("https"), ExcludeLaunchProfile: aspire.BoolPtr(false), ExcludeKestrelEndpoints: aspire.BoolPtr(false),}))
app, err := builder.Build()if err != nil { log.Fatal(aspire.FormatError(err))}if err := app.Run(); err != nil { log.Fatal(aspire.FormatError(err))}The following options are available:
| Option | Description |
|---|---|
LaunchProfileName | The name of the launch profile to use from launchSettings.json. |
ExcludeLaunchProfile | When true, ignores launch profile settings. |
ExcludeKestrelEndpoints | When true, doesn’t automatically add Kestrel endpoints. |
File-based AppHost
Section titled “File-based AppHost”You can also write the AppHost itself as a file-based C# app. Reference the AppHost packages with #:package directives:
#: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.
Learn more about C# AppHost project configuration.
IDE debugging behavior
Section titled “IDE debugging behavior”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.
Limitations
Section titled “Limitations”Be aware of the following limitations:
- Single-file only — file-based apps are limited to a single
.csfile per resource. - .NET 10 SDK required — file-based app execution requires the .NET 10 SDK or later.