2 Managing infrastructure components with containers and integrations
This chapter introduces two main ways to connect infrastructure components to an Aspire application: by running them as Docker containers or by using Aspire integration libraries. It uses security infrastructure as the main example, but the ideas are meant to apply broadly to other enterprise services as well. Along the way, it explains why distributed applications often need shared services such as identity providers, caches, databases, and brokers, and how Aspire helps reduce duplicated configuration through service discovery and shared references.
The chapter first builds a clear picture of Docker containers, emphasizing that they package an application and its dependencies into lightweight, isolated, portable units. It then walks through adding a Keycloak container to an Aspire host, mounting realm data, setting environment variables, and wiring the container into both a web frontend and an API service. The authentication setup shows how the frontend uses OpenID Connect while the API uses JWT bearer authentication, with Aspire service discovery and backchannel HTTP clients used to resolve the identity provider cleanly in development.
The second half shifts to Aspire integrations, showing how they differ from raw containers by providing a higher-level way to host and consume services such as Redis. The Redis example demonstrates how the host registers the component and how the application consumes it for output caching. Finally, the chapter builds a custom Aspire integration from scratch using MailDev, illustrating the structure of an integration library, how it exposes resources and endpoints, and how it is launched from the Aspire host. The overall message is that Aspire makes infrastructure easier to compose, safer to configure, and more reusable across services.
Docker container structure
Interdependency between Keycloak container and hosted services
Aspire dashboard with a Keycloak container
Keycloak admin console
Keycloak admin login page
Selecting Keycloak realm
User management panel in Keycloak
Redis component shown in the Aspire dashboard
MailDev container running in the Aspire dashboard
MailDev user interface
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.
Building Distributed Applications that Work ebook for free