.NET Aspire Overview

.NET Aspire is an opinionated, cloud-ready stack for building observable, production-ready, distributed applications. .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

Introduction

.NET Aspire is a cloud-ready toolkit designed for building modern, distributed applications. It focuses on helping developers create scalable and observable microservices. With a collection of NuGet packages, .NET Aspire simplifies deploying .NET Core APIs, allowing you to quickly build cloud-native applications. These applications typically consist of smaller, interconnected components instead of a single, large code base. They often rely on services like databases, messaging, and caching to function effectively in a cloud environment.

Creating a New Project in Visual Studio 2022

Creating a New Project in Visual Studio 2022 Preview Start the Visual Studio software and select "Create a new project."

Create a new project

Visual Studio 2022 will generate the project structure for the selected application. In this example, we are using ASP.Net Core Web API, so we can create a controller to write the code or use the existing controller. There, you can enter the code and build/run the application.

Web API

To create a .NET Aspire Application, follow these steps.

  1. Right-click on the Solution section in Visual Studio.
  2. Select "Add" > "New Project."
  3. Choose "Create a new project."
  4. Find and select ".NET Aspire Application" from the list of project templates.

NET Aspire Application

The .NET Aspire Application is a basic starter template that includes only the AppHost and ServiceDefaults projects. This template is designed to only provide the essentials for you to build off of

Service Defaults projects

The .NET Aspire Application is a basic starter template that includes only the AppHost and ServiceDefaults projects. This template is designed to provide the essentials to build upon.

Project Structure

In the Program.cs file on the AppHost project, you can see the following code.

var builder = DistributedApplication.CreateBuilder(args);
var RESTAPI = builder.AddProject<Projects.WEB>("RESTAPI");
builder.Build().Run();

Program file

WEB .NET core API program.cs file configured in Aspire.ServiceDefaults.

builder.AddServiceDefaults();
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSwaggerGen();

//cloud configuration:

builder.AddServiceDefaults();

var app = builder.Build();

// Configure the HTTP request pipeline.

if (app.Environment.IsDevelopment())

{
    app.UseSwagger();

    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Extensions

On the Extensions. cs file, you can find some extension methods such as:

Dashboard

Start the project with AspireCloud.AppHost is the Starter project (this project knows how to run the whole distributed application), and the following page will be opened in your browser with this nice dashboard.

Dashboard

In this dashboard, you can monitor various parts of your app, such as,

The Traces for your APIs.

Traces

.NET Aspire Cloud Application Source Link:https://github.com/jobin4jo/Dotnetcore-Aspire

Conclusion

.NET Aspire is a stack that provides a range of important features such as resilience, service discovery, telemetry, health checks, facilitating the development of cloud-native apps easier and also integrates a dashboard where you can monitor your distributed applications.