
Running, composing, and debugging distributed applications on the local developer machine can be challenging, often filled with errors and consuming a lot of time. However, .NET Aspire could be the solution to simplify these everyday tasks.
In this article, we delve into .NET Aspire and showcase how it can be utilized to manage modern distributed (specifically cloud-native) applications that involve containers, WebAssembly workloads, and their necessary dependencies.
.NET Aspire provides a structured framework for the orchestration of distributed applications made up of varied interconnected components and their dependencies. In the context of .NET Aspire, orchestration involves the administration, linkage, and configuration of all parts of a distributed (cloud-native) application to improve the local development environment and its processes (inner-loop).
It is crucial to understand that the orchestration capabilities of .NET Aspire do not aim to supplant the comprehensive systems deployed in production environments, like Kubernetes. .NET Aspire overview
.NET Aspire enables developers to define their distributed application architecture directly using C#. It provides orchestration that addresses several common aspects:
Integration with well-known services like PostgreSQL and Redis is simplified through the use of .NET Aspire components. These components are available as NuGet packages and are designed to integrate cleanly with .NET Aspire’s orchestration features, facilitating cloud-native functionalities such as health monitoring and telemetry contribution.
Furthermore, .NET Aspire offers compatibility with various development environments including Visual Studio, Visual Studio Code, and the dotnet
CLI.
Microsoft announced the General Availability of .NET Aspire during Microsoft Build 2024.
As part of this article, we will explore .NET Aspire using the dotnet
CLI. To get started, ensure you have the latest dotnet
CLI (8.0) installed on your machine. You can install .NET Aspire by adding the corresponding workload to your .NET installation. You add the aspire
workload using the following commands:
Different application components and/or dependencies may be executed as containers on your local machine. Currently, .NET Aspire supports two container runtimes: Docker Desktop and Podman. Although .NET Aspire defaults to Docker Desktop as the container runtime, you can switch to Podman by setting the DOTNET_ASPIRE_CONTAINER_RUNTIME
environment variable to podman
:
.NET Aspire orchestration goes to a special kind of project called the AppHost. We will not explore creating an AppHost project step-by-step as part of this article. Instead, we want to highlight some of the .NET Aspire-specific characteristics here. If you have never used or heard of .NET Aspire before, we would recommend you explore the official .NET Aspire documentation. It does a great job of explaining the core concepts of .NET Aspire and guiding you through the steps of orchestrating, running, and debugging distributed applications on your local machine.
Let’s take a look at a fairly simple orchestration consisting of a .NET WebAPI and a Redis cache (Redis cache capabilities are contributed by the Aspire.Hosting.Redis
NuGet-package):
Assuming that the WebAPI project is instrumented using OpenTelemetry (which could also be achieved using .NET Aspire Service Defaults), you can trace invocations using the .NET Aspire Dashboard when running the AppHost project locally.
Traces of a .NET WebAPI project in the .NET Aspire Dashboard
At Fermyon, we strongly believe that .NET Aspire is a productivity booster for many developers and organizations, and that’s why we started building an extension for .NET Aspire, allowing you to add Serverless WebAssembly Applications (Spin Apps) to your distributed application architectures. With Fermyon.Aspire.Spin
, you can add Spin Apps written in any language that compiles down to WebAssembly (the wasm32-wasi
platform) – to your application using the IDistributedApplicationBuilder
interface.
For the sake of flexibility, Fermyon.Aspire.Spin
allows you to add Spin Apps either by referencing the source code of your Spin Apps, or by pointing to an existing OCI reference that could be pulled from any OCI-compliant container registry (such as Azure Container Registry).
Using Fermyon.Aspire.Spin
, several functionalities are accessible:
To integrate Fermyon.Aspire.Spin
into your project, simply incorporate the appropriate NuGet package into your .NET Aspire AppHost project:
After the installation of Fermyon.Aspire.Spin
, you can utilize the extension methods included in the NuGet package to manage your distributed application seamlessly. These methods are available under the Aspire.Hosting
namespace, so there’s no need for additional namespace imports. Notably, the AddSpinApp
method lets you incorporate Spin Apps into the DistributedApplicationBuilder
.
The example below illustrates how to construct a distributed application that includes a Spin App, a .NET API, and a Redis cache:
You can manage the behavior for various host capabilities in a Spin App by using a Runtime Configuration File. Fermyon.Aspire.Spin
offers a SpinRuntimeConfigurationBuilder
, which allows you to construct a customized Runtime Configuration File:
It is often beneficial to use the same Runtime Configuration Files across different Spin Apps. This approach can be advantageous, especially in distributed systems where multiple Spin Apps need to utilize the same Redis instance for key-value storage. Here is how you can apply the SpinRuntimeConfigurationBuilder
(rtc
) across multiple applications within your distributed setup:
Spin Apps are initiated using the spin up
command in your .NET Aspire Host project. The --build
flag is also provided automatically by Fermyon.Aspire.Spin
, which ensures that the most recent version of your source code is compiled into WebAssembly.
Besides deploying Spin Apps by specifying the application source code path, you can link to existing Spin Apps by using the appropriate OCI reference:
In the example provided, a Spin App is created using a public OCI artifact sourced from Docker Hub.
It is often necessary to use private registries that require authentication for distributing OCI artifacts. Fermyon.Aspire.Spin
accommodates private registries by including the SpinRegistryLogin
lifecycle hook. The following example demonstrates how to utilize an OCI artifact from an Azure Container Registry (ACR), which necessitates authentication:
For the execution of Spin Apps via .NET Aspire, it’s essential to have the Spin CLI installed on both developer machines and Continuous Integration (CI) systems. Fermyon.Aspire.Spin
offers an essential Lifecycle Hook named CheckForSpin
, designed to confirm the installation of the Spin CLI on the system in use.
In cases where the Lifecycle Hook fails to detect the Spin CLI on a developer’s machine, an exception will be triggered during the bootstrap process of the distributed application.
The Spin CLI is equipped with a robust plugin system, enabling developers to enhance its functionality. To make sure Spin Apps are consistent across various development environments, use the *InstallSpinPlugin*
Lifecycle Hook to install necessary Spin CLI plugins during the initialization of the .NET Aspire Host project.
A notable plugin is kube
, designed to generate Kubernetes Deployment Manifests for your Spin Apps if you plan to deploy on SpinKube or the Fermyon Platform for Kubernetes. The code snippet below demonstrates the application of the InstallSpinPlugin
Lifecycle Hook to ensure the kube
plugin for Spin CLI is installed:
Built-in telemetry in Spin works seamlessly integrated with OpenTelemetry. Within the .NET Aspire framework, setting up OpenTelemetry for your Spin Apps is straightforward by using the WithOtlpExporter
function in your Spin App:
Upon running the distributed application again and making HTTP requests to the Spin App, it captures and forwards distributed traces to the designated OpenTelemetry Endpoint, managed by .NET Aspire:
Distributed Traces of a Spin App and underlying Redis key-value store in the .NET Aspire Dashboard
The .NET Aspire Dashboard provides important insights about all the components of your distributed applications, and Spin Apps are no exception here. The .NET Aspire Dashboard allows you to drill into:
As for distributed traces, Spin Apps contribute fundamental metrics through OpenTelemetry, which will be automatically picked up by .NET Aspire and visualized in the Metrics section of the .NET Aspire Dashboard.
Metrics of a Spin App visualized by the .NET Aspire Dashboard
Managing distributed applications that involve Spin Apps, .NET projects, and containers has become significantly more efficient with .NET Aspire, reducing complications and enhancing the overall development process. By utilizing C#, developers can easily set up and link various components of a distributed system, avoiding the need for extensive declarative coding.
The .NET Aspire Dashboard serves as a crucial information hub, offering insights into the runtime behavior of distributed applications, detailed views of configuration settings for distinct components, and access to extensive telemetry data produced by the system.
The integration of first-class support for Spin Apps with .NET Aspire marks a pivotal advancement in making applications faster, more secure, and easily portable, by harnessing the strengths of Spin and WebAssembly technology.
The source code is available here. Please report any issues, defects, or challenges you encounter while integrating your Spin Apps with .NET Aspire.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.