RabbitMQ Integration
RabbitMQ is a reliable messaging and streaming broker, which is easy to deploy on cloud environments, on-premises, and on your local machine. The Aspire RabbitMQ integration enables you to connect to existing RabbitMQ instances, or create new instances from the docker.io/library/rabbitmq
container image.
Hosting integration
Section titled “Hosting integration”The RabbitMQ hosting integration models a RabbitMQ server as the RabbitMQServerResource
type. To access this type and its APIs add the 📦 Aspire.Hosting.RabbitMQ NuGet package in the AppHost project.
aspire add rabbit
The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:
Select an integration to add:
> rabbit (Aspire.Hosting.RabbitMQ)> Other results listed as selectable options...
<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="*" />
Add RabbitMQ server resource
Section titled “Add RabbitMQ server resource”In your AppHost project, call AddRabbitMQ
on the builder
instance to add a RabbitMQ server resource:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging");
builder.AddProject<Projects.ExampleProject>() .WithReference(rabbitmq);
// After adding all resources, run the app...
-
When Aspire adds a container image to the app host, as shown in the preceding example with the
docker.io/library/rabbitmq
image, it creates a new RabbitMQ server instance on your local machine. A reference to your RabbitMQ server (therabbitmq
variable) is added to theExampleProject
. -
The RabbitMQ server resource includes default credentials with a
username
of"guest"
and randomly generatedpassword
using theCreateDefaultPasswordParameter
method. -
The
WithReference
method configures a connection in theExampleProject
named"messaging"
.
Add RabbitMQ server resource with data volume
Section titled “Add RabbitMQ server resource with data volume”To add a data volume to the RabbitMQ server resource, call the WithDataVolume
method on the RabbitMQ server resource:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging") .WithDataVolume(isReadOnly: false);
builder.AddProject<Projects.ExampleProject>() .WithReference(rabbitmq);
// After adding all resources, run the app...
The data volume is used to persist the RabbitMQ server data outside the lifecycle of its container. The data volume is mounted at the /var/lib/rabbitmq
path in the RabbitMQ server container and when a name
parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.
Add RabbitMQ server resource with data bind mount
Section titled “Add RabbitMQ server resource with data bind mount”To add a data bind mount to the RabbitMQ server resource, call the WithDataBindMount
method:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging") .WithDataBindMount( source: @"/RabbitMQ/Data", isReadOnly: false);
builder.AddProject<Projects.ExampleProject>() .WithReference(rabbitmq);
// After adding all resources, run the app...
Data bind mounts rely on the host machine’s filesystem to persist the RabbitMQ server data across container restarts. The data bind mount is mounted at the C:\RabbitMQ\Data
on Windows (or /RabbitMQ/Data
on Unix) path on the host machine in the RabbitMQ server container. For more information on data bind mounts, see Docker docs: Bind mounts.
Add RabbitMQ server resource with parameters
Section titled “Add RabbitMQ server resource with parameters”When you want to explicitly provide the username and password used by the container image, you can provide these credentials as parameters. Consider the following alternative example:
var builder = DistributedApplication.CreateBuilder(args);
var username = builder.AddParameter("username", secret: true);var password = builder.AddParameter("password", secret: true);
var rabbitmq = builder.AddRabbitMQ("messaging", username, password);
builder.AddProject<Projects.ExampleProject>() .WithReference(rabbitmq);
// After adding all resources, run the app...
Add RabbitMQ server resource with management plugin
Section titled “Add RabbitMQ server resource with management plugin”To add the RabbitMQ management plugin to the RabbitMQ server resource, call the WithManagementPlugin
method. Remember to use parameters to set the credentials for the container. You’ll need these credentials to log into the management plugin:
var builder = DistributedApplication.CreateBuilder(args);
var username = builder.AddParameter("username", secret: true);var password = builder.AddParameter("password", secret: true);
var rabbitmq = builder.AddRabbitMQ("messaging", username, password) .WithManagementPlugin();
builder.AddProject<Projects.ExampleProject>() .WithReference(rabbitmq);
// After adding all resources, run the app...
The RabbitMQ management plugin provides an HTTP-based API for management and monitoring of your RabbitMQ server. Aspire adds another container image docker.io/library/rabbitmq-management
to the AppHost that runs the management plugin. You can access the management plugin from the Aspire dashboard by selecting an endpoint for your RabbitMQ resource:
Log into the management plugin using the credentials you configured with parameters:
Hosting integration health checks
Section titled “Hosting integration health checks”The RabbitMQ hosting integration automatically adds a health check for the RabbitMQ server resource. The health check verifies that the RabbitMQ server is running and that a connection can be established to it.
The hosting integration relies on the 📦 AspNetCore.HealthChecks.Rabbitmq NuGet package.
Client integration
Section titled “Client integration”To get started with the Aspire RabbitMQ client integration, install the 📦 Aspire.RabbitMQ.Client NuGet package in the client-consuming project, that is, the project for the application that uses the RabbitMQ client. The RabbitMQ client integration registers an IConnection instance that you can use to interact with RabbitMQ.
dotnet add package Aspire.RabbitMQ.Client
<PackageReference Include="Aspire.RabbitMQ.Client" Version="*" />
Add RabbitMQ client
Section titled “Add RabbitMQ client”In the Program.cs
file of your client-consuming project, call the AddRabbitMQClient
extension method on any IHostApplicationBuilder
to register an IConnection
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddRabbitMQClient(connectionName: "messaging");
You can then retrieve the IConnection
instance using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(IConnection connection){ // Use connection...}
Add keyed RabbitMQ client
Section titled “Add keyed RabbitMQ client”There might be situations where you want to register multiple IConnection
instances with different connection names. To register keyed RabbitMQ clients, call the AddKeyedRabbitMQClient
method:
builder.AddKeyedRabbitMQClient(name: "chat");builder.AddKeyedRabbitMQClient(name: "queue");
Then you can retrieve the IConnection
instances using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService( [FromKeyedServices("chat")] IConnection chatConnection, [FromKeyedServices("queue")] IConnection queueConnection){ // Use connections...}
Configuration
Section titled “Configuration”The Aspire RabbitMQ integration provides multiple options to configure the connection based on the requirements and conventions of your project.
Use a connection string
Section titled “Use a connection string”When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling the AddRabbitMQClient
method:
builder.AddRabbitMQClient(connectionName: "messaging");
Then the connection string is retrieved from the ConnectionStrings
configuration section:
{ "ConnectionStrings": { "messaging": "amqp://username:password@localhost:5672" }}
For more information on how to format this connection string, see the RabbitMQ URI specification docs.
Use configuration providers
Section titled “Use configuration providers”The Aspire RabbitMQ integration supports Configuration
. It loads the RabbitMQClientSettings
from configuration by using the Aspire:RabbitMQ:Client
key. The following snippet is an example of a :::no-loc text=“appsettings.json”::: file that configures some of the options:
{ "Aspire": { "RabbitMQ": { "Client": { "ConnectionString": "amqp://username:password@localhost:5672", "DisableHealthChecks": true, "DisableTracing": true, "MaxConnectRetryCount": 2 } } }}
For the complete RabbitMQ client integration JSON schema, see Aspire.RabbitMQ.Client/ConfigurationSchema.json.
Use inline delegates
Section titled “Use inline delegates”Also you can pass the Action<RabbitMQClientSettings> configureSettings
delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddRabbitMQClient( "messaging", static settings => settings.DisableHealthChecks = true);
You can also set up the IConnectionFactory using the Action<IConnectionFactory> configureConnectionFactory
delegate parameter of the AddRabbitMQClient
method. For example to set the client provided name for connections:
builder.AddRabbitMQClient( "messaging", configureConnectionFactory: static factory => factory.ClientProvidedName = "MyApp");
Client integration health checks
Section titled “Client integration health checks”By default, Aspire integrations enable health checks for all services. The Aspire RabbitMQ integration:
- Adds the health check when
DisableHealthChecks?displayProperty=nameWithType
isfalse
, which attempts to connect to and create a channel on the RabbitMQ server. - Integrates with the
/health
HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic.
Observability and telemetry
Section titled “Observability and telemetry”Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. Depending on the backing service, some integrations might only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.
Logging
Section titled “Logging”The Aspire RabbitMQ integration uses the following log categories:
RabbitMQ.Client
Tracing
Section titled “Tracing”The Aspire RabbitMQ integration emits the following tracing activities using OpenTelemetry:
Aspire.RabbitMQ.Client
Metrics
Section titled “Metrics”The Aspire RabbitMQ integration currently doesn’t support metrics by default.