DotnetProjectHostingExtensions Methods
Hosting.IDistributedApplicationBuilder . AddDotnetProject(IDistributedApplicationBuilder, string, string) Section titled AddDotnetProject(IDistributedApplicationBuilder, string, string)extensionIResourceBuilder<DotnetProjectResource> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> AddDotnetProject( this IDistributedApplicationBuilder builder, string name, string path) { // ... }}Parameters
builderIDistributedApplicationBuilder The 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.Returns
IResourceBuilder<DotnetProjectResource> A reference to the ApplicationModel.IResourceBuilder`1 .Remarks
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/ and fall back to Properties/. F# scripts and non-SDK project systems are not supported.
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> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> AddDotnetProject( this IDistributedApplicationBuilder builder, string name, string path, Action<ProjectResourceOptions> configure) { // ... }}Parameters
builderIDistributedApplicationBuilder The 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.Returns
IResourceBuilder<DotnetProjectResource> A reference to the ApplicationModel.IResourceBuilder`1 .Remarks
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/ and fall back to Properties/. F# scripts and non-SDK project systems are not supported.
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> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> DisableForwardedHeaders( this IResourceBuilder<DotnetProjectResource> builder) { // ... }}Parameters
builderIResourceBuilder<DotnetProjectResource> The .NET project resource builder.Returns
IResourceBuilder<DotnetProjectResource> The same builder for chaining additional configuration.Exceptions
ArgumentNullExceptionThrown when builder is null.GetProjectMetadata(DotnetProjectResource) Section titled GetProjectMetadata(DotnetProjectResource)extensionIProjectMetadata public static class DotnetProjectHostingExtensions{ public static IProjectMetadata GetProjectMetadata( this DotnetProjectResource projectResource) { // ... }}Parameters
projectResource DotnetReturns
IProjectMetadata The project metadata.Exceptions
ArgumentNullExceptionThrown when projectResource is null.InvalidOperationExceptionThe resource does not carry exactly one stable project metadata annotation.Remarks
WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, string, string) Section titled WithBuildEnvironment(IResourceBuilder<DotnetProjectResource>, string, string)extensionIResourceBuilder<DotnetProjectResource> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment( this IResourceBuilder<DotnetProjectResource> builder, string name, string value) { // ... }}Parameters
builderIResourceBuilder<DotnetProjectResource> The .NET project resource builder.namestringThe name of the environment variable.valuestringThe value of the environment variable.Returns
IResourceBuilder<DotnetProjectResource> A reference to the ApplicationModel.IResourceBuilder`1 for chaining additional configuration.Exceptions
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. Remarks
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 With 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 Known 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 With.
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.
Examples
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> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment( this IResourceBuilder<DotnetProjectResource> builder, Action<EnvironmentCallbackContext> callback) { // ... }}Parameters
builderIResourceBuilder<DotnetProjectResource> The .NET project resource builder.callbackAction<EnvironmentCallbackContext> The callback that configures the build environment.Returns
IResourceBuilder<DotnetProjectResource> A reference to the ApplicationModel.IResourceBuilder`1 for chaining additional configuration.Exceptions
ArgumentNullException Thrown when builder or callback is null. DistributedApplicationException Thrown when builder represents a file-based C# app. Remarks
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 Known 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 With.
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> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> WithBuildEnvironment( this IResourceBuilder<DotnetProjectResource> builder, Func<EnvironmentCallbackContext, Task> callback) { // ... }}Parameters
builderIResourceBuilder<DotnetProjectResource> The .NET project resource builder.callbackFunc<EnvironmentCallbackContext, Task> The callback that configures the build environment.Returns
IResourceBuilder<DotnetProjectResource> A reference to the ApplicationModel.IResourceBuilder`1 for chaining additional configuration.Exceptions
ArgumentNullException Thrown when builder or callback is null. DistributedApplicationException Thrown when builder represents a file-based C# app. Remarks
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 Known 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 With.
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> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> WithEndpointsInEnvironment( this IResourceBuilder<DotnetProjectResource> builder, Func<EndpointAnnotation, bool> filter) { // ... }}Parameters
builderIResourceBuilder<DotnetProjectResource> The .NET project resource builder.filterFunc<EndpointAnnotation, bool> A filter that returns true for endpoints whose environment variables should be injected.Returns
IResourceBuilder<DotnetProjectResource> The same builder for chaining additional configuration.Exceptions
ArgumentNullExceptionThrown when builder or filter is null.Remarks
WithReplicas(IResourceBuilder<DotnetProjectResource>, int) Section titled WithReplicas(IResourceBuilder<DotnetProjectResource>, int)extensionIResourceBuilder<DotnetProjectResource> public static class DotnetProjectHostingExtensions{ public static IResourceBuilder<DotnetProjectResource> WithReplicas( this IResourceBuilder<DotnetProjectResource> builder, int replicas) { // ... }}Parameters
builderIResourceBuilder<DotnetProjectResource> The .NET project resource builder.replicasintThe number of replicas.Returns
IResourceBuilder<DotnetProjectResource> The same builder for chaining additional configuration.Exceptions
ArgumentNullExceptionThrown when builder is null.