Partager via


DistributedApplication Class

Definition

Represents a distributed application that implements the IHost and IAsyncDisposable interfaces.

public class DistributedApplication : IAsyncDisposable, IDisposable, Microsoft.Extensions.Hosting.IHost
type DistributedApplication = class
    interface IHost
    interface IDisposable
    interface IAsyncDisposable
Public Class DistributedApplication
Implements IAsyncDisposable, IDisposable, IHost
Inheritance
DistributedApplication
Implements

Examples

The following example shows creating a PostgreSQL server resource with a database and referencing that database in a .NET project.

var builder = DistributedApplication.CreateBuilder(args);
var inventoryDatabase = builder.AddPostgres("mypostgres").AddDatabase("inventory");
builder.AddProject<Projects.InventoryService>()
       .WithReference(inventoryDatabase);

builder.Build().Run();

Remarks

The DistributedApplication is an implementation of the IHost interface that orchestrates a .NET Aspire application. To build an instance of the DistributedApplication class, use the CreateBuilder() method to create an instance of the IDistributedApplicationBuilder interface. Using the IDistributedApplicationBuilder interface you can configure the resources that comprise the distributed application and describe the dependencies between them.

Once the distributed application has been defined use the Build() method to create an instance of the DistributedApplication class. The DistributedApplication class exposes a Run() method which then starts the distributed application and its resources.

The CreateBuilder(DistributedApplicationOptions) method provides additional options for constructing the IDistributedApplicationBuilder including disabling the .NET Aspire dashboard (see DisableDashboard) or allowing unsecured communication between the browser and dashboard, and dashboard and app host (see AllowUnsecuredTransport.

Constructors

DistributedApplication(IHost)

Initializes a new instance of the DistributedApplication class.

Properties

Services

Gets the IServiceProvider instance configured for the application.

Methods

CreateBuilder()

Creates a new instance of the IDistributedApplicationBuilder interface.

CreateBuilder(DistributedApplicationOptions)

Creates a new instance of the IDistributedApplicationBuilder interface with the specified options.

CreateBuilder(String[])

Creates a new instance of IDistributedApplicationBuilder with the specified command-line arguments.

Dispose()

Disposes the distributed application by disposing the IHost.

DisposeAsync()

Asynchronously disposes the distributed application by disposing the IHost.

Run()

Runs an application and blocks the calling thread until host shutdown is triggered and all IHostedService instances are stopped.

RunAsync(CancellationToken)

Runs an application and returns a Task that only completes when the token is triggered or shutdown is triggered and all IHostedService instances are stopped.

StartAsync(CancellationToken)

Starts the IHostedService objects configured for the program. The application will run until interrupted or until StopApplication() is called.

StopAsync(CancellationToken)

Attempts to gracefully stop the program.

Explicit Interface Implementations

IHost.StartAsync(CancellationToken)
IHost.StopAsync(CancellationToken)

Extension Methods

CreateHttpClient(DistributedApplication, String, String)

Creates an HttpClient configured to communicate with the specified resource.

GetConnectionStringAsync(DistributedApplication, String, CancellationToken)

Gets the connection string for the specified resource.

GetEndpoint(DistributedApplication, String, String)

Gets the endpoint for the specified resource.

Applies to