Introduction To .NET Aspire: A Beginner’s Guide to Getting Started

sharmila subbiah
6 min readDec 4, 2023

--

Introduction to .NET Aspire:

.NET Aspire represents a cloud-optimized framework, specifically tailored for the development of observable, production-grade distributed applications. It is uniquely structured as an opinionated stack, ensuring cloud readiness and streamlined deployment. This framework is encapsulated within a suite of NuGet packages, each addressing distinct aspects of cloud-native application needs. Emphasizing a microservices architecture, .NET Aspire facilitates the creation of applications composed of multiple small, interconnected components, rather than relying on a single, large codebase. This approach inherently supports extensive service integration, including databases, messaging systems, and caching services, essential for modern cloud-native applications

Prerequisites

To work with .NET Aspire, you’ll need the following installed locally:

Create a default Visual Studio .net core API

  1. Open Visual Studio 2022: Start Visual Studio and choose “Create a new project.”
  2. Select Project Type: In the project creation wizard, select “ASP.NET Core Web API” and click “Create.”

Create a Basic Blazer app

  1. Open Visual Studio 2022: Start Visual Studio and select “Create a new project.”
  2. Choose Project Type: In the project creation wizard, select “Blazor Web App” and click “Create.”

Connect Api to our Blazer App

Create a new class file named AspireApiClient.cs in your Blazor app under the namespace AspireApp.Components. This class will be responsible for making HTTP requests to your API.

namespace AspireApp.Components
{
using System.Net.Http;
using AspireAPI;

public class AspireApiClinet
{
private readonly HttpClient _httpClient;

public AspireApiClinet(HttpClient httpClient)
{
this._httpClient = httpClient;
}

public async Task<WeatherForecast[]> GetWeatherForecastsAsync()
{
return await _httpClient.GetFromJsonAsync<WeatherForecast[]>("weatherforecast");
}
}
}

Update Weather.razor

Modify the Weather.razor component to use the AspireApiClient for fetching weather data. Ensure you have proper dependency injection and error handling.

@inject AspireApiClinet AspireApiClinet

private AspireAPI.WeatherForecast[]? forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await AspireApiClinet.GetWeatherForecastsAsync();
}

Configure HttpClient in program.cs

In your program.cs, configure the HttpClient for the AspireApiClient. This includes setting the base address for your API requests.

builder.Services.AddHttpClient<AspireApiClinet>(client =>
{
client.BaseAddress = new Uri("http://localhost:5208/weatherforecast");
});

Run the Blazor App

Now that all the components are in place, you can run your Blazor app. Your app should now be able to communicate with the API and display the fetched data.

Create a .Net Aspire Application

  1. Open Visual Studio 2022: Start Visual Studio and select “Create a new project.”
  2. In the dialog window, search for Aspire and select .NET Aspire Application. Select Next.

3. Make sure .NET 8.0 (Long Term Support) is selected.

4. Ensure that Use Redis for caching (requires Docker) is checked and select Create.

Your .NET Aspire application will include two projects:

AspireSample.AppHost: An orchestrator project designed to connect and configure the different projects and services of your app.

AspireSample.ServiceDefaults: 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 is how your project directory should look like

Orchestrating API and Blazor Projects

Orchestrate projects using Visual Studio Tooling by right-clicking the project and selecting “.NET Aspire Orchestrator Support”.

The action of adding .NET Aspire Orchestrator Support automatically makes necessary modifications to the program.cs file of the AppHost project.These modifications include the registration of your project and setting up defaults for the orchestrated projects.

var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedisContainer("rediscache");

var api = builder.AddProject<Projects.AspireAPI>("aspireapi");

builder.AddProject<Projects.AspireApp>("aspireapp")
.WithReference(api)
.WithReference(cache);


builder.Build().Run();

NOTE: These can also be done manually .

Monitoring with .NET Aspire Dashboard

Running the AppHost project launches the .NET Aspire dashboard, which monitors various aspects of your application. The dashboard provides views for Projects, Containers, Executables, Logs, Traces, and Metrics, offering insights into the performance and health of your application.

.NET Aspire Dashboard

Dashboard has view different information about the .NET Aspire app:

  • Projects: Lists basic information for all of the individual .NET projects in your .NET Aspire app, such as the app state, endpoint addresses, and the environment variables that were loaded in.
Project
  • Containers: Lists basic information about your app containers, such as the state, image tag, and port number. You should see the Redis container you added for output caching with the name you provided.
Containers
  • Executables: Lists the running executables used by your app. The sample app doesn’t include any executables, so it should display the message No running executables found.

Logs:

  • Project: Displays the output logs for the projects in your app. Select which project you’d like to display logs for using the drop-down at the top of the page.
Project Logs
  • Container: Displays logs from the containers in your app. You should see Redis logs from the container you configured as part of the template. If you have more than one container, you can select which to show logs from using the drop-down at the top of the page.
Container Logs
  • Executable: Displays logs from the executables in your app. The sample app doesn’t include any executables, so there’s nothing to see here.
  • Structured: Displays structured logs in table format. These logs support basic filtering, free-form search, and log level filtering as well. You should see logs from the apiservice and the webfrontend. You can expand the details of each log entry by selecting the View button on the right end of the row.
Structured Logs

. Traces: Displays the traces for your application, which can track request paths through your apps. Locate a request for /weather and select View on the right side of the page. The dashboard should display the request in stages as it travels through the different parts of your app.

Traces
  • Metrics: Displays various instruments and meters that are exposed and their corresponding dimensions for your app. Metrics conditionally expose filters based on their available dimensions.
Metrics

For further insights and a comprehensive understanding, refer to Explore the .NET Aspire dashboard, and What Is .NET Aspire? The Insane Future of .NET! These resources will provide additional information and practical examples to enhance your learning about .Net Aspire

--

--

sharmila subbiah

With over a decade of experience in the tech industry, I currently hold the position of Senior Software Engineer at Youlend.