DistributedApplication Methods
Hosting.IHost and IAsyncDisposable interfaces. IDistributedApplicationBuilder interface. public class DistributedApplication{ public static IDistributedApplicationBuilder CreateBuilder() { // ... }}Returns
IDistributedApplicationBuilder A new instance of the IDistributedApplicationBuilder interface.Remarks
DistributedApplication.CreateBuilder method should only be used when the AppHost is not intended to be used with a deployment tool. Because no arguments are passed to the DistributedApplication.CreateBuilder method the AppHost has no way to be put into publish mode. Refer to DistributedApplication.CreateBuilder or DistributedApplication.CreateBuilder when more control is needed over the behavior of the distributed application at runtime. The following example is creating a Postgres server resource with a database and referencing that database in a .NET project. var builder = DistributedApplication.CreateBuilder();var inventoryDatabase = builder.AddPostgres( "mypostgres").AddDatabase("inventory");builder.AddProject<Projects.InventoryService>() .WithReference(inventoryDatabase);
builder.Build().Run();IDistributedApplicationBuilder with the specified command-line arguments. public class DistributedApplication{ public static IDistributedApplicationBuilder CreateBuilder( string[] args) { // ... }}Parameters
argsstring[]The command-line arguments to use when building the distributed application.Returns
IDistributedApplicationBuilder A new instance of IDistributedApplicationBuilder .Remarks
The Distributed method is the most common way to create an instance of the I interface. Typically this method will be called as a top-level statement in the application's entry-point.
Note that the args parameter is a string and is essential in allowing the application host to work with deployment tools because arguments are used to tell the application host that it is in publish mode. If args is not provided the application will not work with deployment tools. It is also possible to provide arguments using the Distributed overload of this method.
var builder = DistributedApplication.CreateBuilder(args);var inventoryDatabase = builder.AddPostgres( "mypostgres").AddDatabase("inventory");builder.AddProject<Projects.InventoryService>() .WithReference(inventoryDatabase);
builder.Build().Run();public class Program{ public static void Main(string[] args) { var builder = DistributedApplication.CreateBuilder(args); var inventoryDatabase = builder.AddPostgres( "mypostgres").AddDatabase("inventory"); builder.AddProject<Projects.InventoryService>() .WithReference(inventoryDatabase);
builder.Build().Run(); }}CreateBuilder(DistributedApplicationOptions) Section titled CreateBuilder(DistributedApplicationOptions)staticIIDistributedApplicationBuilder interface with the specified options. public class DistributedApplication{ public static IDistributedApplicationBuilder CreateBuilder( DistributedApplicationOptions options) { // ... }}Parameters
optionsDistributedDistributedApplicationOptions to use for configuring the builder.Returns
IDistributedApplicationBuilder A new instance of the IDistributedApplicationBuilder interface.Remarks
The Distributed method provides greater control over the behavior of the distributed application at runtime. For example providing an options argument allows developers to force all container images to be loaded from a specified container registry by using the Distributed property, or disabling the dashboard by using the Distributed property. Refer to the Distributed class for more details on each option that may be provided.
When supplying a custom Distributed it is recommended to populate the Distributed property to ensure that the AppHost continues to function correctly when used with deployment tools that need to enable publish mode.
var options = new DistributedApplicationOptions{ Args = args; // Important for deployment tools ContainerRegistryOverride = "registry.example.com"};var builder = DistributedApplication.CreateBuilder(options);var inventoryDatabase = builder.AddPostgres( "mypostgres").AddDatabase("inventory");builder.AddProject<Projects.InventoryService>() .WithReference(inventoryDatabase);
builder.Build().Run();Hosting.IHost . public class DistributedApplication{ public virtual void Dispose() { // ... }}Remarks
Typically developers do not need to worry about calling the Dispose method on the Distributed instance because it is typically used in the entry point of the application and all resources used by the application are destroyed when the application exists.
If you are using the Distributed and I inside unit test code then you should correctly dispose of the Distributed instance. This is because the I instance initializes configuration providers which make use of file watchers which are a finite resource.
Without disposing of the Distributed correctly projects with a large number of functional/integration tests may see a "The configured user limit (128) on the number of inotify instances has been reached, or the per-process limit on the number of open file descriptors has been reached." error.
Refer to the Aspire testing page for more information on how to use Aspire APIs for functional an integrating testing.
Hosting.IHost . public class DistributedApplication{ public virtual ValueTask DisposeAsync() { // ... }}Returns
ValueTask A Tasks.ValueTask representing the asynchronous operation.Remarks
Typically developers do not need to worry about calling the Dispose method on the Distributed instance because it is typically used in the entry point of the application and all resources used by the application are destroyed when the application exists.
If you are using the Distributed and I inside unit test code then you should correctly dispose of the Distributed instance. This is because the I instance initializes configuration providers which make use of file watchers which are a finite resource.
Without disposing of the Distributed correctly projects with a large number of functional/integration tests may see a "The configured user limit (128) on the number of inotify instances has been reached, or the per-process limit on the number of open file descriptors has been reached." error.
Refer to the Aspire testing page for more information on how to use Aspire APIs for functional an integrating testing.
Hosting.IHostedService instances are stopped. public class DistributedApplication{ public void Run() { // ... }}Remarks
When the Aspire AppHost is launched via Distributed there are two possible modes that it is running in:
- Run mode; in run mode the AppHost runs until a shutdown of the app is triggered either by the users pressing
Ctrl-, the debugger detaching, or the browser associated with the dashboard being closed.C - Publish mode; in publish mode the AppHost runs just long enough to generate a manifest file that is used by deployment tool.
Developers extending the Aspire application model should consider the lifetime of Hosting. instances which are added to the dependency injection container. For more information on determining the mode that the AppHost is running in refer to Distributed.
Hosting.IHostedService instances are stopped. public class DistributedApplication{ public virtual Task RunAsync( CancellationToken cancellationToken = default(CancellationToken)) { // ... }}Parameters
cancellationToken CancellationToken optionalThe token to trigger shutdown.Returns
TaskThe Tasks.Task that represents the asynchronous operation.Remarks
When the Aspire AppHost is launched via Distributed there are two possible modes that it is running in:
- Run mode; in run mode the AppHost runs until a shutdown of the app is triggered either by the users pressing
Ctrl-, the debugger detaching, or the browser associated with the dashboard being closed.C - Publish mode; in publish mode the AppHost runs just long enough to generate a manifest file that is used by deployment tool.
Developers extending the Aspire application model should consider the lifetime of Hosting. instances which are added to the dependency injection container. For more information on determining the mode that the AppHost is running in refer to Distributed.
Hosting.IHostedService objects configured for the program. The application will run until interrupted or until IHostApplicationLifetime.StopApplication is called. public class DistributedApplication{ public virtual Task StartAsync( CancellationToken cancellationToken = default(CancellationToken)) { // ... }}Parameters
cancellationToken CancellationToken optionalUsed to abort program start.Returns
TaskA Tasks.Task that will be completed when the Hosting.IHost starts.public class DistributedApplication{ public virtual Task StopAsync( CancellationToken cancellationToken = default(CancellationToken)) { // ... }}Parameters
cancellationToken CancellationToken optionalUsed to indicate when stop should no longer be graceful.Returns
TaskA Tasks.Task that will be completed when the Hosting.IHost stops.