Overview

2 Managing infrastructure components with containers and integrations

In this chapter, the focus is on how an Aspire-based distributed application can manage supporting infrastructure alongside its own services. It introduces Docker containers as lightweight, isolated, and portable environments that package an application together with its dependencies, then uses Keycloak as the main example of a security-oriented infrastructure component. The chapter emphasizes that the same hosting principles apply to many other kinds of infrastructure, but the Keycloak scenario is especially useful for learning how Aspire wires resources together and simplifies service discovery.

The Keycloak walkthrough shows how to define a custom container resource, register it with the Aspire host, configure its image, ports, environment variables, and startup arguments, and optionally import preconfigured realms from a mounted JSON file. The web frontend and API service are then connected to that identity provider through Aspire references and service discovery, allowing the frontend to use OpenID Connect and the API to validate JWT bearer tokens. The chapter also highlights an important practical distinction between development and production: tightly coupling endpoint discovery and realm setup can be convenient locally, but production systems usually need separate, more persistent configuration steps.

After containers, the chapter contrasts raw Docker hosting with Aspire integration libraries, showing that integrations are more convenient abstractions for common components such as Redis. Using Redis output caching as an example, it demonstrates how a host project adds the integration package, registers the component, and exposes it to a web app that can then use distributed caching instead of only in-memory caching. Finally, the chapter explains how to build a custom Aspire integration from scratch by wrapping the MailDev SMTP container in a dedicated class library, defining resource metadata and connection strings, and hosting it from the Aspire app so the dashboard can manage and display it like any other integrated service.

Docker container structure
figure
Interdependency between Keycloak container and hosted services
figure
Aspire dashboard with a Keycloak container
figure
Keycloak admin console
figure
Keycloak admin login page
figure
Selecting Keycloak realm
figure
User management panel in Keycloak
figure
Redis component shown in the Aspire dashboard
figure
MailDev container running in the Aspire dashboard
figure
MailDev user interface
figure

Summary

  • Docker containers provide a consistent hosting environment for apps. They’re typically used to host shared infrastructure components, such as IdPs for SSO and databases.
  • Docker containers can be added to the Aspire app host by using its native methods. Additional configuration can be implemented via extension methods.
  • URLs resolved from various Aspire-hosted apps can be passed to Docker containers as environment variables. Aspire can also be configured to connect to externally hosted Docker containers.
  • Aspire integrations, also known as Aspire components, are special NuGet libraries that act as wrappers for Docker containers and references to managed resources.
  • Aspire integrations are designed to be much easier to set up than raw Docker containers.
  • An Aspire integration consists of a NuGet package referenced by the host project and a NuGet package referenced by the apps that connect to the container inside the integration.
  • The Aspire dashboard displays Aspire integrations as the types they wrap, such as Docker containers.
  • Although there is an extensive library of existing Aspire integrations, you can create custom ones.

FAQ

What is a Docker container and why is it useful for distributed applications?A Docker container is a lightweight, isolated environment that packages an application with its dependencies so it runs consistently across development, test, and production environments. This makes deployment simpler, reduces compatibility issues, and uses fewer resources than a traditional virtual machine.
How do Docker containers differ from virtual machines?Docker containers share the host operating system kernel, so they start faster and use fewer resources than virtual machines. VMs include a full guest OS per instance, while containers isolate applications using kernel features like namespaces and cgroups.
Why is Keycloak used in this chapter?Keycloak is used as an identity and access management solution to provide authentication and authorization for the sample application. It supports features like single sign-on (SSO), user federation, identity brokering, and customizable login flows.
How is a Keycloak container added to an Aspire host?A custom `KeycloakResource` is created, registered with `AddResource()`, configured with container image details, HTTP endpoint settings, and environment variables, and then launched through an Aspire extension method. This gives the host more control than using a generic container registration.
Why does the chapter use bind mounts with Keycloak?A bind mount maps a folder on the host machine to a path inside the container. In this chapter, it is used to make a realm import JSON file available to Keycloak so the container can automatically import users, clients, roles, and settings at startup.
Why is the realm import file copied to the Aspire host output directory?The realm file must be available when the host application runs, so the project file is updated to copy `Keycloak\import.json` to the output directory. This ensures Keycloak can access it through the bind mount during startup.
What is the purpose of Aspire service discovery in this chapter?Aspire service discovery lets hosted apps find and communicate with each other without hardcoding addresses. It reduces duplicated configuration and allows services like the web frontend and API to connect to Keycloak and Redis through registered references.
Why does the chapter use a backchannel `HttpClient` for OpenID Connect and JWT authentication?The backchannel `HttpClient` is used for secure server-to-server communication with Keycloak. It allows the app to resolve the IdP address through Aspire service discovery and configure authentication options without exposing those interactions through the browser.
How do Aspire integrations differ from raw Docker containers?Aspire integrations are higher-level libraries that simplify hosting and consuming infrastructure services. Unlike raw containers, they provide tailored extension methods, service registration patterns, and built-in support for linking apps to services like Redis or SQL Server.
How is a custom Aspire integration library, such as MailDev, created?A custom integration is built as a class library that references `Aspire.Hosting`, defines a resource class extending `ContainerResource`, and provides extension methods to register the container with the Aspire host. The host then adds the library reference and invokes the custom registration method.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free