ProjectResourceBuilderExtensions.WithReplicas Method

Definition

Configures how many replicas of the project should be created for the project.

C#
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ProjectResource> WithReplicas(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ProjectResource> builder, int replicas);

Parameters

builder
IResourceBuilder<ProjectResource>

The project resource builder.

replicas
Int32

The number of replicas.

Returns

A reference to the IResourceBuilder<T>.

Examples

Start multiple instances of the same service.

C#
var builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<Projects.InventoryService>("inventoryservice")
       .WithReplicas(3);

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 WithReplicas(IResourceBuilder<ProjectResource>, Int32) 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.

Applies to

Product Versions
.NET Aspire 8.0.0, 9.0.0, 9.1.0