Deploying .NET 8 Core Web API in Cloud with .NET Aspire App"

.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

  • WEB: This is an ASP.NET Core API project, This project depends on the shared AspireCloud.ServiceDefaults project.
  • AspireCloud.AppHost: This is an orchestrator project designed to connect and configure the different projects and services of your app. This project handles running all of the projects that are part of the .NET Aspire application. The orchestrator should be set as the Startup project, and it depends on the WEBThis is the project responsible for running the applications inside of this solution.
  • AspireCloud.ServiceDefaults: This is a.NET Aspire shared project to manage configurations that are reused across the projects in your solution related to resilience, service discovery, and telemetry. This project ensures that all dependent services share the same resilience, service discovery, and OpenTelemetry configuration.

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:

  • AddServiceDefaults: used to add default functionality.
  • ConfigureOpenTelemetry: used to configure OpenTelemetry metrics and tracing
  • AddDefaultHealthChecks: adds default health checks endpoints
  • MapDefaultEndpoints: maps the health check endpoint to /health and the liveness endpoint to alive.

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,

  • Projects: Displays information about your .NET projects in your .NET Aspire app, such as the app state, endpoints, and environment variables.
  • Containers: Displays information about your app containers, such as state, image tag, and port number (you should also see the Redis container you added for output caching with the name you provided.
  • Executables: Displays the running executables used by your app.
  • Logs: In this section, you can see the output logs for the projects, containers, and executables, and can also see the logs in a table format (structured).
  • Traces: displays the traces for your application, providing information about the requests.
  • Metrics: Displays various instruments and meters that are exposed and their corresponding dimensions for your app.

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.


Similar Articles