.NET Aspire - Cloud Native Apps Framework by Microsoft

Introduction

In this article, we are going to understand the Aspire framework offered by Microsoft. This newbie in town has got a lot to offer and makes our cloud app development effortless. It is an abstraction around the most common things we do when developing cloud applications and many other things in general. If you are thinking what are the most common things that we do, below is the list.

  1. Communication between multiple APIs
  2. Connection to Caching Services and/or Databases.
  3. Structuring projects such that they can be easily maintained and scaled.
  4. Less coding by reusing code and focusing more on business logic.
  5. Dashboarding to monitor our all API services and everything related and stay updated. 

It is designed to solve problems around these 3 principles.

  1. Orchestration: Communication and coordination between various elements of cloud application. Eg. Environment variables, Configurations, communication between multiple projects, etc. 
  2. Components: It is a collection of Nuget packages that makes connection with popular services effortless. Eg: Redis, Postgres, SQL Server, Messaging Queues, etc. More details - 
  3. Tooling: Structure of projects organized in a way that is extensible, maintainable, and standardized. 

Since this is an introductory article, you can dig deeper by following the link below - https://learn.microsoft.com/en-us/dotnet/aspire/get-started/aspire-overview

Let us learn more by taking a walkthrough and how to use it.

Prerequisite

Licensed/Community

  • Visual Studio 17.9 or above

Open Source

  • Visual Studio Code
  • .NET 8 SDK
  • .NET CLI

I will be showing this demo using the Open Source toolchain.

Walkthrough

The way to consume Aspire is by creating a .NET Project if you start from scratch. There are 2 templates offered for aspire projects.

Template Name Short Name Language Tags What it contains
.NET Aspire Application aspire [C#]

Common/.NET

Aspire/Cloud

Creates a project that is empty
.NET Aspire Starter Application aspire-starter [C#]

Common/.NET

Aspire/Blazor/Web/Web

API/API/Service/Cloud

Creates sample project with sample 

UI and API included

Since this is an introductory tutorial, let us create a Starter application i.e. 2nd option from the above table.

Open Source toolchain via dotnet CLI and Visual Studio Code.

Type the below commands using cmd.

dotnet workload update

Stack Trace

No workloads are installed for this feature band. To update workloads installed with earlier SDK versions, include the --from-previous-sdk option.

Updated advertising manifest microsoft.net.sdk.tvos.
Updated advertising manifest microsoft.net.workload.emscripten.current.
Updated advertising manifest microsoft.net.workload.mono.toolchain.net6.
Updated advertising manifest microsoft.net.workload.emscripten.net7.
Updated advertising manifest microsoft.net.workload.mono.toolchain.net7.
Updated advertising manifest microsoft.net.sdk.aspire.
Updated advertising manifest microsoft.net.sdk.maccatalyst.
Updated advertising manifest microsoft.net.workload.mono.toolchain.current.
Updated advertising manifest microsoft.net.workload.emscripten.net6.
Updated advertising manifest microsoft.net.sdk.ios.
Updated advertising manifest microsoft.net.sdk.macos.
Updated advertising manifest microsoft.net.sdk.android.
Updated advertising manifest microsoft.net.sdk.maui.
Downloading microsoft.net.sdk.aspire.manifest-8.0.100.msi.x64 (8.0.0-preview.1.23557.2)
Installing microsoft.net.sdk.aspire.manifest-8.0.100.msi.x64 .... Done

No workloads are installed for this feature band. To update workloads installed with earlier SDK versions, include the --from-previous-sdk option.

Successfully updated workload(s):

dotnet workload install aspire

Common Error: Workload ID aspire is not recognized.

Solution: Update SDK to .NET 8, Link - https://dotnet.microsoft.com/en-us/download/dotnet/8.0

Stack Trace:

Downloading Aspire.Hosting.Sdk.Msi.x64 (8.0.0-preview.1.23557.2)
Installing Aspire.Hosting.Sdk.Msi.x64 .... Done
Downloading Aspire.ProjectTemplates.Msi.x64 (8.0.0-preview.1.23557.2)
Installing Aspire.ProjectTemplates.Msi.x64 .... Done
Downloading Aspire.Hosting.Orchestration.win-x64.Msi.x64 (8.0.0-preview.1.23557.2)
Installing Aspire.Hosting.Orchestration.win-x64.Msi.x64 ........ Done
Downloading Aspire.Hosting.Msi.x64 (8.0.0-preview.1.23557.2)
Installing Aspire.Hosting.Msi.x64 .... Done
Successfully installed workload(s) aspire.
dotnet new aspire-starter

Stack Trace:

The template ".NET Aspire Starter Application" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/aspire/1.0-third-party-notices for details.
Processing post-creation actions...
Restoring D:\Demo\Demo.sln:
  Determining projects to restore...
  Restored D:\Demo\Demo.ServiceDefaults\Demo.ServiceDefaults.csproj (in 22.11 sec).
  Restored D:\Demo\Demo.ApiService\Demo.ApiService.csproj (in 22.11 sec).
  Restored D:\Demo\Demo.Web\Demo.Web.csproj (in 22.11 sec).
  Restored D:\Demo\Demo.AppHost\Demo.AppHost.csproj (in 24.53 sec).
Restore succeeded.
Restoring D:\Demo\Demo.AppHost\Demo.AppHost.csproj:
  Determining projects to restore...
  All projects are up-to-date for restore.
Restore succeeded.
Restoring D:\Demo\Demo.ServiceDefaults\Demo.ServiceDefaults.csproj:
  Determining projects to restore...
  All projects are up-to-date for restore.
Restore succeeded.
Restoring D:\Demo\Demo.ApiService\Demo.ApiService.csproj:
  Determining projects to restore...
  All projects are up-to-date for restore.
Restore succeeded.
Restoring D:\Demo\Demo.Web\Demo.Web.csproj:
  Determining projects to restore...
  All projects are up-to-date for restore.
Restore succeeded.
code .

Select App Host project and type

dotnet run

Stack Trace:

Building...
info: Aspire.Dashboard.DashboardWebApplication[0]
      Now listening on: http://localhost:15009
info: Aspire.Dashboard.DashboardWebApplication[0]
      OTLP server running at: http://localhost:16101
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\Demo\Demo.AppHost

Overview of the home screen

  • Projects:
    • This section lists the various projects you are managing.
  • Project Details:
    • Each project has the following details:
      • Name: The name of the project.
      • State: The current state of the project (e.g., "Running").
      • Start Time: The timestamp indicating when the project started (e.g., "19-12-2023 21:30:37").
      • Process Id: An identifier for the process associated with the project (e.g., "18768").
      • Source Location: The file path or directory where the project source code is located (e.g., "D:\Demo\Demo.ApiService\Demo.ApiService.csproj").
  • Endpoints:
    • For each project, there are endpoints associated with it.
      • Environment: The environment in which the project is running.
      • Logs: Information related to project logs.
  • Individual Project Information:
    • There are two projects mentioned with their respective details:
      • Project 1 ("apiservice"):
        • State: Running
        • Start Time: 19-12-2023 21:30:37
        • Process Id: 18768
        • Source Location: D:\Demo\Demo.ApiService\Demo.ApiService.csproj
        • Endpoints:
          • Environment: (Not specified)
          • Logs: (Not specified)
        • API Service Endpoint: http://localhost:5371/weatherforecast
        • Actions: View
      • Project 2 ("webfrontend"):
        • State: Running
        • Start Time: 19-12-2023 21:30:37
        • Process Id: 19656
        • Source Location: D:\Demo\Demo.Web\Demo.Web.csproj
        • Endpoints:
          • Environment: (Not specified)
          • Logs: (Not specified)
        • Web Frontend Endpoint: http://localhost:5202
        • Actions: View

This dashboard provides an overview of the current state, start time, process ID, source location, and endpoints for each project. The "View" action at the end of each project entry likely allows users to access more detailed information or interact with the project in some way.

Navigation overview

  • Projects: Manages software projects, their states, start times, process IDs, source locations, and endpoints in different environments.
  • Containers: Handles information related to containerized applications within the system.
  • Executables: Manages information about executable files and their states within the system.
  • Logs: Expands to provide access to logs for Projects, Containers, and Executables.
  • Structured: Provides access to structured logs within the system.
  • Traces: Manages tracing information for various components in the system.

That’s it! Thanks for reading till the end. Please add in comment about how you feel about this article.

Also, please feel free to add your suggestion if you want to know more about this framework so I can more post articles for you. 


Recommended Free Ebook
Similar Articles