.NET Aspire RabbitMQ component

In this article, you learn how to use the .NET Aspire RabbitMQ client message-broker. The Aspire.RabbitMQ.Client library is used to register an IConnection in the dependency injection (DI) container for connecting to a RabbitMQ server. It enables corresponding health check, logging and telemetry.

Get started

To get started with the .NET Aspire RabbitMQ component, install the Aspire.RabbitMQ.Client NuGet package.

dotnet add package Aspire.RabbitMQ.Client --prerelease

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your component-consuming project, call the AddRabbitMQClient extension method to register an IConnection for use via the dependency injection container. The method takes a connection name parameter.

builder.AddRabbitMQClient("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...
}

App host usage

In your app host project, register a RabbitMQ server and consume the connection using the following methods, such as AddRabbitMQ:

// Service registration
var messaging = builder.AddRabbitMQ("messaging");

// Service consumption
builder.AddProject<Projects.ExampleProject>()
       .WithReference(messaging);

The WithReference method configures a connection in the ExampleProject project named messaging.

Configuration

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

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 builder.AddRabbitMQ:

builder.AddRabbitMQ("RabbitMQConnection");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "RabbitMQConnection": "amqp://username:password@localhost:5672"
  }
}

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

Use configuration providers

The .NET Aspire RabbitMQ component supports Microsoft.Extensions.Configuration. It loads the RabbitMQClientSettings from configuration by using the Aspire:RabbitMQ:Client key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "RabbitMQ": {
      "Client": {
        "HealthChecks": false
      }
    }
  }
}

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.AddRabbitMQ(
    "messaging",
    static settings => settings.HealthChecks = false);

You can also set up the IConnectionFactory using the Action<IConnectionFactory> configureConnectionFactory delegate parameter of the AddRabbitMQ method. For example to set the client provided name for connections:

builder.AddRabbitMQ(
    "messaging",
    static configureConnectionFactory:
        factory => factory.ClientProvidedName = "MyApp");

Health checks

By default, .NET Aspire components enable health checks for all services. For more information, see .NET Aspire components overview.

The .NET Aspire RabbitMQ component handles the following:

  • Adds the health check when RabbitMQClientSettings.HealthChecks is true, 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

.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire RabbitMQ component uses the following log categories:

  • RabbitMQ.Client

Tracing

The .NET Aspire RabbitMQ component will emit the following tracing activities using OpenTelemetry:

  • "Aspire.RabbitMQ.Client"

Metrics

The .NET Aspire RabbitMQ component currently doesn't support metrics by default. If that changes in the future, this section will be updated to reflect those changes.

See also