Skip to content

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.

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 CLI — Add Aspire.Hosting.RabbitMQ package
aspire add rabbit

The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:

Aspire CLI — Example output prompt
Select an integration to add:
> rabbit (Aspire.Hosting.RabbitMQ)
> Other results listed as selectable options...

In your AppHost project, call AddRabbitMQ on the builder instance to add a RabbitMQ server resource:

AppHost.aspire
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging");
builder.AddProject<Projects.ExampleProject>()
.WithReference(rabbitmq);
// After adding all resources, run the app...
  1. 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 (the rabbitmq variable) is added to the ExampleProject.

  2. The RabbitMQ server resource includes default credentials with a username of "guest" and randomly generated password using the CreateDefaultPasswordParameter method.

  3. The WithReference method configures a connection in the ExampleProject 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:

AppHost.aspire
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:

AppHost.aspire
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:

AppHost.aspire
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:

AppHost.aspire
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:

The Aspire dashboard interface displays a list of resources with RabbitMQ selected. The right panel shows endpoints including a link labeled Management Plugin. The environment is a web-based dashboard with a clean, organized layout. Visible text includes Management Plugin and other resource names. The tone is neutral and informative, focusing on guiding users to access the RabbitMQ management plugin.

Log into the management plugin using the credentials you configured with parameters:

RabbitMQ management plugin dashboard showing an overview of queues, exchanges, and connections. The interface displays navigation tabs for Overview, Connections, Channels, Exchanges, Queues, Admin, and a search bar. Visible text includes RabbitMQ Management, Overview, Queues, Exchanges, and Connections. The environment is a web-based dashboard with a clean and organized layout. The tone is neutral and informative, focusing on providing management and monitoring information for RabbitMQ.

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.

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.

.NET CLI — Add Aspire.RabbitMQ.Client package
dotnet add package Aspire.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.

C# — Program.cs
builder.AddRabbitMQClient(connectionName: "messaging");

You can then retrieve the IConnection instance using dependency injection. For example, to retrieve the connection from an example service:

C# — ExampleService.cs
public class ExampleService(IConnection connection)
{
// Use connection...
}

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:

C# — Program.cs
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:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("chat")] IConnection chatConnection,
[FromKeyedServices("queue")] IConnection queueConnection)
{
// Use connections...
}

The Aspire RabbitMQ integration provides multiple options to configure the connection based on the requirements and conventions of your project.

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling the AddRabbitMQClient method:

C# — Program.cs
builder.AddRabbitMQClient(connectionName: "messaging");

Then the connection string is retrieved from the ConnectionStrings configuration section:

JSON — appsettings.json
{
"ConnectionStrings": {
"messaging": "amqp://username:password@localhost:5672"
}
}

For more information on how to format this connection string, see the RabbitMQ URI specification docs.

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:

JSON — appsettings.json
{
"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.

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:

C# — Program.cs
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:

C# — Program.cs
builder.AddRabbitMQClient(
"messaging",
configureConnectionFactory:
static factory => factory.ClientProvidedName = "MyApp");

By default, Aspire integrations enable health checks for all services. The Aspire RabbitMQ integration:

  • Adds the health check when DisableHealthChecks?displayProperty=nameWithType is false, 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.

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.

The Aspire RabbitMQ integration uses the following log categories:

  • RabbitMQ.Client

The Aspire RabbitMQ integration emits the following tracing activities using OpenTelemetry:

  • Aspire.RabbitMQ.Client

The Aspire RabbitMQ integration currently doesn’t support metrics by default.

Ask & Answer Collaborate Community Discuss Watch