Get started with the RabbitMQ integration

RabbitMQ logo

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.

In this introduction, you'll see how to install and use the Aspire RabbitMQ integrations in a simple configuration. If you already have this knowledge, see RabbitMQ hosting integration for full reference details.

Note

To follow this guide, you must have created an Aspire solution to work with. To learn how to do that, see Build your first Aspire app.

Set up hosting integration

To begin, install the Aspire RabbitMQ hosting integration in your Aspire AppHost project:

Install the NuGet package
dotnet add package Aspire.Hosting.RabbitMQ

Next, in the AppHost project, create instances of RabbitMQ resources and pass them to the consuming client projects:

var builder = DistributedApplication.CreateBuilder(args);
  
var rabbitmq = builder.AddRabbitMQ("messaging");
  
builder.AddProject<Projects.ExampleProject>()
       .WithReference(rabbitmq);
  
builder.Build().Run();

When Aspire adds a container image to the app host, it creates a new RabbitMQ server instance on your local machine. The RabbitMQ server resource includes default credentials with a username of "guest" and randomly generated password.

Tip

If you'd rather connect to an existing RabbitMQ server, chain a call to AsExisting instead—passing the appropriate parameters.

Set up client integration

To get started with the Aspire RabbitMQ client integration, install the package:

Install the NuGet package
dotnet add package Aspire.RabbitMQ.Client

In the Program.cs file of your client-consuming project, call the AddRabbitMQClient extension method to register an IConnection for use via the dependency injection container:

builder.AddRabbitMQClient(connectionName: "messaging");

Tip

The connectionName parameter must match the name used when adding the RabbitMQ server resource in the AppHost project.

You can then retrieve the IConnection instance using dependency injection:

public class ExampleService(IConnection connection)
{
    // Use connection...
}

See also