इसे छोड़कर कंटेंट पर जाएं

C# file-based apps

यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।

C# logo

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 AddCSharpApp to add these lightweight apps to your AppHost alongside traditional projects, containers, and executables.

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 production workloads or apps with multiple source files, use AddProject<T> with a standard .csproj project instead.

Call AddCSharpApp with a resource name and the relative path to a .cs file:

C# — AppHost.cs
#pragma warning disable ASPIRECSHARPAPPS001
var builder = DistributedApplication.CreateBuilder(args);
builder.AddCSharpApp("worker", "../worker/Program.cs");
builder.Build().Run();

The path is resolved relative to the AppHost directory. You can also point to a .csproj file or a directory that contains one:

C# — AppHost.cs
#pragma warning disable ASPIRECSHARPAPPS001
var builder = DistributedApplication.CreateBuilder(args);
// Point to a .csproj file
builder.AddCSharpApp("api", "../api/Api.csproj");
// Point to a directory containing a .csproj file
builder.AddCSharpApp("frontend", "../frontend/");
builder.Build().Run();

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

C# — AppHost.cs
#pragma warning disable ASPIRECSHARPAPPS001
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var db = builder.AddPostgres("pg").AddDatabase("mydb");
builder.AddCSharpApp("worker", "../worker/Program.cs")
.WithReference(cache)
.WithReference(db)
.WithExternalHttpEndpoints();
builder.Build().Run();

The file-based app receives connection strings and service discovery information through environment variables, exactly like projects added with AddProject<T>.

The AddCSharpApp method accepts an optional Action<ProjectResourceOptions> parameter to configure launch settings:

C# — AppHost.cs
#pragma warning disable ASPIRECSHARPAPPS001
var builder = DistributedApplication.CreateBuilder(args);
builder.AddCSharpApp("api", "../api/Api.cs", options =>
{
options.LaunchProfileName = "https";
options.ExcludeLaunchProfile = false;
options.ExcludeKestrelEndpoints = false;
});
builder.Build().Run();

The available options are the same as those used with AddProject<T>:

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 app using the #:sdk directive:

C# — apphost.cs
#:sdk Aspire.AppHost.Sdk@13.1.0
#:package Aspire.Hosting.Redis@13.1.0
#pragma warning disable ASPIRECSHARPAPPS001
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
builder.AddCSharpApp("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.

Since this feature is experimental, be aware of the following limitations:

  • Experimental status — the API is marked with [Experimental("ASPIRECSHARPAPPS001")] and may change in future releases.
  • 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.
  • No deployment support — file-based apps are designed for local development scenarios.

To use AddCSharpApp, suppress the ASPIRECSHARPAPPS001 diagnostic using one of these methods:

In code with a pragma directive:

C# — AppHost.cs
#pragma warning disable ASPIRECSHARPAPPS001
builder.AddCSharpApp("worker", "../worker/Program.cs");
#pragma warning restore ASPIRECSHARPAPPS001

In your project file with NoWarn:

C# project file
<PropertyGroup>
<NoWarn>$(NoWarn);ASPIRECSHARPAPPS001</NoWarn>
</PropertyGroup>

In an .editorconfig file:

.editorconfig
[*.{cs,vb}]
dotnet_diagnostic.ASPIRECSHARPAPPS001.severity = none