RabbitMQ 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.
dotnet add package Aspire.Hosting.RabbitMQAdd 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...
- 1
When Aspire adds a container image to the app host, as shown in the preceding example with the
docker.io/library/rabbitmqimage, it creates a new RabbitMQ server instance on your local machine. A reference to your RabbitMQ server (therabbitmqvariable) is added to theExampleProject. - 2
The RabbitMQ server resource includes default credentials with a
usernameof"guest"and randomly generatedpasswordusing theCreateDefaultPasswordParametermethod. - 3
The
WithReferencemethod configures a connection in theExampleProjectnamed"messaging".
Tip
If you'd rather connect to an existing RabbitMQ server, chain a call to AsExisting instead—passing the appropriate parameters.
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
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...
Note
Data bind mounts have limited functionality compared to volumes, which offer better performance, portability, and security, making them more suitable for production environments. However, bind mounts allow direct access and modification of files on the host system, ideal for development and testing where real-time changes are needed.
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
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
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.
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.