ProjectResourceBuilderExtensions Methods
IDistributedApplicationBuilder to add and configure project resources. AddCSharpApp(IDistributedApplicationBuilder, string, string) Section titled AddCSharpApp(IDistributedApplicationBuilder, string, string) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> AddCSharpApp( this IDistributedApplicationBuilder builder, string name, string path) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. path string The path to the file-based app file, project file, or project directory. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddCSharpApp method adds a C# project or file-based app to the application model using a path to the file-based app .cs file, project file (.csproj), or project directory. If the path is not an absolute path then it will be computed relative to the app host directory.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddCSharpApp("inventoryservice", @"..\InventoryService.cs");
builder.Build().Run();AddCSharpApp(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>) Section titled AddCSharpApp(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>) extension IResourceBuilder<CSharpAppResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<CSharpAppResource> AddCSharpApp( this IDistributedApplicationBuilder builder, string name, string path, Action<ProjectResourceOptions> configure) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. path string The path to the file-based app file, project file, or project directory. configure Action<ProjectResourceOptions> An optional action to configure the C# app resource options. Returns
IResourceBuilder<CSharpAppResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddCSharpApp method adds a C# project or file-based app to the application model using a path to the file-based app .cs file, project file (.csproj), or project directory. If the path is not an absolute path then it will be computed relative to the app host directory.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddCSharpApp( "inventoryservice", @"..\InventoryService.cs", o => o.LaunchProfileName = "https");
builder.Build().Run();AddProject(IDistributedApplicationBuilder, string) Section titled AddProject(IDistributedApplicationBuilder, string) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> AddProject<TProject>( this IDistributedApplicationBuilder builder, string name) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddProject method takes a TProject type parameter. The TProject type parameter is constrained to types that implement the IProjectMetadata interface.
Classes that implement the IProjectMetadata interface are generated when a .NET project is added as a reference to the app host project. The generated class contains a property that returns the path to the referenced project file. Using this path .NET Aspire parses the launchSettings.json file to determine which launch profile to use when running the project, and what endpoint configuration to automatically generate.
The name of the automatically generated project metadata type is a normalized version of the project name. Periods, dashes, and spaces in project names are converted to underscores. This normalization may lead to naming conflicts. If a conflict occurs the <ProjectReference /> that references the project can have a AspireProjectMetadataTypeName="..." attribute added to override the name.
Note that endpoints coming from the Kestrel configuration are automatically added to the project. The Kestrel Url and Protocols are used to build the equivalent EndpointAnnotation.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.InventoryService>("inventoryservice");
builder.Build().Run();AddProject(IDistributedApplicationBuilder, string, string) Section titled AddProject(IDistributedApplicationBuilder, string, string) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> AddProject( this IDistributedApplicationBuilder builder, string name, string projectPath) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. projectPath string The path to the project file. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddProject method adds a project to the application model using a path to the project file. This allows for projects to be referenced that may not be part of the same solution. If the project path is not an absolute path then it will be computed relative to the app host directory.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject( "inventoryservice", @"..\InventoryService\InventoryService.csproj");
builder.Build().Run();AddProject(IDistributedApplicationBuilder, string, string?) Section titled AddProject(IDistributedApplicationBuilder, string, string?) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> AddProject<TProject>( this IDistributedApplicationBuilder builder, string name, string? launchProfileName) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. launchProfileName string? The launch profile to use. If null then no launch profile will be used. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddProject method takes a TProject type parameter. The TProject type parameter is constrained to types that implement the IProjectMetadata interface.
Classes that implement the IProjectMetadata interface are generated when a .NET project is added as a reference to the app host project. The generated class contains a property that returns the path to the referenced project file. Using this path .NET Aspire parses the launchSettings.json file to determine which launch profile to use when running the project, and what endpoint configuration to automatically generate.
The name of the automatically generated project metadata type is a normalized version of the project name. Periods, dashes, and spaces in project names are converted to underscores. This normalization may lead to naming conflicts. If a conflict occurs the <ProjectReference /> that references the project can have a AspireProjectMetadataTypeName="..." attribute added to override the name.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.InventoryService>( "inventoryservice", launchProfileName: "otherLaunchProfile");
builder.Build().Run();AddProject(IDistributedApplicationBuilder, string, string, string?) Section titled AddProject(IDistributedApplicationBuilder, string, string, string?) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> AddProject( this IDistributedApplicationBuilder builder, string name, string projectPath, string? launchProfileName) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. projectPath string The path to the project file. launchProfileName string? The launch profile to use. If null then no launch profile will be used. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddProject method adds a project to the application model using a path to the project file. This allows for projects to be referenced that may not be part of the same solution. If the project path is not an absolute path then it will be computed relative to the app host directory.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject( "inventoryservice", @"..\InventoryService\InventoryService.csproj", launchProfileName: "otherLaunchProfile");
builder.Build().Run();AddProject(IDistributedApplicationBuilder, string, Action<ProjectResourceOptions>) Section titled AddProject(IDistributedApplicationBuilder, string, Action<ProjectResourceOptions>) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> AddProject<TProject>( this IDistributedApplicationBuilder builder, string name, Action<ProjectResourceOptions> configure) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. configure Action<ProjectResourceOptions> A callback to configure the project resource options. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddProject method takes a TProject type parameter. The TProject type parameter is constrained to types that implement the IProjectMetadata interface.
Classes that implement the IProjectMetadata interface are generated when a .NET project is added as a reference to the app host project. The generated class contains a property that returns the path to the referenced project file. Using this path .NET Aspire parses the launchSettings.json file to determine which launch profile to use when running the project, and what endpoint configuration to automatically generate.
The name of the automatically generated project metadata type is a normalized version of the project name. Periods, dashes, and spaces in project names are converted to underscores. This normalization may lead to naming conflicts. If a conflict occurs the <ProjectReference /> that references the project can have a AspireProjectMetadataTypeName="..." attribute added to override the name.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.InventoryService>( "inventoryservice", options => { options.LaunchProfileName = "otherLaunchProfile"; });
builder.Build().Run();AddProject(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>) Section titled AddProject(IDistributedApplicationBuilder, string, string, Action<ProjectResourceOptions>) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> AddProject( this IDistributedApplicationBuilder builder, string name, string projectPath, Action<ProjectResourceOptions> configure) { // ... }}Parameters
name string The name of the resource. This name will be used for service discovery when referenced in a dependency. projectPath string The path to the project file or directory. configure Action<ProjectResourceOptions> A callback to configure the project resource options. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
This overload of the ProjectResourceBuilderExtensions.AddProject method adds a project to the application model using a path to the project file or directory. This allows for projects to be referenced that may not be part of the same solution. If the project path is not an absolute path then it will be computed relative to the app host directory.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject( "inventoryservice", @"..\InventoryService\InventoryService.csproj", options => { options.LaunchProfileName = "otherLaunchProfile"; });
builder.Build().Run();DisableForwardedHeaders(IResourceBuilder<ProjectResource>) Section titled DisableForwardedHeaders(IResourceBuilder<ProjectResource>) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> DisableForwardedHeaders( this IResourceBuilder<ProjectResource> builder) { // ... }}Parameters
builder IResourceBuilder<ProjectResource> The project resource builder. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
By default .NET Aspire assumes that .NET applications which expose endpoints should be configured to use forwarded headers. This is because most typical cloud native deployment scenarios involve a reverse proxy which translates an external endpoint hostname to an internal address.
To enable forwarded headers the ASPNETCORE_FORWARDEDHEADERS_ENABLED variable is injected into the project and set to true. If the ProjectResourceBuilderExtensions.DisableForwardedHeaders extension is used this environment variable will not be set.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.InventoryService>("inventoryservice") .DisableForwardedHeaders();PublishAsDockerFile(IResourceBuilder<T>, Action<IResourceBuilder<ContainerResource>>) Section titled PublishAsDockerFile(IResourceBuilder<T>, Action<IResourceBuilder<ContainerResource>>) extension IResourceBuilder<T> ProjectResource during deployment. The resulting container image is built, and when the optional configure action is provided, it is used to configure the container resource. public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<T> PublishAsDockerFile<T>( this IResourceBuilder<T> builder, Action<IResourceBuilder<ContainerResource>>? configure = null) { // ... }}Parameters
builder IResourceBuilder<T> Resource builder configure Action<IResourceBuilder<ContainerResource>> optional Optional action to configure the container resource Returns
IResourceBuilder<T> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
configure action. WithEndpointsInEnvironment(IResourceBuilder<ProjectResource>, Func<EndpointAnnotation, bool>) Section titled WithEndpointsInEnvironment(IResourceBuilder<ProjectResource>, Func<EndpointAnnotation, bool>) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> WithEndpointsInEnvironment( this IResourceBuilder<ProjectResource> builder, Func<EndpointAnnotation, bool> filter) { // ... }}Parameters
builder IResourceBuilder<ProjectResource> The project resource builder. filter Func<EndpointAnnotation, bool> The filter callback that returns true if and only if the endpoint should be included. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. WithReplicas(IResourceBuilder<ProjectResource>, int) Section titled WithReplicas(IResourceBuilder<ProjectResource>, int) extension IResourceBuilder<ProjectResource> public static class ProjectResourceBuilderExtensions{ public static IResourceBuilder<ProjectResource> WithReplicas( this IResourceBuilder<ProjectResource> builder, int replicas) { // ... }}Parameters
builder IResourceBuilder<ProjectResource> The project resource builder. replicas int The number of replicas. Returns
IResourceBuilder<ProjectResource> A reference to the ApplicationModel.IResourceBuilder`1. Remarks
When this method is applied to a project resource it will configure the app host to start multiple instances of the application based on the specified number of replicas. By default the app host automatically starts a reverse proxy for each process. When ProjectResourceBuilderExtensions.WithReplicas is used the reverse proxy will load balance traffic between the replicas.
This capability can be useful when debugging scale out scenarios to ensure state is appropriately managed within a cluster of instances.
Start multiple instances of the same service.var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.InventoryService>("inventoryservice") .WithReplicas(3);