Skip to content

Dotnet Aspire

Dotnet Aspire


Aspire - What is it?

.NET Aspire is an opinionated, cloud ready stack for building observable, production ready, distributed applications. - Microsoft


Aspire - What is it? (2)

opinionatedAspire has its own opinions on how to do certain things.
cloud readyThe focus for Aspire is to build applications that run in the cloud.
observableMonitoring, telemetry and system overview play a vital part.
production readyEffectiveness, efficiency and reliability.
distributedAspire applications consist of more than one software part that work together.

Aspire - How?

.NET Aspire is delivered through a collection of NuGet packages that handle specific cloud-native concerns.

Cloud-native apps often consist of small, interconnected pieces or microservices rather than a single, monolithic code base.

Cloud-native apps generally consume a large number of services, such as databases, messaging, and caching.


Aspire - Distributed

distributed application is one that uses computational resources across multiple nodes, such as containers running on different hosts.

Such nodes must communicate over network boundaries to deliver responses to users.

A cloud-native app is a specific type of distributed app that takes full advantage of the scalability, resilience, and manageability of cloud infrastructures.


Aspire - Parts

  • Orchestration
    • provides features for running and connecting multi-project applications and their dependencies.
  • Components
    • Aspire components are NuGet packages for commonly used services, such as Redis or Postgres, with standardized interfaces ensuring they connect consistently and seamlessly with your app.
  • Tooling
    • Aspire comes with project templates and tooling experiences for Visual Studio/Rider and the dotnet CLI help you create and interact with .NET Aspire apps.

Aspire - Orchestration

… means the coordination and management of various elements within a cloud-native application.

  • App composition
    • Specify the .NET projects, containers, executables, and cloud resources that make up the application.
  • Service discovery and connection string management
    • The app host manages injecting the right connection strings and service discovery information to simplify the developer experience.

Aspire - Orchestration example

// Create a distributed application builder
// given the command line arguments.
var builder = DistributedApplication.CreateBuilder(args);
// Add a Redis server to the application.
var cache = builder.AddRedis("cache");
// Add the frontend project to the application
// and configure it to use the Redis server,
// defined as a referenced dependency.
builder.AddProject<Projects.MyFrontend>("frontend")
.WithReference(cache);

Aspire - Template

AspireTemplate.excalidraw.svg


Aspire - Template - AppHost

The Orchestration Part of the Aspire application and the startup project.

var builder = DistributedApplication.CreateBuilder(args);
var apiservice = builder
.AddProject<Projects.AspireIntro_ApiService>("apiservice");
builder.AddProject<Projects.AspireIntro_Web>("webfrontend")
.WithReference(apiservice);
builder.Build().Run();

Aspire - Template - ServiceDefaults

Configuration via code that can be shared in different projects. Other projects call the AddServiceDefaults extension method.


Aspire - Template - Web/ApiService

These are standard C# projects for a REST API and a web frontend. They can use Aspire for standard configuration and service discovery though.

Web/Program.cs
// Register a HTTP client for dependency injection.
// The endpoint will be discovered automatically
// by using the service name defined in the AppHost project.
builder.Services
.AddHttpClient<WeatherApiClient>(client =>
client.BaseAddress = new("http://apiservice"));

Aspire - Dashboard

Aspire ships with a dashboard primarily meant for local development and analysis. Currently there is no standard way to deploy the dashboard to production applications, although the community issued the wish.

AspireDashboard.png


To use dotnet Aspire you might need to run

Terminal window
dotnet workload install aspire