Master .NET Aspire: Level Up Your .NET Skills

Introduction to .NET Aspire:

Microsoft introduced a groundbreaking feature in the .NET 8 lineup – ".NET Aspire." As a developer, it has quickly become a go-to tool for building modern, cloud-optimized applications. In this blog, we’ll cover the core aspects of ".NET Aspire" and guide you through the installation process to get it up and run on your development setup. Once installed, we’ll launch a new project that showcases the power and flexibility it offers for cloud-native development.". NET Aspire" is specifically designed to support the creation of production-ready, observable distributed applications. It is structured as an opinionated framework, streamlining the development of cloud-native applications by leveraging microservices architecture. Instead of relying on monolithic codebases, ".NET Aspire" enables developers to build applications using smaller, interconnected components. This modularity facilitates smooth integration with essential cloud services, such as databases, messaging systems, and caching services, all packaged into a comprehensive set of NuGet packages.

Prerequisites:

You need to install the following tools locally:

  1. .NET 8
  2. .NET Aspire Workload: command installs:
    • dotnet workload update
    • dotnet workload install aspire
    • dotnet workload list
  1. Docker Desktop
  2. Visual Studio 2022 Preview version 17.9 or Higher

How to Create a .NET Aspire Application:

Master .NET Aspire: Level Up Your .NET Skills
  1. Open Visual Studio 2022: Start Visual Studio and select Create a new project.
  2. In the project setup dialog, type "Aspire" in the search bar and select .NET Aspire Application. Click Next to proceed.
  3. Verify that .NET 8.0 (Long Term Support) is chosen as the framework.
  4. Check the box for Use Redis for caching (requires Docker) and then click Create.

Once the project is created, you will have two key projects in your solution:

  • AppHost: This project serves as the orchestrator, managing connections and configurations for the various services and projects within your application.

The AppHost acts like the conductor of your application, orchestrating all your projects. It compiles them, sets up the environment, and ensures everything runs smoothly together. As you add more projects, AppHost will manage the coordination, handle dependencies and NuGet packages, establish connections, and ensure everything works in sync.

  • ServiceDefaults: This shared project contains reusable configurations across your solution, focusing on aspects such as resilience, service discovery, and telemetry.

Introducing ServiceDefaults, the powerhouse of Aspire. It's like a pre-configured toolkit for your app, providing telemetry, logging, metrics, and analytical setup and ready to go without any effort on your part.

Here’s the expected structure of your project directory:

Master .NET Aspire: Level Up Your .NET Skills

Let's run the project:

Master .NET Aspire: Level Up Your .NET Skills

The dashboard for the .NET Aspire project currently shows "No projects found" since we haven't added any projects yet. Let's add a simple API project to this Aspire solution to get started.

Master .NET Aspire: Level Up Your .NET Skills Master .NET Aspire: Level Up Your .NET Skills

I’ve created a basic ASP.NET Core Web API project named dotnet8Aspire.API.

Now, let’s add the API project to Aspire.

Master .NET Aspire: Level Up Your .NET Skills

The API project reference has been added to the Aspire project. Now, let's configure it in the Program.cs file of the AppHost project.

Master .NET Aspire: Level Up Your .NET Skills

When we add references to other projects in Aspire, all referenced projects will be accessible within the "projects" namespace.

In the dotnet8Aspire.API project, add a reference to the ServiceDefaults project and register the AddServiceDefault method in the Program.cs file.

Master .NET Aspire: Level Up Your .NET Skills

Monitoring using the .NET Aspire Dashboard

Launching the AppHost project starts the .NET Aspire dashboard, which monitors key aspects of your application. The dashboard offers detailed views for Projects, Containers, Executables, Logs, Traces, and Metrics, providing valuable insights into your application's performance and overall health.

Master .NET Aspire: Level Up Your .NET Skills

As we can see, our newly added project now appears on the dashboard. From here, we can run projects, view logs, manage environment variables, and more.

Projects: Displays key details for all individual .NET projects within your .NET Aspire app, including the application state, endpoint addresses, and the environment variables that were loaded.

Master .NET Aspire: Level Up Your .NET Skills

Containers: Provides essential details about your app containers, including their state, image tag, and port number. The Redis container you added for output catching will appear with the name you specified.

Master .NET Aspire: Level Up Your .NET Skills

Logs:

  1. Project: This section shows the output logs for the projects within your application. You can select which project's logs to view by using the drop-down menu at the top of the page.
Master .NET Aspire: Level Up Your .NET Skills

2. Container: This section displays logs from the containers in your application. You should see logs from the Redis container you configured in the template. If you have multiple containers, you can choose which one to display logs for using the drop-down menu at the top of the page.

Master .NET Aspire: Level Up Your .NET Skills

3. Executable: This section displays logs from executables within your application. Since the sample app doesn't include any executables, there won't be any logs shown here.

4. Structured: This section presents structured logs in a table format, supporting basic filtering, free-form search, and log-level filtering. You should see logs from the API service and Web frontend. To view more details for any log entry, click the View button at the right end of the row.

Master .NET Aspire: Level Up Your .NET Skills

5. Traces: This section shows traces for your application, allowing you to track the path of requests through different components. Find a request for /weather and click View on the right side of the page. The dashboard will display the stages of the request as it moves through various parts of your app.

Master .NET Aspire: Level Up Your .NET Skills

6. Metrics: This section displays various instruments and meters that are exposed within your app, along with their related dimensions. Filters are conditionally available based on the dimensions associated with each metric.

 

Master .NET Aspire: Level Up Your .NET Skills

Conclusion:

In summary, .NET Aspire offers a comprehensive, cloud-optimized framework designed for building and managing distributed microservices-based applications. From the easy project setup to powerful features like the Aspire dashboard, developers gain full control over their application's lifecycle. The dashboard provides essential insights through logs, traces, metrics, and structured logs, allowing real-time monitoring and management of services, containers, and executables. By integrating Redis caching and offering modular project organization, .NET Aspire equips developers to build resilient, production-ready applications with ease. Whether it's tracking requests, viewing logs, or managing services, .NET Aspire simplifies the complexities of modern cloud-native development.

 

Leverage the power of .NET Aspire with Blueflame Labs.

Our team can design and develop scalable, high-performance applications that meet your business needs.

  • Expert Guidance: Blueflame Labs offers deep expertise in .NET Aspire development, ensuring your projects are built with best practices and leverage the framework's full potential.
  • Custom Solutions: Their team can tailor .NET Aspire solutions to your specific business needs, providing you with a highly efficient and scalable application.
  • Cloud Optimization: Blueflame Labs specializes in building cloud-native applications, helping you optimize your projects for performance and cost-effectiveness on platforms like Azure.
  • Comprehensive Monitoring: They can set up robust monitoring and logging systems using .NET Aspire's dashboard, providing valuable insights into your application's health and performance.
  • End-to-End Solutions: From initial development to ongoing maintenance and support, Blueflame Labs offers a comprehensive range of services to meet your project requirements.

Ready to embark on your .NET Aspire journey? Visit Blueflame Labs today to learn more about our expert development services and how we can help you build cutting-edge cloud-native applications.