DotnetProjectHostingExtensions Methods

ClassMethods9 members
Provides extension methods for adding SDK-style .NET projects and file-based C# apps (by path) to an Hosting.IDistributedApplicationBuilder.
AddDotnetProject(IDistributedApplicationBuilder, string, string)Section titled AddDotnetProject(IDistributedApplicationBuilder, string, string)extensionIResourceBuilder<DotnetProjectResource>
Adds an SDK-style .NET project or file-based C# app to the application model.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> AddDotnetProject(
this IDistributedApplicationBuilder builder,
string name,
string path)
{
// ...
}
}
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.
namestringThe name of the resource. This name will be used for service discovery when referenced in a dependency.
pathstringThe path to the file-based app file, project file, or project directory.
IResourceBuilder<DotnetProjectResource>A reference to the ApplicationModel.IResourceBuilder`1.

This overload adds a path to a file-based app ( .cs), an SDK-style C#, F#, or Visual Basic project ( .csproj, .fsproj, or .vbproj), or a directory containing exactly one supported project. Directories containing multiple supported projects require an explicit project filename. If the path is not an absolute path then it will be computed relative to the AppHost directory.

Visual Basic projects prefer My Project/launchSettings.json and fall back to Properties/launchSettings.json. F# scripts and non-SDK project systems are not supported.

Add a file-based app to the app model via a file path.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddDotnetProject("inventoryservice", @"..\InventoryService.cs");
builder.Build().Run();
AddDotnetProject(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>)Section titled AddDotnetProject(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>)extensionIResourceBuilder<DotnetProjectResource>
Adds an SDK-style .NET project or file-based C# app to the application model.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> AddDotnetProject(
this IDistributedApplicationBuilder builder,
string name,
string path,
Action<ProjectResourceOptions> configure)
{
// ...
}
}
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.
namestringThe name of the resource. This name will be used for service discovery when referenced in a dependency.
pathstringThe path to the file-based app file, project file, or project directory.
configureAction<ProjectResourceOptions>An action to configure the .NET project resource options.
IResourceBuilder<DotnetProjectResource>A reference to the ApplicationModel.IResourceBuilder`1.

This overload adds a path to a file-based app ( .cs), an SDK-style C#, F#, or Visual Basic project ( .csproj, .fsproj, or .vbproj), or a directory containing exactly one supported project. Directories containing multiple supported projects require an explicit project filename. If the path is not an absolute path then it will be computed relative to the AppHost directory.

Visual Basic projects prefer My Project/launchSettings.json and fall back to Properties/launchSettings.json. F# scripts and non-SDK project systems are not supported.

Add a file-based app to the app model via a file path.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddDotnetProject(
"inventoryservice",
@"..\InventoryService.cs",
o => o.LaunchProfileName = "https");
builder.Build().Run();
DisableForwardedHeaders(IResourceBuilder<DotnetProjectResource>)Section titled DisableForwardedHeaders(IResourceBuilder<DotnetProjectResource>)extensionIResourceBuilder<DotnetProjectResource>
Disables automatic forwarded-header configuration when publishing the .NET project.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> DisableForwardedHeaders(
this IResourceBuilder<DotnetProjectResource> builder)
{
// ...
}
}
builderIResourceBuilder<DotnetProjectResource>The .NET project resource builder.
IResourceBuilder<DotnetProjectResource>The same builder for chaining additional configuration.
ArgumentNullExceptionThrown when builder is null.
GetProjectMetadata(DotnetProjectResource)Section titled GetProjectMetadata(DotnetProjectResource)extensionIProjectMetadata
Gets the project metadata for the .NET project resource.
public static class DotnetProjectHostingExtensions
{
public static IProjectMetadata GetProjectMetadata(
this DotnetProjectResource projectResource)
{
// ...
}
}
projectResourceDotnetProjectResourceThe .NET project resource.
IProjectMetadataThe project metadata.
ArgumentNullExceptionThrown when projectResource is null.
InvalidOperationExceptionThe resource does not carry exactly one stable project metadata annotation.
Project metadata cannot be replaced after project defaults have been applied because launch settings, endpoints, and rebuild behavior are derived from that annotation.
WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, string, string)Section titled WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, string, string)extensionIResourceBuilder<DotnetProjectResource>
Adds an environment variable to the build process for a .NET project.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment(
this IResourceBuilder<DotnetProjectResource> builder,
string name,
string value)
{
// ...
}
}
builderIResourceBuilder<DotnetProjectResource>The .NET project resource builder.
namestringThe name of the environment variable.
valuestringThe value of the environment variable.
IResourceBuilder<DotnetProjectResource>A reference to the ApplicationModel.IResourceBuilder`1 for chaining additional configuration.
ArgumentNullException Thrown when builder, name, or value is null.
ArgumentException Thrown when name is empty.
DistributedApplicationException Thrown when builder represents a file-based C# app.

This method supports SDK-style project files ( .csproj, .fsproj, and .vbproj) only. File-based C# apps ( .cs) do not support build-only environment variables.

The variable is available while Aspire builds the project but is not added to the environment of the launched project. Use WithEnvironment separately when the same variable is also needed at runtime.

Configuring a build environment causes Aspire to build this project separately from traversal groups.

IDE launches require support for KnownLaunchConfigurationTypes.ProjectWithExternalBuild so the launcher can consume the same build context. Aspire rejects legacy project-only IDE sessions because they cannot reliably locate output produced with project-specific build settings.

When publishing a container, do not use this API to set MSBuild properties that control the output artifact's identity, destination, format, or target platform. Aspire rejects those properties because downstream publishing steps use the values configured with WithContainerBuildOptions.

Do not use this API for secrets. Aspire carries the value in build process environments and, for external-build-capable IDEs, launch metadata. The value can appear in build diagnostics. Protected temporary MSBuild response files preserve global-property semantics without exposing values in process command lines, but they are not a general-purpose secret transport.

Configure an environment variable that selects a custom build output:

builder.AddDotnetProject("worker", "../Worker/Worker.csproj")
.WithBuildEnvironment("BUILD_FLAVOR", "custom");
WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Action<EnvironmentCallbackContext>)Section titled WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Action<EnvironmentCallbackContext>)extensionIResourceBuilder<DotnetProjectResource>
Adds a callback that configures build-only environment variables for a .NET project.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment(
this IResourceBuilder<DotnetProjectResource> builder,
Action<EnvironmentCallbackContext> callback)
{
// ...
}
}
builderIResourceBuilder<DotnetProjectResource>The .NET project resource builder.
callbackAction<EnvironmentCallbackContext>The callback that configures the build environment.
IResourceBuilder<DotnetProjectResource>A reference to the ApplicationModel.IResourceBuilder`1 for chaining additional configuration.
ArgumentNullException Thrown when builder or callback is null.
DistributedApplicationException Thrown when builder represents a file-based C# app.

This method supports SDK-style project files ( .csproj, .fsproj, and .vbproj) only. File-based C# apps ( .cs) do not support build-only environment variables.

IDE launches require support for KnownLaunchConfigurationTypes.ProjectWithExternalBuild so the launcher can consume the same build context. Aspire rejects legacy project-only IDE sessions because they cannot reliably locate output produced with project-specific build settings.

When publishing a container, do not use this API to set MSBuild properties that control the output artifact's identity, destination, format, or target platform. Aspire rejects those properties because downstream publishing steps use the values configured with WithContainerBuildOptions.

Values configured by this callback are not added to the environment of the launched project. Do not use this API for secrets because Aspire carries the values in build process environments and, for external-build-capable IDEs, launch metadata. Protected temporary MSBuild response files preserve global-property semantics without exposing values in process command lines, but the values can appear in build diagnostics.

WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EnvironmentCallbackContext, Task>)Section titled WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EnvironmentCallbackContext, Task>)extensionIResourceBuilder<DotnetProjectResource>
Adds an asynchronous callback that configures build-only environment variables for a .NET project.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment(
this IResourceBuilder<DotnetProjectResource> builder,
Func<EnvironmentCallbackContext, Task> callback)
{
// ...
}
}
builderIResourceBuilder<DotnetProjectResource>The .NET project resource builder.
callbackFunc<EnvironmentCallbackContext, Task>The callback that configures the build environment.
IResourceBuilder<DotnetProjectResource>A reference to the ApplicationModel.IResourceBuilder`1 for chaining additional configuration.
ArgumentNullException Thrown when builder or callback is null.
DistributedApplicationException Thrown when builder represents a file-based C# app.

This method supports SDK-style project files ( .csproj, .fsproj, and .vbproj) only. File-based C# apps ( .cs) do not support build-only environment variables.

IDE launches require support for KnownLaunchConfigurationTypes.ProjectWithExternalBuild so the launcher can consume the same build context. Aspire rejects legacy project-only IDE sessions because they cannot reliably locate output produced with project-specific build settings.

When publishing a container, do not use this API to set MSBuild properties that control the output artifact's identity, destination, format, or target platform. Aspire rejects those properties because downstream publishing steps use the values configured with WithContainerBuildOptions.

Values configured by this callback are not added to the environment of the launched project. Do not use this API for secrets because Aspire carries the values in build process environments and, for external-build-capable IDEs, launch metadata. Protected temporary MSBuild response files preserve global-property semantics without exposing values in process command lines, but the values can appear in build diagnostics.

WithEndpointsInEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EndpointAnnotation, bool>)Section titled WithEndpointsInEnvironment(IResourceBuilder<DotnetProjectResource>, Func<EndpointAnnotation, bool>)extensionIResourceBuilder<DotnetProjectResource>
Configures which endpoints contribute environment variables for the .NET project.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> WithEndpointsInEnvironment(
this IResourceBuilder<DotnetProjectResource> builder,
Func<EndpointAnnotation, bool> filter)
{
// ...
}
}
builderIResourceBuilder<DotnetProjectResource>The .NET project resource builder.
filterFunc<EndpointAnnotation, bool>A filter that returns true for endpoints whose environment variables should be injected.
IResourceBuilder<DotnetProjectResource>The same builder for chaining additional configuration.
ArgumentNullExceptionThrown when builder or filter is null.
Filters compose: an endpoint is included only when every configured filter accepts it.
WithReplicas(IResourceBuilder<DotnetProjectResource>, int)Section titled WithReplicas(IResourceBuilder<DotnetProjectResource>, int)extensionIResourceBuilder<DotnetProjectResource>
Configures the number of replicas of the .NET project.
public static class DotnetProjectHostingExtensions
{
public static IResourceBuilder<DotnetProjectResource> WithReplicas(
this IResourceBuilder<DotnetProjectResource> builder,
int replicas)
{
// ...
}
}
builderIResourceBuilder<DotnetProjectResource>The .NET project resource builder.
replicasintThe number of replicas.
IResourceBuilder<DotnetProjectResource>The same builder for chaining additional configuration.
ArgumentNullExceptionThrown when builder is null.